-
Notifications
You must be signed in to change notification settings - Fork 241
/
signals.go
62 lines (56 loc) · 1.67 KB
/
signals.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Signal Handling in goworker
//
// To stop goworker, send a QUIT, TERM, or INT
// signal to the process. This will immediately
// stop job polling. There can be up to
// $CONCURRENCY jobs currently running, which
// will continue to run until they are finished.
//
// Failure Modes
//
// Like Resque, goworker makes no guarantees
// about the safety of jobs in the event of
// process shutdown. Workers must be both
// idempotent and tolerant to loss of the job in
// the event of failure.
//
// If the process is killed with a KILL or by a
// system failure, there may be one job that is
// currently in the poller's buffer that will be
// lost without any representation in either the
// queue or the worker variable.
//
// If you are running Goworker on a system like
// Heroku, which sends a TERM to signal a process
// that it needs to stop, ten seconds later sends
// a KILL to force the process to stop, your jobs
// must finish within 10 seconds or they may be
// lost. Jobs will be recoverable from the Redis
// database under
//
// resque:worker:<hostname>:<process-id>-<worker-id>:<queues>
//
// as a JSON object with keys queue, run_at, and
// payload, but the process is manual.
// Additionally, there is no guarantee that the
// job in Redis under the worker key has not
// finished, if the process is killed before
// goworker can flush the update to Redis.
package goworker
import (
"os"
"os/signal"
"syscall"
)
func signals() <-chan bool {
quit := make(chan bool)
go func() {
signals := make(chan os.Signal)
defer close(signals)
signal.Notify(signals, syscall.SIGQUIT, syscall.SIGTERM, os.Interrupt)
defer signalStop(signals)
<-signals
quit <- true
}()
return quit
}