Skip to content

Commit

Permalink
Merge pull request #20 from reugn/develop
Browse files Browse the repository at this point in the history
v0.3.8
  • Loading branch information
reugn authored Dec 3, 2021
2 parents 501ea0e + f5b4d00 commit 1c112df
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 115 deletions.
5 changes: 5 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
coverage:
status:
project:
default:
target: 80%
21 changes: 21 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Build

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [1.17.x]
steps:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Run coverage
run: go test ./... -coverprofile=coverage.out -covermode=atomic
- name: Upload coverage to Codecov
run: bash <(curl -s https://codecov.io/bash)
15 changes: 15 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: golangci-lint
on:
push:
branches:
- master
pull_request:

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
.vscode/
examples/examples
coverage.out
41 changes: 41 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
run:
skip-dirs:
- examples

linters:
disable-all: true
enable:
- deadcode
- dupl
- errcheck
- exportloopref
- funlen
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- gosimple
- govet
- ineffassign
- lll
- misspell
- prealloc
- revive
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck

issues:
exclude-rules:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- errcheck
- unparam
- prealloc
16 changes: 0 additions & 16 deletions .travis.yml

This file was deleted.

18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# go-quartz
[![Build Status](https://travis-ci.org/reugn/go-quartz.svg?branch=master)](https://travis-ci.org/reugn/go-quartz)
[![Build](https://github.com/reugn/go-quartz/actions/workflows/build.yml/badge.svg)](https://github.com/reugn/go-quartz/actions/workflows/build.yml)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/reugn/go-quartz)](https://pkg.go.dev/github.com/reugn/go-quartz)
[![Go Report Card](https://goreportcard.com/badge/github.com/reugn/go-quartz)](https://goreportcard.com/report/github.com/reugn/go-quartz)
[![codecov](https://codecov.io/gh/reugn/go-quartz/branch/master/graph/badge.svg)](https://codecov.io/gh/reugn/go-quartz)
Expand All @@ -25,21 +25,21 @@ Implemented Jobs
Scheduler interface
```go
type Scheduler interface {
// start the scheduler
// Start starts the scheduler.
Start()
// whether the scheduler has been started
// IsStarted determines whether the scheduler has been started.
IsStarted() bool
// schedule the job with the specified trigger
// ScheduleJob schedules a job using a specified trigger.
ScheduleJob(job Job, trigger Trigger) error
// get all scheduled jobs keys
// GetJobKeys returns the keys of all of the scheduled jobs.
GetJobKeys() []int
// get the scheduled job metadata
// GetScheduledJob returns the scheduled job with the specified key.
GetScheduledJob(key int) (*ScheduledJob, error)
// remove the job from the execution queue
// DeleteJob removes the job with the specified key from the Scheduler's execution queue.
DeleteJob(key int) error
// clear all scheduled jobs
// Clear removes all of the scheduled jobs.
Clear()
// shutdown the scheduler
// Stop shutdowns the scheduler.
Stop()
}
```
Expand Down
36 changes: 26 additions & 10 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@ import (
"github.com/reugn/go-quartz/quartz"
)

//demo main
func main() {
wg := new(sync.WaitGroup)
wg.Add(2)

go demoJobs(wg)
go demoScheduler(wg)
go sampleJobs(wg)
go sampleScheduler(wg)

wg.Wait()
}

func demoScheduler(wg *sync.WaitGroup) {
func sampleScheduler(wg *sync.WaitGroup) {
sched := quartz.NewStdScheduler()
cronTrigger, _ := quartz.NewCronTrigger("1/3 * * * * *")
cronTrigger, err := quartz.NewCronTrigger("1/3 * * * * *")
if err != nil {
fmt.Println(err)
return
}

cronJob := PrintJob{"Cron job"}
sched.Start()
sched.ScheduleJob(&PrintJob{"Ad hoc Job"}, quartz.NewRunOnceTrigger(time.Second*5))
Expand All @@ -33,8 +37,13 @@ func demoScheduler(wg *sync.WaitGroup) {

time.Sleep(time.Second * 10)

j, _ := sched.GetScheduledJob(cronJob.Key())
fmt.Println(j.TriggerDescription)
scheduledJob, err := sched.GetScheduledJob(cronJob.Key())
if err != nil {
fmt.Println(err)
return
}

fmt.Println(scheduledJob.TriggerDescription)
fmt.Println("Before delete: ", sched.GetJobKeys())
sched.DeleteJob(cronJob.Key())
fmt.Println("After delete: ", sched.GetJobKeys())
Expand All @@ -44,16 +53,23 @@ func demoScheduler(wg *sync.WaitGroup) {
wg.Done()
}

func demoJobs(wg *sync.WaitGroup) {
func sampleJobs(wg *sync.WaitGroup) {
sched := quartz.NewStdScheduler()
sched.Start()
cronTrigger, _ := quartz.NewCronTrigger("1/5 * * * * *")

cronTrigger, err := quartz.NewCronTrigger("1/5 * * * * *")
if err != nil {
fmt.Println(err)
return
}

shellJob := quartz.NewShellJob("ls -la")
curlJob, err := quartz.NewCurlJob(http.MethodGet, "http://worldclockapi.com/api/json/est/now", "", nil)
if err != nil {
fmt.Println(err.Error())
fmt.Println(err)
return
}

sched.ScheduleJob(shellJob, cronTrigger)
sched.ScheduleJob(curlJob, quartz.NewSimpleTrigger(time.Second*7))

Expand Down
12 changes: 6 additions & 6 deletions examples/print_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ type PrintJob struct {
desc string
}

// Description returns a PrintJob description.
func (pj PrintJob) Description() string {
// Description returns the description of the PrintJob.
func (pj *PrintJob) Description() string {
return pj.desc
}

// Key returns a PrintJob unique key.
func (pj PrintJob) Key() int {
// Key returns the unique PrintJob key.
func (pj *PrintJob) Key() int {
return quartz.HashCode(pj.Description())
}

// Execute Called by the Scheduler when a Trigger fires that is associated with the Job.
func (pj PrintJob) Execute() {
// Execute is called by a Scheduler when the Trigger associated with this job fires.
func (pj *PrintJob) Execute() {
fmt.Println("Executing " + pj.Description())
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module github.com/reugn/go-quartz

go 1.13
25 changes: 11 additions & 14 deletions quartz/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (ct *CronTrigger) NextFireTime(prev int64) (int64, error) {
return parser.nextTime(prev, ct.fields)
}

// Description returns a CronTrigger description.
// Description returns the CronTrigger description.
func (ct *CronTrigger) Description() string {
return fmt.Sprintf("CronTrigger %s", ct.expression)
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func (parser *CronExpressionParser) nextTime(prev int64, fields []*CronField) (n
case error:
err = x
default:
err = errors.New("Unknown cron expression error")
err = errors.New("unknown cron expression error")
}
}
}()
Expand Down Expand Up @@ -358,10 +358,7 @@ func (parser *CronExpressionParser) setDone(index int) {
}

func (parser *CronExpressionParser) lastSet(index int) bool {
if parser.lastDefined <= index {
return true
}
return false
return parser.lastDefined <= index
}

func (parser *CronExpressionParser) nextSeconds(prev int, field *CronField) string {
Expand Down Expand Up @@ -482,18 +479,18 @@ func bumpLiteral(iprev int, max int, step int) (int, bool) {

// returns bumped value, bump next
func bumpValue(prev interface{}, max, step int, zero bool) (int, bool) {
var iprev, bumped int
var iprev int

switch prev.(type) {
switch prevValue := prev.(type) {
case string:
iprev, _ = strconv.Atoi(prev.(string))
iprev = atoi(prevValue)
case int:
iprev = prev.(int)
iprev = prevValue
default:
panic("Unknown type at bumpValue")
}

bumped = iprev + step
bumped := iprev + step
if bumped > max {
if zero {
return 0, true
Expand All @@ -508,11 +505,11 @@ func bumpValue(prev interface{}, max, step int, zero bool) (int, bool) {
func (parser *CronExpressionParser) findNextValue(prev interface{}, values []int) (int, bool) {
var iprev int

switch prev.(type) {
switch prevValue := prev.(type) {
case string:
iprev, _ = strconv.Atoi(prev.(string))
iprev = atoi(prevValue)
case int:
iprev = prev.(int)
iprev = prevValue
default:
panic("Unknown type at findNextValue")
}
Expand Down
Loading

0 comments on commit 1c112df

Please sign in to comment.