Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements fetcher runner by Cron instead of Config.MinFetchInterval #29

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 57 additions & 3 deletions README.md
Expand Up @@ -25,7 +25,7 @@ go get github.com/jpillora/overseer

This program works with process managers, supports graceful, zero-down time restarts and self-upgrades its own binary.

``` go
```go
package main

import (
Expand Down Expand Up @@ -77,11 +77,21 @@ See [Config](https://godoc.org/github.com/jpillora/overseer#Config)uration optio

### More examples

See the [example/](example/) directory and run `example.sh`, you should see the following output:

See the [example/](example/) directory and run `example.sh EXAMPLE_NAME`, you should see the following output:

```sh
$ cd example/
$ sh example.sh
```

Examples:
- default: `$ sh example.sh default` (see above)
- cron_default: `$ sh example.sh cron_default`
- cron_custom: `$ sh example.sh cron_custom`


```sh
$ sh example.sh default
BUILT APP (1)
RUNNING APP
app#1 (031c802ee74f00b2a5c52f2fe647523973c09441) listening...
Expand Down Expand Up @@ -148,6 +158,50 @@ func main() {
}
```

#### Using CRON

##### Using default cron instance

```go
import (
"github.com/robfig/cron"
)

func main() {
schedule, _ := cron.Parse("@daily")
overseer.Run(overseer.Config{
Program: prog,
Address: ":3000",

FetchCronSchedule: &schedule,
})
}
```

##### Using your cron instance

```go
import (
"github.com/robfig/cron"
)

func main() {
Cron := cron.New()
Cron.Start()
defer Cron.Stop()

schedule, _ := cron.Parse("@daily")
overseer.Run(overseer.Config{
Program: prog,
Address: ":3000",

Cron: Cron,
FetchCronSchedule: &schedule,
})
}
```


### Known issues

* The master process's `overseer.Config` cannot be changed via an upgrade, the master process must be restarted.
Expand Down
33 changes: 33 additions & 0 deletions example/cron_custom/main.go
@@ -0,0 +1,33 @@
package main

import (
"github.com/jpillora/overseer"
"github.com/jpillora/overseer/example"
"github.com/jpillora/overseer/fetcher"
"github.com/robfig/cron"
)

//see example.sh for the use-case

// BuildID is compile-time variable
var BuildID = "0"

//then create another 'main' which runs the upgrades
//'main()' is run in the initial process
func main() {
Cron := cron.New()
Cron.Start()
defer Cron.Stop()

schedule, _ := cron.Parse("@every 5s")

overseer.Run(overseer.Config{
Program: example.Prog(BuildID),
Address: ":5001",
Fetcher: &fetcher.File{Path: "my_app_next"},
Debug: false, //display log of overseer actions

Cron: Cron,
FetchCronSchedule: &schedule,
})
}
23 changes: 23 additions & 0 deletions example/cron_default/main.go
@@ -0,0 +1,23 @@
package main

import (
"github.com/jpillora/overseer"
"github.com/jpillora/overseer/example"
"github.com/jpillora/overseer/fetcher"
)

//see example.sh for the use-case

// BuildID is compile-time variable
var BuildID = "0"

//then create another 'main' which runs the upgrades
//'main()' is run in the initial process
func main() {
overseer.Run(overseer.Config{
Program: example.Prog(BuildID),
Address: ":5001",
Fetcher: &fetcher.File{Path: "my_app_next"},
Debug: false, //display log of overseer actions
})
}
23 changes: 23 additions & 0 deletions example/default/main.go
@@ -0,0 +1,23 @@
package main

import (
"github.com/jpillora/overseer"
"github.com/jpillora/overseer/example"
"github.com/jpillora/overseer/fetcher"
)

//see ../example.sh default for the use-case

// BuildID is compile-time variable
var BuildID = "0"

//then create another 'main' which runs the upgrades
//'main()' is run in the initial process
func main() {
overseer.Run(overseer.Config{
Program: example.Prog(BuildID),
Address: ":5001",
Fetcher: &fetcher.File{Path: "my_app_next"},
Debug: false, //display log of overseer actions
})
}
7 changes: 7 additions & 0 deletions example/example.sh
@@ -1,5 +1,12 @@
#!/bin/bash

if [ "$1" = '' ]; then
echo "Usage: $0 EXAMPLE_NAME" >&2
exit 1
fi

cd "$1" || exit $?

#NOTE: DONT CTRL+C OR CLEANUP WONT OCCUR
# ENSURE PORTS 5001,5002 ARE UNUSED

Expand Down
39 changes: 0 additions & 39 deletions example/main.go

This file was deleted.

24 changes: 24 additions & 0 deletions example/prog.go
@@ -0,0 +1,24 @@
package example

import (
"fmt"
"net/http"
"time"

"github.com/jpillora/overseer"
)

//convert your 'main()' into a 'prog(state)'
//'prog()' is run in a child process
func Prog(BuildID string) func(state overseer.State) {
return func(state overseer.State) {
fmt.Printf("app#%s (%s) listening...\n", BuildID, state.ID)
http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
d, _ := time.ParseDuration(r.URL.Query().Get("d"))
time.Sleep(d)
fmt.Fprintf(w, "app#%s (%s) says hello\n", BuildID, state.ID)
}))
http.Serve(state.Listener, nil)
fmt.Printf("app#%s (%s) exiting...\n", BuildID, state.ID)
}
}
7 changes: 7 additions & 0 deletions overseer.go
Expand Up @@ -5,6 +5,7 @@ package overseer
import (
"errors"
"fmt"
"github.com/robfig/cron"
"log"
"os"
"runtime"
Expand Down Expand Up @@ -44,6 +45,12 @@ type Config struct {
//This helps to prevent unwieldy fetch.Interfaces from hogging
//too many resources. Defaults to 1 second.
MinFetchInterval time.Duration
//FetchCronRule defines the cron (github.com/robfig/cron) rule instead of
//MinFetchInterval
FetchCronSchedule *cron.Schedule
//Cron cron instance for use instead of interval.
//See `FetchCronSchedule` and `MinFetchInterval` fields.
Cron *cron.Cron
//PreUpgrade runs after a binary has been retrieved, user defined checks
//can be run here and returning an error will cancel the upgrade.
PreUpgrade func(tempBinaryPath string) error
Expand Down
26 changes: 23 additions & 3 deletions proc_master.go
Expand Up @@ -19,6 +19,7 @@ import (
"time"

"github.com/kardianos/osext"
"github.com/robfig/cron"
)

var tmpBinPath = filepath.Join(os.TempDir(), "overseer-"+token())
Expand All @@ -40,6 +41,8 @@ type master struct {
descriptorsReleased chan bool
signalledAt time.Time
printCheckUpdate bool
fetchingMux sync.Mutex
fetching bool
}

func (mp *master) run() error {
Expand All @@ -59,8 +62,17 @@ func (mp *master) run() error {
}
if mp.Config.Fetcher != nil {
mp.printCheckUpdate = true
mp.fetch()
go mp.fetchLoop()
if mp.FetchCronSchedule != nil {
if mp.Cron == nil {
mp.Cron = cron.New()
mp.Cron.Start()
defer mp.Cron.Stop()
}
go mp.Cron.Schedule(*mp.FetchCronSchedule, cron.FuncJob(mp.fetch))
} else {
mp.fetch()
go mp.fetchLoop()
}
}
return mp.forkLoop()
}
Expand Down Expand Up @@ -196,9 +208,17 @@ func (mp *master) fetchLoop() {
}

func (mp *master) fetch() {
if mp.restarting {
if mp.fetching || mp.restarting {
return //skip if restarting
}
mp.fetchingMux.Lock()
mp.fetching = true

defer func() {
mp.fetching = false
mp.fetchingMux.Unlock()
}()

if mp.printCheckUpdate {
mp.debugf("checking for updates...")
}
Expand Down