Navigation Menu

Skip to content

Commit

Permalink
Configurable requeue interval
Browse files Browse the repository at this point in the history
  • Loading branch information
kjoe committed Jun 6, 2018
1 parent 4237d41 commit e9b2bf1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CONFIG.md
Expand Up @@ -32,6 +32,9 @@ The first time brooce runs, it will create a `~/.brooce` dir in your home direct
"command": "",
"time": 0
},
"requeue": {
"interval": 60
},
"queues": [
{
"name": "common",
Expand Down Expand Up @@ -83,6 +86,9 @@ The db which will be used by brooce on your redis server. Defaults to 0.
### `suicide.enable` / `suicide.command` / `suicide.time`
For example, if you enabled suicide and set command to `"sudo shutdown -h now"` and time to `600`, you could shutdown your server after there haven't been any jobs for some time. Useful for shutting down idle EC2 instances. Keep in mind that the brooce program will need to have proper permissions to execute the given command, without additional prompts for passwords.

### `requeue.interval`
The interval in seconds which will be used on requeueing delayed jobs. Defaults to 60.

### `queues`
Brooce is multithreaded, and can listen for commands on multiple queues. For example, you could do the following to run 5 threads on the common queue and 2 more threads on the rare queue.

Expand Down
10 changes: 9 additions & 1 deletion src/brooce/config/config.go
Expand Up @@ -48,6 +48,10 @@ type ConfigType struct {
Time int `json:"time"`
} `json:"suicide"`

Requeue struct {
Interval int `json:"interval"`
} `json:"requeue"`

Queues []Queue `json:"queues"`

Path string `json:"path"`
Expand Down Expand Up @@ -161,11 +165,15 @@ func initDefaultConfig() {
Config.Suicide.Command = "sudo shutdown -h now"
}

if Config.Suicide.Time == 0 {
if Config.Suicide.Time <= 0 {
Config.Suicide.Time = 600
}
}

if Config.Requeue.Interval <= 0 {
Config.Requeue.Interval = 60
}

if Config.Path != "" {
os.Setenv("PATH", os.Getenv("PATH")+":"+Config.Path)
}
Expand Down
3 changes: 2 additions & 1 deletion src/brooce/requeue/requeue.go
Expand Up @@ -11,11 +11,12 @@ import (
)

var redisHeader = config.Config.ClusterName
var requeueInterval = config.Config.Requeue.Interval

func Start() {
go func() {
for {
util.SleepUntilNextMinute()
util.SleepUntilNextInterval(requeueInterval)
err := requeue()
if err != nil {
log.Println("Error trying to requeue delayed jobs:", err)
Expand Down
12 changes: 8 additions & 4 deletions src/brooce/util/util.go
Expand Up @@ -12,15 +12,19 @@ import (
"time"
)

func SleepUntilNextMinute() {
func SleepUntilNextInterval(interval int) {
now := time.Now().Unix()
last_minute := now - now%60
next_minute := last_minute + 60
sleep_for := next_minute - now
last_interval := now - now % int64(interval)
next_interval := last_interval + int64(interval)
sleep_for := next_interval - now

time.Sleep(time.Duration(sleep_for) * time.Second)
}

func SleepUntilNextMinute() {
SleepUntilNextInterval(60)
}

func Md5sum(data interface{}) string {
hasher := md5.New()

Expand Down

0 comments on commit e9b2bf1

Please sign in to comment.