Skip to content

Commit

Permalink
One off jobs (distribworks#137)
Browse files Browse the repository at this point in the history
One off jobs
  • Loading branch information
Victor Castell committed Jun 2, 2016
1 parent 0a9b4de commit b9b0e43
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 6 deletions.
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,4 @@ script:
- go vet ./...
# - fgt golint ./...
- glide install
- go test -v ./dkron

env:
global:
- GO15VENDOREXPERIMENT=1
- go test -v $(glide novendor)
13 changes: 13 additions & 0 deletions cron/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ Note: The interval does not take the job runtime into account. For example,
if a job takes 3 minutes to run, and it is scheduled to run every 5 minutes,
it will have only 2 minutes of idle time between each run.
Fixed times
You may also schedule a job to execute once and never more. This is supported by
formatting the cron spec like this:
@at <datetime>
Where "datetime" is a string accepted by time.Parse in RFC 3339/ISO 8601 format
(https://golang.org/pkg/time/#Parse).
For example, "@at 2018-01-02T15:04:00" would run the job on the specified date and time
assuming UTC timezone.
Time zones
All interpretation and scheduling is done in the machine's local time zone (as
Expand Down
9 changes: 9 additions & 0 deletions cron/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ func parseDescriptor(spec string) Schedule {
return Every(duration)
}

const at = "@at "
if strings.HasPrefix(spec, at) {
date, err := time.Parse(time.RFC3339, spec[len(at):])
if err != nil {
log.Panicf("Failed to parse date %s: %s", spec, err)
}
return At(date)
}

log.Panicf("Unrecognized descriptor: %s", spec)
return nil
}
2 changes: 2 additions & 0 deletions cron/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func TestSpecSchedule(t *testing.T) {
}{
{"* 5 * * * *", &SpecSchedule{all(seconds), 1 << 5, all(hours), all(dom), all(months), all(dow)}},
{"@every 5m", ConstantDelaySchedule{time.Duration(5) * time.Minute}},
{"@at 2018-01-02T15:04:00Z", SimpleSchedule{time.Date(2018, time.January, 2, 15, 4, 0, 0, time.UTC)}},
{"@at 2019-02-04T09:20:00+06:00", SimpleSchedule{time.Date(2019, time.February, 4, 9, 20, 0, 0, time.FixedZone("", 21600))}},
}

for _, c := range entries {
Expand Down
21 changes: 21 additions & 0 deletions cron/simple.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cron

import "time"

// SimpleDelaySchedule represents a simple non recurring duration.
type SimpleSchedule struct {
Date time.Time
}

// Just store the given time for this schedule.
func At(date time.Time) SimpleSchedule {
return SimpleSchedule{
Date: date,
}
}

// Next conforms to the Schedule interface but this kind of jobs
// doesn't need to be run more than once, so it doesn't return a new date but the existing one.
func (schedule SimpleSchedule) Next(t time.Time) time.Time {
return schedule.Date
}
2 changes: 1 addition & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
docker-compose up -d etcd
export COMPOSE_ETCD_PORT=`docker port dkron_etcd_1 4001/tcp | cut -d":" -f 2`
export DKRON_BACKEND_MACHINE=`docker-machine ip default`:$COMPOSE_ETCD_PORT
go test -v ./dkron $1
go test -v $(glide novendor) $1

0 comments on commit b9b0e43

Please sign in to comment.