Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add job arguments to periodic jobs #162

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/gocraft/work
module github.com/mrNobody95/redisGoWork

go 1.14

Expand All @@ -15,7 +15,6 @@ require (
github.com/garyburd/redigo v1.6.0 // indirect
github.com/gocraft/health v0.0.0-20170925182251-8675af27fef0
github.com/gocraft/web v0.0.0-20190207150652-9707327fb69b
github.com/gocraft/work v0.5.1
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/gomodule/redigo v2.0.0+incompatible
github.com/jrallison/go-workers v0.0.0-20180112190529-dbf81d0b75bb
Expand All @@ -24,6 +23,5 @@ require (
github.com/robfig/cron v1.2.0 // indirect
github.com/robfig/cron/v3 v3.0.1
github.com/stretchr/testify v1.5.1
github.com/youtube/vitess v2.1.1+incompatible // indirect
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 // indirect
)
3 changes: 2 additions & 1 deletion periodic_enqueuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type periodicJob struct {
jobName string
spec string
schedule cron.Schedule
args map[string]interface{}
}

type scheduledPeriodicJob struct {
Expand Down Expand Up @@ -102,7 +103,7 @@ func (pe *periodicEnqueuer) enqueue() error {

// This is technically wrong, but this lets the bytes be identical for the same periodic job instance. If we don't do this, we'd need to use a different approach -- probably giving each periodic job its own history of the past 100 periodic jobs, and only scheduling a job if it's not in the history.
EnqueuedAt: epoch,
Args: nil,
Args: pj.args,
}

rawJSON, err := job.serialize()
Expand Down
6 changes: 3 additions & 3 deletions worker_pool.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package work

import (
"github.com/robfig/cron/v3"
"reflect"
"sort"
"strings"
"sync"

"github.com/gomodule/redigo/redis"
"github.com/robfig/cron/v3"
)

// WorkerPool represents a pool of workers. It forms the primary API of gocraft/work. WorkerPools provide the public API of gocraft/work. You can attach jobs and middlware to them. You can start and stop them. Based on their concurrency setting, they'll spin up N worker goroutines.
Expand Down Expand Up @@ -179,15 +179,15 @@ func (wp *WorkerPool) JobWithOptions(name string, jobOpts JobOptions, fn interfa
// The spec format is based on https://godoc.org/github.com/robfig/cron, which is a relatively standard cron format.
// Note that the first value is the seconds!
// If you have multiple worker pools on different machines, they'll all coordinate and only enqueue your job once.
func (wp *WorkerPool) PeriodicallyEnqueue(spec string, jobName string) *WorkerPool {
func (wp *WorkerPool) PeriodicallyEnqueue(spec string, jobName string, jobArgs map[string]interface{}) *WorkerPool {
p := cron.NewParser(cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor)

schedule, err := p.Parse(spec)
if err != nil {
panic(err)
}

wp.periodicJobs = append(wp.periodicJobs, &periodicJob{jobName: jobName, spec: spec, schedule: schedule})
wp.periodicJobs = append(wp.periodicJobs, &periodicJob{jobName: jobName, spec: spec, schedule: schedule, args: jobArgs})

return wp
}
Expand Down