Skip to content

Commit 768c99e

Browse files
committed
features done,tidy code
1 parent f70ddda commit 768c99e

18 files changed

+1279
-2
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Describe the bug**
11+
A clear and concise description of what the bug is.
12+
13+
**To Reproduce**
14+
Steps to reproduce the behavior:
15+
1. Go to '...'
16+
2. Click on '....'
17+
3. Scroll down to '....'
18+
4. See error
19+
20+
**Expected behavior**
21+
A clear and concise description of what you expected to happen.
22+
23+
**Screenshots**
24+
If applicable, add screenshots to help explain your problem.
25+
26+
**Desktop (please complete the following information):**
27+
- OS: [e.g. MacOS]
28+
- Version [e.g. 14.2.1]
29+
30+
**Go (please complete the following information):**
31+
- Version [e.g. 22]
32+
33+
**Additional context**
34+
Add any other context about the problem here.
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.
15+
16+
**Describe alternatives you've considered**
17+
A clear and concise description of any alternative solutions or features you've considered.
18+
19+
**Additional context**
20+
Add any other context or screenshots about the feature request here.

.github/workflows/codeql.yml

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# For most projects, this workflow file will not need changing; you simply need
2+
# to commit it to your repository.
3+
#
4+
# You may wish to alter this file to override the set of languages analyzed,
5+
# or to provide custom queries or build logic.
6+
#
7+
# ******** NOTE ********
8+
# We have attempted to detect the languages in your repository. Please check
9+
# the `language` matrix defined below to confirm you have the correct set of
10+
# supported CodeQL languages.
11+
#
12+
name: "CodeQL"
13+
14+
on:
15+
push:
16+
branches: [ main ]
17+
pull_request:
18+
# The branches below must be a subset of the branches above
19+
branches: [ main ]
20+
schedule:
21+
- cron: '35 0 * * 1'
22+
23+
jobs:
24+
analyze:
25+
name: Analyze
26+
runs-on: ubuntu-latest
27+
permissions:
28+
actions: read
29+
contents: read
30+
security-events: write
31+
32+
strategy:
33+
fail-fast: false
34+
matrix:
35+
language: [ 'go' ]
36+
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
37+
# Learn more about CodeQL language support at https://git.io/codeql-language-support
38+
39+
steps:
40+
- name: Checkout repository
41+
uses: actions/checkout@v2
42+
43+
# Initializes the CodeQL tools for scanning.
44+
- name: Initialize CodeQL
45+
uses: github/codeql-action/init@v2
46+
with:
47+
languages: ${{ matrix.language }}
48+
# If you wish to specify custom queries, you can do so here or in a config file.
49+
# By default, queries listed here will override any specified in a config file.
50+
# Prefix the list here with "+" to use these queries and those in the config file.
51+
# queries: ./path/to/local/query, your-org/your-repo/queries@main
52+
53+
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
54+
# If this step fails, then you should remove it and run the build manually (see below)
55+
- name: Autobuild
56+
uses: github/codeql-action/autobuild@v2
57+
58+
# ℹ️ Command-line programs to run using the OS shell.
59+
# 📚 https://git.io/JvXDl
60+
61+
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
62+
# and modify them (or add more) to build your code if your project
63+
# uses a compiled language
64+
65+
#- run: |
66+
# make bootstrap
67+
# make release
68+
69+
- name: Perform CodeQL Analysis
70+
uses: github/codeql-action/analyze@v2

.github/workflows/go.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v2
15+
16+
- name: Set up Go
17+
uses: actions/setup-go@v2
18+
with:
19+
go-version: 1.19
20+
21+
- name: Build
22+
run: go build -v ./...
23+
24+
- name: Test
25+
run: go test -v ./...

.github/workflows/goreport.yml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# For most projects, this workflow file will not need changing; you simply need
2+
# to commit it to your repository.
3+
#
4+
# You may wish to alter this file to override the set of languages analyzed,
5+
# or to provide custom queries or build logic.
6+
#
7+
# ******** NOTE ********
8+
# We have attempted to detect the languages in your repository. Please check
9+
# the `language` matrix defined below to confirm you have the correct set of
10+
# supported CodeQL languages.
11+
#
12+
name: "Go Report"
13+
14+
on:
15+
push:
16+
branches: [ main ]
17+
pull_request:
18+
# The branches below must be a subset of the branches above
19+
branches: [ main ]
20+
schedule:
21+
- cron: '31 4 * * 6'
22+
23+
jobs:
24+
analyze:
25+
name: Analyze
26+
runs-on: ubuntu-latest
27+
permissions:
28+
actions: read
29+
contents: read
30+
security-events: write
31+
32+
strategy:
33+
fail-fast: false
34+
matrix:
35+
language: [ 'go' ]
36+
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
37+
# Learn more about CodeQL language support at https://git.io/codeql-language-support
38+
39+
steps:
40+
- name: Perform go report
41+
uses: creekorful/[email protected]

.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/schedule.iml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+48-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,48 @@
1-
# schedule
2-
A distrubited scheduled task tool written in Go.
1+
# Schedule
2+
[![Go](https://github.com/keepchen/schedule/actions/workflows/go.yml/badge.svg)](https://github.com/keepchen/schedule/actions/workflows/go.yml) [![CodeQL](https://github.com/keepchen/schedule/actions/workflows/codeql.yml/badge.svg)](https://github.com/keepchen/schedule/actions/workflows/codeql.yml) [![Go Report Card](https://goreportcard.com/badge/github.com/keepchen/schedule/v3)](https://goreportcard.com/report/github.com/keepchen/go-sail/v3)
3+
A distributed scheduled task tool written in Go. It was incubated and evolved from the [go-sail](https://github.com/keepchen/go-sail) framework.
4+
5+
### Requirement
6+
```text
7+
go version >= 1.19
8+
```
9+
10+
### Installation
11+
```shell
12+
go get -u github.com/keepchen/schedule
13+
```
14+
15+
### Features
16+
- [x] Interval task
17+
- [x] Once time task
18+
- [x] Linux Crontab Style task
19+
- [x] Cancelable
20+
- [x] Race detection
21+
22+
### Examples
23+
#### Interval
24+
```go
25+
schedule.NewJob("say hello").EveryMinute()
26+
```
27+
#### Once time
28+
```go
29+
schedule.NewJob("check in").RunAfter(time.Second)
30+
```
31+
#### Linux Crontab Style
32+
```go
33+
schedule.NewJob("good morning").RunAt(schedule.EveryDayOfEightAMClock)
34+
```
35+
#### Cancelable
36+
```go
37+
cancel := schedule.NewJob("say hello").EveryMinute()
38+
39+
time.AfterFunc(time.Minute*3, cancel)
40+
```
41+
#### Race detection
42+
> Note: You must set redis provider before use.
43+
```go
44+
// set redis provider
45+
schedule.SetRedisProviderStandalone(...)
46+
47+
schedule.NewJob("say hello").WithoutOverlapping().EveryMinute()
48+
```

crontabexpr.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package schedule
2+
3+
// 一些常用的crontab表达式
4+
const (
5+
EveryMinute = "* * * * *" //每分钟的开始第0秒
6+
EveryFiveMinute = "*/5 * * * *" //每5分钟的开始第0秒
7+
EveryTenMinute = "*/10 * * * *" //每10分钟的开始第0秒
8+
EveryFifteenMinute = "*/15 * * * *" //每15分钟的开始第0秒
9+
EveryTwentyMinute = "*/20 * * * *" //每20分钟的开始第0秒
10+
EveryThirtyMinute = "*/30 * * * *" //每30分钟的开始第0秒
11+
EveryFortyFiveMinute = "*/45 * * * *" //每45分钟的开始第0秒
12+
EveryDayOfEightAMClock = "0 8 * * *" //每天早上八点
13+
FirstDayOfMonth = "0 0 1 * *" //每月的第一天的0点0分
14+
LastDayOfMonth = "0 0 L * *" //每月的最后一天的0点0分
15+
FirstDayOfWeek = "0 0 * * 1" //每周的第一天(周一)的0点0分
16+
LastDayOfWeek = "0 0 * * 7" //每周的最后一天(周天)的0点0分
17+
TenClockAtWeekday = "0 10 * * MON-FRI" //每个工作日(周一~周五)的上午10点0分
18+
TenClockAtWeekend = "0 10 * * SAT,SUN" //每个周末(周六和周日)的上午10点0分
19+
HourlyBetween9And17ClockAtWeekday = "0 9-17 * * MON-FRI" //每个工作日(周一~周五)的上午9点0分到下午5点0分每小时一次
20+
HalfHourlyBetween9And17ClockAtWeekday = "*/30 9-17 * * MON-FRI" //每个工作日(周一~周五)的上午9点0分到下午5点0分每半时一次
21+
)

delay.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package schedule
2+
3+
import (
4+
"time"
5+
)
6+
7+
// RunOnceTimeAfter 在一定时间后执行
8+
//
9+
// # Note
10+
//
11+
// 这是一个一次性任务,只会执行一次,不会重复执行
12+
func (j *taskJob) RunOnceTimeAfter(delay time.Duration) (cancel CancelFunc) {
13+
timer := time.After(delay)
14+
cancel = j.cancelFunc
15+
16+
j.wrappedTaskFunc = func() {
17+
j.running = true
18+
19+
defer func() {
20+
j.running = false
21+
}()
22+
23+
if !j.withoutOverlapping {
24+
j.task()
25+
return
26+
}
27+
if lockDriver.tryLock(j.lockerKey) {
28+
defer func() {
29+
lockDriver.unlock(j.lockerKey)
30+
j.lockedByMe = false
31+
}()
32+
j.lockedByMe = true
33+
j.task()
34+
}
35+
}
36+
37+
go func() {
38+
LOOP:
39+
for {
40+
select {
41+
case <-timer:
42+
go j.wrappedTaskFunc()
43+
break LOOP
44+
case <-j.cancelTaskChan:
45+
break LOOP
46+
}
47+
}
48+
}()
49+
50+
return cancel
51+
}

go.mod

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module github.com/keepchen/schedule
2+
3+
go 1.19
4+
5+
require (
6+
github.com/go-redis/redis/v8 v8.11.5
7+
github.com/robfig/cron/v3 v3.0.1
8+
)
9+
10+
require (
11+
github.com/cespare/xxhash/v2 v2.2.0 // indirect
12+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
13+
golang.org/x/net v0.17.0 // indirect
14+
golang.org/x/sys v0.17.0 // indirect
15+
golang.org/x/text v0.14.0 // indirect
16+
)

go.sum

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
2+
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
3+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
4+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
5+
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
6+
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
7+
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
8+
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
9+
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
10+
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
11+
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
12+
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
13+
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
14+
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
15+
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
16+
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
17+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
18+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
19+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
20+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=

0 commit comments

Comments
 (0)