Skip to content

Commit

Permalink
Merge pull request #31 from krallin/context-for-exit
Browse files Browse the repository at this point in the history
Use a context to exit after being signalled
  • Loading branch information
krallin authored Apr 23, 2018
2 parents c0abdc1 + 398e49a commit 2cf5742
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
4 changes: 2 additions & 2 deletions cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func monitorJob(ctx context.Context, expression crontab.Expression, t0 time.Time
}
}

func StartJob(wg *sync.WaitGroup, cronCtx *crontab.Context, job *crontab.Job, exitChan chan interface{}, cronLogger *logrus.Entry) {
func StartJob(wg *sync.WaitGroup, cronCtx *crontab.Context, job *crontab.Job, exitCtx context.Context, cronLogger *logrus.Entry) {
wg.Add(1)

go func() {
Expand All @@ -144,7 +144,7 @@ func StartJob(wg *sync.WaitGroup, cronCtx *crontab.Context, job *crontab.Job, ex
}

select {
case <-exitChan:
case <-exitCtx.Done():
cronLogger.Debug("shutting down")
return
case <-time.After(delay):
Expand Down
12 changes: 7 additions & 5 deletions cron/cron_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cron

import (
"context"
"fmt"
"github.com/aptible/supercronic/crontab"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -189,8 +190,10 @@ func TestStartJobExitsOnRequest(t *testing.T) {
logger, _ := newTestLogger()

var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
cancel()

StartJob(&wg, &basicContext, &job, exitChan, logger)
StartJob(&wg, &basicContext, &job, ctx, logger)

wg.Wait()
}
Expand All @@ -205,13 +208,12 @@ func TestStartJobRunsJob(t *testing.T) {
Position: 1,
}

exitChan := make(chan interface{}, 1)

var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())

logger, channel := newTestLogger()

StartJob(&wg, &basicContext, &job, exitChan, logger)
StartJob(&wg, &basicContext, &job, ctx, logger)

select {
case entry := <-channel:
Expand Down Expand Up @@ -255,6 +257,6 @@ func TestStartJobRunsJob(t *testing.T) {
t.Fatalf("timed out waiting for second success")
}

exitChan <- nil
cancel()
wg.Wait()
}
1 change: 1 addition & 0 deletions integration/exit.crontab
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* * * * * * * echo "$MSG_START" && sleep 2 && echo "$MSG_DONE"
35 changes: 35 additions & 0 deletions integration/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@ function run_supercronic() {
"${BATS_TEST_DIRNAME}/../supercronic" ${SUPERCRONIC_ARGS:-} "$crontab" 2>&1
}

setup () {
WORK_DIR="$(mktemp -d)"
export WORK_DIR
}

teardown() {
rm -r "$WORK_DIR"
}

wait_for() {
for i in $(seq 0 50); do
if "$@" > /dev/null 2>&1; then
return 0
fi
sleep 0.1
done

return 1
}

@test "it starts" {
run_supercronic "${BATS_TEST_DIRNAME}/noop.crontab"
}
Expand Down Expand Up @@ -42,3 +62,18 @@ function run_supercronic() {
@test "it supports JSON logging " {
SUPERCRONIC_ARGS="-json" run_supercronic "${BATS_TEST_DIRNAME}/noop.crontab" | grep -iE "^{"
}

@test "it waits for jobs to exit before terminating" {
ready="will start"
canary="all done"

out="${WORK_DIR}/out"

MSG_START="$ready" MSG_DONE="$canary" \
"${BATS_TEST_DIRNAME}/../supercronic" "${BATS_TEST_DIRNAME}/exit.crontab" >"$out" 2>&1 &

wait_for grep "$ready" "$out"
kill -TERM "$!"

wait_for grep "$canary" "$out"
}
16 changes: 5 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"flag"
"fmt"
"github.com/aptible/supercronic/cron"
Expand Down Expand Up @@ -48,22 +49,17 @@ func main() {
return
}

var (
wg sync.WaitGroup
exitChans []chan interface{}
)
var wg sync.WaitGroup
exitCtx, notifyExit := context.WithCancel(context.Background())

for _, job := range tab.Jobs {
exitChan := make(chan interface{}, 1)
exitChans = append(exitChans, exitChan)

cronLogger := logrus.WithFields(logrus.Fields{
"job.schedule": job.Schedule,
"job.command": job.Command,
"job.position": job.Position,
})

cron.StartJob(&wg, tab.Context, job, exitChan, cronLogger)
cron.StartJob(&wg, tab.Context, job, exitCtx, cronLogger)
}

termChan := make(chan os.Signal, 1)
Expand All @@ -72,9 +68,7 @@ func main() {
termSig := <-termChan

logrus.Infof("received %s, shutting down", termSig)
for _, c := range exitChans {
c <- true
}
notifyExit()

logrus.Info("waiting for jobs to finish")
wg.Wait()
Expand Down

0 comments on commit 2cf5742

Please sign in to comment.