vkuznet / transfer2go

Distributed, loosely couple agent-based transferring system
MIT License
8 stars 2 forks source link

Make wrapper/supervisor for 24/7 operation #24

Closed vkuznet closed 7 years ago

rishiloyola commented 7 years ago

We can run services 24/7 using unit files. Check out this blog: link And also look at restart option of unit files. Which OS are we going to use?

vkuznet commented 7 years ago

Rishi, I'll have a look at your solution, but I was thinking about much simpler approach. Here it is, please try it out and decide: Let's say we have a server which may crash, here is a simple one which crashes periodically:

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    rand.Seed(123)
    count := 0
    for {
        time.Sleep(time.Duration(1) * time.Second)
        count += 1
        crash := rand.Intn(20)
        fmt.Println("count", count, "crash", crash)
        if count > crash {
            panic(fmt.Sprintf("test server crash at %d iteration", count))
        }
    }
}

I wanted to have a script which can start our server, monitor its behavior and restart it if necessary. I can accomplish this simply doing this:

#!/bin/sh
# go server
cmd=$PWD/goserver
args=${1+"$@"}
server(){
    # check if goserver is running
    for (( ; ; )); do
        local pid=`ps auxwww | egrep "$cmd" | grep -v grep | awk 'BEGIN{ORS=" "} {print $2}'`
        echo "PID=$pid"
        if [ -z "$pid" ]; then
            local tstamp=`date "+%Y/%m/%d %H:%M:%S"`
            echo "$tstamp goserver is not running, restart"
            $cmd $args
            if [ "$args" == "-help" ] || [ "$args" == "--help" ]; then
                break
            fi
        fi
        sleep 10
    done
}
server

And, you can run it as simple as:

./server.sh

that's it. The server.sh will ensure that goserver executable runs 24/7. It is very simple model which we can extend to our needs. More complex solution will require more maintenance and setup and provides for this specific use case almost no new functionality.

For the reference, our main OS is Linux.

On 0, Rishi notifications@github.com wrote:

We can run services 24/7 using unit files. Which OS are we going to use? Check out this blog : link And also look at restart option.

-- You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub: https://github.com/vkuznet/transfer2go/issues/24#issuecomment-304635011