Commit a6736534 authored by Darrell Kienzle's avatar Darrell Kienzle Committed by Leo Ahnn
Browse files

revert carlescere/scheduler to version that allows multiple concurrent jobs

cr: https://code.amazon.com/reviews/CR-56475096
parent 985a2ffb
......@@ -10,7 +10,7 @@ require (
github.com/Jeffail/gabs v1.0.0
github.com/Workiva/go-datastructures v1.0.53
github.com/aws/aws-sdk-go v1.35.23
github.com/carlescere/scheduler v0.0.0-20170109141437-ee74d2f83d82
github.com/carlescere/scheduler v0.0.0-20150615230211-9b78eac89dfb
github.com/cenkalti/backoff/v4 v4.0.2
github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575
github.com/coreos/go-semver v0.2.0
......
......@@ -47,6 +47,8 @@ github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/carlescere/scheduler v0.0.0-20150615230211-9b78eac89dfb h1:QiWfrCylK55VOrd0IBUlyMjaCM4J3CHhCJXhNUv3jcM=
github.com/carlescere/scheduler v0.0.0-20150615230211-9b78eac89dfb/go.mod h1:tyA14J0sA3Hph4dt+AfCjPrYR13+vVodshQSM7km9qw=
github.com/carlescere/scheduler v0.0.0-20170109141437-ee74d2f83d82 h1:9bAydALqAjBfPHd/eAiJBHnMZUYov8m2PkXVr+YGQeI=
github.com/carlescere/scheduler v0.0.0-20170109141437-ee74d2f83d82/go.mod h1:tyA14J0sA3Hph4dt+AfCjPrYR13+vVodshQSM7km9qw=
github.com/cenkalti/backoff/v4 v4.0.2 h1:JIufpQLbh4DkbQoii76ItQIUFzevQSqOLZca4eamEDs=
......
......@@ -3,8 +3,6 @@ go:
- 1.2
- 1.3
- 1.4
- 1.5
- 1.6
install:
- go get github.com/axw/gocov/gocov
......@@ -13,4 +11,3 @@ install:
- go get github.com/stretchr/testify/assert
script:
- $HOME/gopath/bin/goveralls -service=travis-ci -repotoken $COVERALLS_TOKEN
- go test -race
......@@ -9,43 +9,16 @@ Scheduler allows you to schedule recurrent jobs with an easy-to-read syntax.
Inspired by the article **[Rethinking Cron](http://adam.heroku.com/past/2010/4/13/rethinking_cron/)** and the **[schedule](https://github.com/dbader/schedule)** python module.
## How to use?
## How to use
```go
package main
import (
"fmt"
"runtime"
"time"
"github.com/carlescere/scheduler"
)
func main() {
job := func() {
t := time.Now()
fmt.Println("Time's up! @", t.UTC())
}
// Run every 2 seconds but not now.
scheduler.Every(2).Seconds().NotImmediately().Run(job)
// Run now and every X.
scheduler.Every(5).Minutes().Run(job)
scheduler.Every().Day().Run(job)
scheduler.Every().Monday().At("08:30").Run(job)
// Keep the program from not exiting.
runtime.Goexit()
job := func() {
fmt.Println("Time's up!")
}
```
## How it works?
By specifying the chain of calls, a `Job` struct is instantiated and a goroutine is starts observing the `Job`.
The goroutine will be on pause until:
* The next run scheduled is due. This will cause to execute the job.
* The `SkipWait` channel is activated. This will cause to execute the job.
* The `Quit` channel is activated. This will cause to finish the job.
scheduler.Every(5).Minutes().Run(job)
scheduler.Every().Day().Run(job)
scheduler.Every().Monday().At("08:30").Run(job)
```
## Not immediate recurrent jobs
By default the behaviour of the recurrent jobs (Every(N) seconds, minutes, hours) is to start executing the job right away and then wait the required amount of time. By calling specifically `.NotImmediately()` you can override that behaviour and not execute it directly when the function `Run()` is called.
......
......@@ -18,7 +18,6 @@ import (
"errors"
"strconv"
"strings"
"sync"
"time"
)
......@@ -28,13 +27,11 @@ type scheduled interface {
// Job defines a running job and allows to stop a scheduled job or run it.
type Job struct {
fn func()
Quit chan bool
SkipWait chan bool
err error
schedule scheduled
isRunning bool
sync.RWMutex
fn func()
Quit chan bool
SkipWait chan bool
err error
schedule scheduled
}
type recurrent struct {
......@@ -177,9 +174,9 @@ func (j *Job) Run(f func()) (*Job, error) {
case <-j.Quit:
return
case <-j.SkipWait:
go runJob(j)
go j.fn()
case <-time.After(next):
go runJob(j)
go j.fn()
}
next, _ = j.schedule.nextRun()
}
......@@ -187,22 +184,6 @@ func (j *Job) Run(f func()) (*Job, error) {
return j, nil
}
func (j *Job) setRunning(running bool) {
j.Lock()
defer j.Unlock()
j.isRunning = running
}
func runJob(job *Job) {
if job.IsRunning() {
return
}
job.setRunning(true)
job.fn()
job.setRunning(false)
}
func parseTime(str string) (hour, min, sec int, err error) {
chunks := strings.Split(str, ":")
var hourStr, minStr, secStr string
......@@ -318,10 +299,3 @@ func (j *Job) Minutes() *Job {
func (j *Job) Hours() *Job {
return j.timeOfDay(time.Hour)
}
// IsRunning returns if the job is currently running
func (j *Job) IsRunning() bool {
j.RLock()
defer j.RUnlock()
return j.isRunning
}
......@@ -70,7 +70,7 @@ github.com/aws/aws-sdk-go/service/ssmmds/ssmmdsiface
github.com/aws/aws-sdk-go/service/ssmmds/ssmmdsiface/mocks
github.com/aws/aws-sdk-go/service/sts
github.com/aws/aws-sdk-go/service/sts/stsiface
# github.com/carlescere/scheduler v0.0.0-20170109141437-ee74d2f83d82
# github.com/carlescere/scheduler v0.0.0-20150615230211-9b78eac89dfb
## explicit
github.com/carlescere/scheduler
# github.com/cenkalti/backoff/v4 v4.0.2
......@@ -272,8 +272,12 @@ golang.org/x/sys/windows/svc/mgr
# gopkg.in/ini.v1 v1.62.0
## explicit
gopkg.in/ini.v1
# gopkg.in/warnings.v0 v0.1.2
gopkg.in/warnings.v0
# gopkg.in/yaml.v2 v2.3.0
## explicit
gopkg.in/yaml.v2
# gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
gopkg.in/yaml.v3
# github.com/aws/aws-sdk-go => ./extra/aws-sdk-go
# github.com/nightlyone/lockfile => ./extra/lockfile
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment