From aa9a51d7380cd7508a9243f6d3257490d9f2c3db Mon Sep 17 00:00:00 2001 From: Sven Nierlein Date: Mon, 4 Nov 2024 17:37:29 +0100 Subject: [PATCH] wait 5 seconds on shutdown --- .golangci.yml | 1 + Changes | 1 + pkg/modgearman/decrypt.go | 5 +++++ pkg/modgearman/mainWorker.go | 2 +- pkg/modgearman/worker.go | 12 +++++++++--- 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index bb1baa1..6c0e513 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -64,6 +64,7 @@ issues: - 'Magic number: 1\D ' - 'Magic number: 2\D ' - 'Magic number: 3\D ' + - 'Magic number: 5\D ' - 'Magic number: 10\D ' - 'Magic number: 30\D ' - 'Magic number: 16\D ' diff --git a/Changes b/Changes index bf4fc94..18fb6c1 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ This file documents the revision history for the Mod-Gearman-Worker-Go next: + - change graceful shutdown timeout to 5seconds - fix epn warnings perspective when using printf wrong 1.5.2 Wed Sep 4 11:03:28 CEST 2024 diff --git a/pkg/modgearman/decrypt.go b/pkg/modgearman/decrypt.go index d4f9a99..4814251 100644 --- a/pkg/modgearman/decrypt.go +++ b/pkg/modgearman/decrypt.go @@ -15,6 +15,7 @@ var myCipher cipher.Block type request struct { typ string resultQueue string + targetQueue string hostName string serviceDescription string startTime float64 @@ -25,12 +26,14 @@ type request struct { ballooning bool // flag wether this job has been put into background Cancel func() // cancel current job Canceled bool + rawRequest []byte } func (r *request) String() string { return fmt.Sprintf( "\n\t type: %s\n"+ "\t result_queue: %s\n"+ + "\t target_queue: %s\n"+ "\t host_name: %s\n"+ "\t service_description: %s\n"+ "\t start_time: %f\n"+ @@ -40,6 +43,7 @@ func (r *request) String() string { "\t command_line: %s\n\n", r.typ, r.resultQueue, + r.targetQueue, r.hostName, r.serviceDescription, r.startTime, @@ -109,6 +113,7 @@ func createReceived(input []byte) (*request, error) { // then extract them and store them result.typ = stringMap["type"] result.resultQueue = stringMap["result_queue"] + result.targetQueue = stringMap["target_queue"] result.hostName = stringMap["host_name"] result.serviceDescription = stringMap["service_description"] result.commandLine = stringMap["command_line"] diff --git a/pkg/modgearman/mainWorker.go b/pkg/modgearman/mainWorker.go index 4193923..25ee801 100644 --- a/pkg/modgearman/mainWorker.go +++ b/pkg/modgearman/mainWorker.go @@ -550,7 +550,7 @@ func (w *mainWorker) StopAllWorker(state MainStateType) { } // do not wait on shutdown via sigint - wait := 10 * time.Second + wait := 5 * time.Second if state == Shutdown { wait = 1 * time.Second } diff --git a/pkg/modgearman/worker.go b/pkg/modgearman/worker.go index 8e51345..23c78db 100644 --- a/pkg/modgearman/worker.go +++ b/pkg/modgearman/worker.go @@ -121,13 +121,15 @@ func (worker *worker) doWork(job libworker.Job) (res []byte, err error) { worker.activeJobs++ - received, err := decrypt((decodeBase64(string(job.Data()))), worker.config.encryption) + rawRequest := job.Data() + received, err := decrypt(decodeBase64(string(rawRequest)), worker.config.encryption) if err != nil { log.Errorf("decrypt failed: %w", err) worker.activeJobs-- return nil, err } + received.rawRequest = rawRequest worker.mainWorker.tasks++ logJob(job, received, "incoming", nil) @@ -291,9 +293,13 @@ func (worker *worker) Cancel() { log.Debugf("worker %s cancling current jobs", worker.id) worker.lock.Lock() for _, j := range worker.jobs { - if j.Cancel != nil { - j.Cancel() + if j.Cancel == nil { + continue } + j.Cancel() + } + if worker.worker != nil { + worker.worker.Close() } worker.lock.Unlock() }