Skip to content

Commit

Permalink
docs: format annotation (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfyiamcool committed Aug 12, 2023
1 parent a2a4d71 commit 88a785b
Showing 1 changed file with 72 additions and 16 deletions.
88 changes: 72 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# 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)
Expand All @@ -7,71 +8,96 @@
A minimalistic and zero-dependency scheduling library for Go.

## About

Inspired by the [Quartz](https://github.com/quartz-scheduler/quartz) Java scheduler.

### Library building blocks
Scheduler interface

#### Scheduler interface

```go
type Scheduler interface {
// Start starts the scheduler. The scheduler will run until
// the Stop method is called or the context is canceled. Use
// the Wait method to block until all running jobs have completed.
Start(context.Context)

// IsStarted determines whether the scheduler has been started.
IsStarted() bool

// ScheduleJob schedules a job using a specified trigger.
ScheduleJob(ctx context.Context, job Job, trigger Trigger) error

// GetJobKeys returns the keys of all of the scheduled jobs.
GetJobKeys() []int

// GetScheduledJob returns the scheduled job with the specified key.
GetScheduledJob(key int) (*ScheduledJob, error)

// DeleteJob removes the job with the specified key from the Scheduler's execution queue.
DeleteJob(key int) error

// Clear removes all of the scheduled jobs.
Clear()

// Stop shutdowns the scheduler.
Stop()

// Wait blocks until the scheduler stops running and all jobs
// have returned. Wait will return when the context passed to
// it has expired. Until the context passed to start is
// cancelled or Stop is called directly.
Wait(context.Context)
}
```

Implemented Schedulers

- StdScheduler

Trigger interface
#### Trigger interface

```go
type Trigger interface {
// NextFireTime returns the next time at which the Trigger is scheduled to fire.
NextFireTime(prev int64) (int64, error)

// Description returns the description of the Trigger.
Description() string
}
```

Implemented Triggers

- CronTrigger
- SimpleTrigger
- RunOnceTrigger

Job interface. Any type that implements it can be scheduled.
#### Job interface

Any type that implements it can be scheduled.

```go
type Job interface {
// Execute is called by a Scheduler when the Trigger associated with this job fires.
Execute(context.Context)

// Description returns the description of the Job.
Description() string

// Key returns the unique key for the Job.
Key() int
}
```

Implemented Jobs

- ShellJob
- CurlJob
- FunctionJob

## Cron expression format

| Field Name | Mandatory | Allowed Values | Allowed Special Characters |
| ------------ | --------- | --------------- | -------------------------- |
| Seconds | YES | 0-59 | , - * / |
Expand All @@ -83,22 +109,52 @@ Implemented Jobs
| Year | NO | empty, 1970- | , - * / |

## Examples

```go
ctx := context.Background()
sched := quartz.NewStdScheduler()
sched.Start(ctx)
cronTrigger, _ := quartz.NewCronTrigger("1/5 * * * * *")
shellJob := quartz.NewShellJob("ls -la")
request, _ := http.NewRequest(http.MethodGet, "https://worldtimeapi.org/api/timezone/utc", nil)
curlJob, _ := quartz.NewCurlJob(request)
functionJob := quartz.NewFunctionJob(func(_ context.Context) (int, error) { return 42, nil })
sched.ScheduleJob(shellJob, cronTrigger)
sched.ScheduleJob(curlJob, quartz.NewSimpleTrigger(time.Second*7))
sched.ScheduleJob(functionJob, quartz.NewSimpleTrigger(time.Second*5))
sched.Stop()
sched.Wait(ctx)
package main

import (
"context"
"net/http"
"time"

"github.com/reugn/go-quartz/quartz"
)

func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// create scheduler
sched := quartz.NewStdScheduler()

// async start scheduler
sched.Start(ctx)

// create jobs
cronTrigger, _ := quartz.NewCronTrigger("1/5 * * * * *")
shellJob := quartz.NewShellJob("ls -la")

request, _ := http.NewRequest(http.MethodGet, "https://worldtimeapi.org/api/timezone/utc", nil)
curlJob, _ := quartz.NewCurlJob(request)

functionJob := quartz.NewFunctionJob(func(_ context.Context) (int, error) { return 42, nil })

// register jobs to scheduler
sched.ScheduleJob(ctx, shellJob, cronTrigger)
sched.ScheduleJob(ctx, curlJob, quartz.NewSimpleTrigger(time.Second*7))
sched.ScheduleJob(ctx, functionJob, quartz.NewSimpleTrigger(time.Second*5))

// stop scheduler
sched.Stop()

// wait for all workers to exit
sched.Wait(ctx)
}
```

More code samples can be found in the examples directory.

## License

Licensed under the MIT License.

0 comments on commit 88a785b

Please sign in to comment.