Skip to content

Commit

Permalink
Add method to check if pool has been started (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
sudo-suhas authored Mar 12, 2021
1 parent bc58564 commit fc020d2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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).
Expand Down
5 changes: 5 additions & 0 deletions worker_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions worker_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit fc020d2

Please sign in to comment.