-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add job & worker status - add Job.Done() to signal main process (like `ctx.Done()`) - implement workerPool.PushAndWait - add workerPool unit test to test the main flow
- Loading branch information
1 parent
ddc3b40
commit fbfd797
Showing
6 changed files
with
243 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
module github.com/anantadwi13/worker | ||
module github.com/anantadwi13/workers | ||
|
||
go 1.14 | ||
|
||
require github.com/google/uuid v1.3.0 | ||
require ( | ||
github.com/google/uuid v1.3.0 | ||
github.com/stretchr/testify v1.7.1 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,13 @@ | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= | ||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= | ||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,40 @@ | ||
package worker | ||
package workers | ||
|
||
import "context" | ||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
type ChanSignal struct{} | ||
type Status int | ||
|
||
const ( | ||
StatusCreated Status = iota | ||
StatusRunning | ||
StatusStopped | ||
) | ||
|
||
type Job interface { | ||
Id() string | ||
// ctx contains a job timeout | ||
Status() Status | ||
Done() chan ChanSignal | ||
|
||
// Func below should be called by worker | ||
|
||
// Do should be blocking the process until the job is finished or canceled. ctx contains a job timeout | ||
Do(ctx context.Context) | ||
// Cancel will block the process until the job is gracefully canceled. ctx contains a cancellation deadline | ||
// Cancel should be blocking the process until the job is gracefully canceled. ctx contains a cancellation deadline | ||
Cancel(ctx context.Context) | ||
} | ||
|
||
type Worker interface { | ||
Start() error | ||
Shutdown() error | ||
Status() Status | ||
|
||
// GetJobTimeout returns a timeout in seconds | ||
GetJobTimeout() int | ||
// GetShutdownTimeout returns a timeout in seconds | ||
GetShutdownTimeout() int | ||
GetJobTimeout() time.Duration | ||
GetShutdownTimeout() time.Duration | ||
|
||
Push(job Job) error | ||
PushAndWait(job Job) error | ||
} | ||
|
||
type IsCanceled struct{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.