From e9b2bf102963cf0aa0043a0ecb2cc1a1dca615dc Mon Sep 17 00:00:00 2001 From: Jozsef Koszo Date: Wed, 6 Jun 2018 19:12:36 +0200 Subject: [PATCH] Configurable requeue interval --- CONFIG.md | 6 ++++++ src/brooce/config/config.go | 10 +++++++++- src/brooce/requeue/requeue.go | 3 ++- src/brooce/util/util.go | 12 ++++++++---- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CONFIG.md b/CONFIG.md index c74442c..98372da 100644 --- a/CONFIG.md +++ b/CONFIG.md @@ -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", @@ -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. diff --git a/src/brooce/config/config.go b/src/brooce/config/config.go index ece0e63..7a02e6a 100755 --- a/src/brooce/config/config.go +++ b/src/brooce/config/config.go @@ -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"` @@ -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) } diff --git a/src/brooce/requeue/requeue.go b/src/brooce/requeue/requeue.go index d0b1e8b..10a8ac0 100755 --- a/src/brooce/requeue/requeue.go +++ b/src/brooce/requeue/requeue.go @@ -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) diff --git a/src/brooce/util/util.go b/src/brooce/util/util.go index 2144646..c52abcb 100644 --- a/src/brooce/util/util.go +++ b/src/brooce/util/util.go @@ -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()