From fc020d274b4358f51efe4e4991055514c222be83 Mon Sep 17 00:00:00 2001 From: Suhas Karanth Date: Fri, 12 Mar 2021 13:34:35 +0530 Subject: [PATCH] Add method to check if pool has been started (#15) --- README.md | 14 +++++++++++--- worker_pool.go | 5 +++++ worker_pool_test.go | 12 ++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e312b35e..a0c84d38 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,8 @@ gocraft/work lets you enqueue and processes background jobs in Go. Jobs are dura * Periodically enqueue jobs on a cron-like schedule. * Pause / unpause jobs and control concurrency within and across processes +--- + ## Fork - Important Changes #### Usage @@ -42,13 +44,19 @@ in-progress queue was lost. #### Expose lock count & max concurrency for each job (#2) Added to the queue info accessible from -[`work.Client.Queues()`](https://pkg.go.dev/github.com/gojek/work#Client.Queues). Useful for alerting when lock count is -consistently equal to the max concurrency possibly indicating that stale lock count is resulting in jobs not being -picked up. +[`work.Client.Queues()`](/client.go#L205-L212). Useful for alerting when lock count is consistently equal to the max +concurrency possibly indicating that stale lock count is resulting in jobs not being picked up. For the cleanup to be thorough, [`work.(*WorkerPool).Stop`](https://pkg.go.dev/github.com/gojek/work#WorkerPool.Stop) would need to be called on each worker pool instance. +#### Worker pool started check + +Expose [`work.(*WorkerPool).Started`](/worker_pool.go#L195-L198) which can be used to check if the worker pool has been +started and is running. + +--- + ## Enqueue new jobs To enqueue jobs, you need to make an Enqueuer with a redis namespace and a redigo pool. Each enqueued job has a name and can take optional arguments. Arguments are k/v pairs (serialized as JSON internally). diff --git a/worker_pool.go b/worker_pool.go index 159af7b9..062534a2 100644 --- a/worker_pool.go +++ b/worker_pool.go @@ -192,6 +192,11 @@ func (wp *WorkerPool) PeriodicallyEnqueue(spec string, jobName string) *WorkerPo return wp } +// Started returns true if the worker pool has been started. +func (wp *WorkerPool) Started() bool { + return wp.started +} + // Start starts the workers and associated processes. func (wp *WorkerPool) Start() { if wp.started { diff --git a/worker_pool_test.go b/worker_pool_test.go index feef4ff4..de1c7bbc 100644 --- a/worker_pool_test.go +++ b/worker_pool_test.go @@ -84,6 +84,18 @@ func TestWorkerPoolStartStop(t *testing.T) { wp.Stop() } +func TestWorkerPoolStarted(t *testing.T) { + pool := newTestPool(t) + ns := "work" + wp := NewWorkerPool(TestContext{}, 10, ns, pool) + + assert.False(t, wp.Started()) + wp.Start() + assert.True(t, wp.Started()) + wp.Stop() + assert.False(t, wp.Started()) +} + func TestWorkerPoolValidations(t *testing.T) { pool := newTestPool(t) ns := "work"