Skip to content

Commit

Permalink
Clean up (go-co-op#92)
Browse files Browse the repository at this point in the history
* add makefile

* remove deprecated StartImmediately()

* go test -race, remove race conditions

* use make vet, test in actions
  • Loading branch information
JohnRoesler authored Dec 17, 2020
1 parent f0a0fc2 commit caab14a
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 114 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/go_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ jobs:
- name: Checkout code
uses: actions/checkout@v1
- name: fmt
run: gofmt -w .
run: make check-fmt
- name: lint
run: |
go get golang.org/x/lint/golint
$(go list -f {{.Target}} golang.org/x/lint/golint) -set_exit_status ./...
- name: vet
run: go vet ./...
run: make vet
- name: test
run: go test ./...
run: make test
33 changes: 33 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.PHONY: fmt check-fmt lint vet test

GO_PKGS := $(shell go list -f {{.Dir}} ./...)

fmt:
@go list -f {{.Dir}} ./... | xargs -I{} gofmt -w -s {}

check-fmt:
@echo "Checking formatting..."
@FMT="0"; \
for pkg in $(GO_PKGS); do \
OUTPUT=`gofmt -l $$pkg/*.go`; \
if [ -n "$$OUTPUT" ]; then \
echo "$$OUTPUT"; \
FMT="1"; \
fi; \
done ; \
if [ "$$FMT" -eq "1" ]; then \
echo "Problem with formatting in files above."; \
exit 1; \
else \
echo "Success - way to run gofmt!"; \
fi

lint:
# Add -set_exit_status=true when/if we want to enforce the linter rules
@golint -min_confidence 0.8 -set_exit_status $(GO_PKGS)

vet:
@go vet $(GO_FLAGS) $(GO_PKGS)

test:
@go test -race $(GO_FLAGS) -count=1 $(GO_PKGS)
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ func main() {
// Delay start of job
s2.Every(1).Hour().StartAt(time.Now().Add(time.Duration(1 * time.Hour)).Do(task)

// Deprecated: Jobs start immediately by default
// use StartImmediately() to run job upon scheduler start
s2.Every(1).Hour().StartImmediately().Do(task)

// NextRun gets the next running time
_, time := s2.NextRun()
fmt.Println(time)
Expand Down
7 changes: 0 additions & 7 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ func ExampleScheduler_StartAsync() {
s.StartAsync()
}

// Deprecated: All jobs start immediately by default unless set to a specific date or time
func ExampleScheduler_StartImmediately() {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Hour().StartImmediately().Do(task)
s.StartBlocking()
}

func ExampleScheduler_StartAt() {
s := gocron.NewScheduler(time.UTC)
specificTime := time.Date(2019, time.November, 10, 15, 0, 0, 0, time.UTC)
Expand Down
96 changes: 87 additions & 9 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ type Job struct {
}

type runConfig struct {
finiteRuns bool
maxRuns int
removeAfterLastRun bool
finiteRuns bool
maxRuns int
removeAfterLastRun bool
}

// NewJob creates a new Job with the provided interval
Expand All @@ -56,17 +56,47 @@ func (j *Job) run() {
}

func (j *Job) neverRan() bool {
j.RLock()
defer j.RUnlock()
return j.lastRun.IsZero()
}

func (j *Job) getStartsImmediately() bool {
j.RLock()
defer j.RUnlock()
return j.startsImmediately
}

func (j *Job) setStartsImmediately(b bool) {
j.Lock()
defer j.Unlock()
j.startsImmediately = b
}

func (j *Job) getAtTime() time.Duration {
j.RLock()
defer j.RUnlock()
return j.atTime
}

func (j *Job) setAtTime(t time.Duration) {
j.Lock()
defer j.Unlock()
j.atTime = t
}

// Err returns an error if one occurred while creating the Job
func (j *Job) Err() error {
j.RLock()
defer j.RUnlock()
return j.err
}

// Tag allows you to add arbitrary labels to a Job that do not
// impact the functionality of the Job
func (j *Job) Tag(t string, others ...string) {
j.Lock()
defer j.Unlock()
j.tags = append(j.tags, t)
for _, tag := range others {
j.tags = append(j.tags, tag)
Expand All @@ -75,6 +105,8 @@ func (j *Job) Tag(t string, others ...string) {

// Untag removes a tag from a Job
func (j *Job) Untag(t string) {
j.Lock()
defer j.Unlock()
var newTags []string
for _, tag := range j.tags {
if t != tag {
Expand All @@ -87,22 +119,30 @@ func (j *Job) Untag(t string) {

// Tags returns the tags attached to the Job
func (j *Job) Tags() []string {
j.RLock()
defer j.RUnlock()
return j.tags
}

// ScheduledTime returns the time of the Job's next scheduled run
func (j *Job) ScheduledTime() time.Time {
j.RLock()
defer j.RUnlock()
return j.nextRun
}

// ScheduledAtTime returns the specific time of day the Job will run at
func (j *Job) ScheduledAtTime() string {
j.RLock()
defer j.RUnlock()
return fmt.Sprintf("%d:%d", j.atTime/time.Hour, (j.atTime%time.Hour)/time.Minute)
}

// Weekday returns which day of the week the Job will run on and
// will return an error if the Job is not scheduled weekly
func (j *Job) Weekday() (time.Weekday, error) {
j.RLock()
defer j.RUnlock()
if j.scheduledWeekday == nil {
return time.Sunday, ErrNotScheduledWeekday
}
Expand All @@ -113,6 +153,8 @@ func (j *Job) Weekday() (time.Weekday, error) {
// job to n. However, the job will still remain in the
// scheduler
func (j *Job) LimitRunsTo(n int) {
j.Lock()
defer j.Unlock()
j.runConfig = runConfig{
finiteRuns: true,
maxRuns: n,
Expand All @@ -122,36 +164,72 @@ func (j *Job) LimitRunsTo(n int) {
// shouldRun evaluates if this job should run again
// based on the runConfig
func (j *Job) shouldRun() bool {
j.RLock()
defer j.RUnlock()
return !j.runConfig.finiteRuns || j.runCount < j.runConfig.maxRuns
}

// LastRun returns the time the job was run last
func (j *Job) LastRun() time.Time {
j.RLock()
defer j.RUnlock()
return j.lastRun
}

func (j *Job) setLastRun(t time.Time) {
j.Lock()
defer j.Unlock()
lastRun := j.lastRun
return lastRun
j.lastRun = t
}

// NextRun returns the time the job will run next
func (j *Job) NextRun() time.Time {
j.RLock()
defer j.RUnlock()
return j.nextRun
}

func (j *Job) setNextRun(t time.Time) {
j.Lock()
defer j.Unlock()
nextRun := j.nextRun
return nextRun
j.nextRun = t
}

// RunCount returns the number of time the job ran so far
func (j *Job) RunCount() int {
j.RLock()
defer j.RUnlock()
return j.runCount
}

func (j *Job) setRunCount(i int) {
j.Lock()
defer j.Unlock()
runCount := j.runCount
return runCount
j.runCount = i
}

// RemoveAfterLastRun update the job in order to remove the job after its last exec
func (j *Job) RemoveAfterLastRun() *Job {
j.Lock()
defer j.Unlock()
j.runConfig.removeAfterLastRun = true
return j
}

func (j *Job) getFiniteRuns() bool {
j.RLock()
defer j.RUnlock()
return j.runConfig.finiteRuns
}

func (j *Job) getMaxRuns() int {
j.RLock()
defer j.RUnlock()
return j.runConfig.maxRuns
}

func (j *Job) getRemoveAfterLastRun() bool {
j.RLock()
defer j.RUnlock()
return j.runConfig.removeAfterLastRun
}
Loading

0 comments on commit caab14a

Please sign in to comment.