Skip to content

Commit f2138d6

Browse files
harryzcydelvh
andauthored
Replace gogs/cron with go-co-op/gocron (#25977)
Replace `github.com/gogs/cron` with `github.com/go-co-op/gocron` as the former package is not maintained for many years. --------- Co-authored-by: delvh <[email protected]>
1 parent f3d41c6 commit f2138d6

File tree

6 files changed

+123
-22
lines changed

6 files changed

+123
-22
lines changed

assets/go-licenses.json

Lines changed: 10 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ require (
4141
github.com/go-ap/jsonld v0.0.0-20221030091449-f2a191312c73
4242
github.com/go-chi/chi/v5 v5.0.8
4343
github.com/go-chi/cors v1.2.1
44+
github.com/go-co-op/gocron v1.30.1
4445
github.com/go-enry/go-enry/v2 v2.8.4
4546
github.com/go-fed/httpsig v1.1.1-0.20201223112313-55836744818e
4647
github.com/go-git/go-billy/v5 v5.4.1
@@ -52,7 +53,6 @@ require (
5253
github.com/go-webauthn/webauthn v0.8.6
5354
github.com/gobwas/glob v0.2.3
5455
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f
55-
github.com/gogs/cron v0.0.0-20171120032916-9f6c956d3e14
5656
github.com/gogs/go-gogs-client v0.0.0-20210131175652-1d7215cd8d85
5757
github.com/golang-jwt/jwt/v5 v5.0.0
5858
github.com/google/go-github/v53 v53.2.0
@@ -253,6 +253,7 @@ require (
253253
github.com/rhysd/actionlint v1.6.25 // indirect
254254
github.com/rivo/uniseg v0.4.4 // indirect
255255
github.com/robfig/cron v1.2.0 // indirect
256+
github.com/robfig/cron/v3 v3.0.1 // indirect
256257
github.com/rogpeppe/go-internal v1.11.0 // indirect
257258
github.com/rs/xid v1.5.0 // indirect
258259
github.com/russross/blackfriday/v2 v2.1.0 // indirect

go.sum

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
371371
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
372372
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
373373
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
374+
github.com/go-co-op/gocron v1.30.1 h1:tjWUvJl5KrcwpkEkSXFSQFr4F9h5SfV/m4+RX0cV2fs=
375+
github.com/go-co-op/gocron v1.30.1/go.mod h1:39f6KNSGVOU1LO/ZOoZfcSxwlsJDQOKSu8erN0SH48Y=
374376
github.com/go-enry/go-enry/v2 v2.8.4 h1:QrY3hx/RiqCJJRbdU0MOcjfTM1a586J0WSooqdlJIhs=
375377
github.com/go-enry/go-enry/v2 v2.8.4/go.mod h1:9yrj4ES1YrbNb1Wb7/PWYr2bpaCXUGRt0uafN0ISyG8=
376378
github.com/go-enry/go-oniguruma v1.2.1 h1:k8aAMuJfMrqm/56SG2lV9Cfti6tC4x8673aHCcBk+eo=
@@ -496,8 +498,6 @@ github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zV
496498
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
497499
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f h1:3BSP1Tbs2djlpprl7wCLuiqMaUh5SJkkzI2gDs+FgLs=
498500
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f/go.mod h1:Pcatq5tYkCW2Q6yrR2VRHlbHpZ/R4/7qyL1TCF7vl14=
499-
github.com/gogs/cron v0.0.0-20171120032916-9f6c956d3e14 h1:yXtpJr/LV6PFu4nTLgfjQdcMdzjbqqXMEnHfq0Or6p8=
500-
github.com/gogs/cron v0.0.0-20171120032916-9f6c956d3e14/go.mod h1:jPoNZLWDAqA5N3G5amEoiNbhVrmM+ZQEcnQvNQ2KaZk=
501501
github.com/gogs/go-gogs-client v0.0.0-20210131175652-1d7215cd8d85 h1:UjoPNDAQ5JPCjlxoJd6K8ALZqSDDhk2ymieAZOVaDg0=
502502
github.com/gogs/go-gogs-client v0.0.0-20210131175652-1d7215cd8d85/go.mod h1:fR6z1Ie6rtF7kl/vBYMfgD5/G5B1blui7z426/sj2DU=
503503
github.com/golang-jwt/jwt/v4 v4.2.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
@@ -787,6 +787,7 @@ github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
787787
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
788788
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
789789
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
790+
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
790791
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
791792
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
792793
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -1038,10 +1039,14 @@ github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUc
10381039
github.com/robertkrimen/godocdown v0.0.0-20130622164427-0bfa04905481/go.mod h1:C9WhFzY47SzYBIvzFqSvHIR6ROgDo4TtdTuRaOMjF/s=
10391040
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
10401041
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
1042+
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
1043+
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
10411044
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
10421045
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
10431046
github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
10441047
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
1048+
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
1049+
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
10451050
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
10461051
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
10471052
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
@@ -1250,6 +1255,7 @@ go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
12501255
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
12511256
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
12521257
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
1258+
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
12531259
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
12541260
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
12551261
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=

services/cron/cron.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import (
1414
"code.gitea.io/gitea/modules/sync"
1515
"code.gitea.io/gitea/modules/translation"
1616

17-
"github.com/gogs/cron"
17+
"github.com/go-co-op/gocron"
1818
)
1919

20-
var c = cron.New()
20+
var scheduler = gocron.NewScheduler(time.Local)
2121

2222
// Prevent duplicate running tasks.
2323
var taskStatusTable = sync.NewStatusTable()
@@ -39,11 +39,11 @@ func NewContext(original context.Context) {
3939
}
4040
}
4141

42-
c.Start()
42+
scheduler.StartAsync()
4343
started = true
4444
lock.Unlock()
4545
graceful.GetManager().RunAtShutdown(context.Background(), func() {
46-
c.Stop()
46+
scheduler.Stop()
4747
lock.Lock()
4848
started = false
4949
lock.Unlock()
@@ -77,24 +77,34 @@ type TaskTable []*TaskTableRow
7777

7878
// ListTasks returns all running cron tasks.
7979
func ListTasks() TaskTable {
80-
entries := c.Entries()
81-
eMap := map[string]*cron.Entry{}
82-
for _, e := range entries {
83-
eMap[e.Description] = e
80+
jobs := scheduler.Jobs()
81+
jobMap := map[string]*gocron.Job{}
82+
for _, job := range jobs {
83+
// the first tag is the task name
84+
tags := job.Tags()
85+
if len(tags) == 0 { // should never happen
86+
continue
87+
}
88+
jobMap[job.Tags()[0]] = job
8489
}
90+
8591
lock.Lock()
8692
defer lock.Unlock()
93+
8794
tTable := make([]*TaskTableRow, 0, len(tasks))
8895
for _, task := range tasks {
8996
spec := "-"
9097
var (
9198
next time.Time
9299
prev time.Time
93100
)
94-
if e, ok := eMap[task.Name]; ok {
95-
spec = e.Spec
96-
next = e.Next
97-
prev = e.Prev
101+
if e, ok := jobMap[task.Name]; ok {
102+
tags := e.Tags()
103+
if len(tags) > 1 {
104+
spec = tags[1] // the second tag is the task spec
105+
}
106+
next = e.NextRun()
107+
prev = e.PreviousRun()
98108
}
99109
task.lock.Lock()
100110
tTable = append(tTable, &TaskTableRow{

services/cron/tasks.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"fmt"
99
"reflect"
10+
"strings"
1011
"sync"
1112

1213
"code.gitea.io/gitea/models/db"
@@ -176,8 +177,7 @@ func RegisterTask(name string, config Config, fun func(context.Context, *user_mo
176177

177178
if config.IsEnabled() {
178179
// We cannot use the entry return as there is no way to lock it
179-
if _, err = c.AddJob(name, config.GetSchedule(), task); err != nil {
180-
log.Error("Unable to register cron task with name: %s Error: %v", name, err)
180+
if err := addTaskToScheduler(task); err != nil {
181181
return err
182182
}
183183
}
@@ -199,3 +199,21 @@ func RegisterTaskFatal(name string, config Config, fun func(context.Context, *us
199199
log.Fatal("Unable to register cron task %s Error: %v", name, err)
200200
}
201201
}
202+
203+
func addTaskToScheduler(task *Task) error {
204+
tags := []string{task.Name, task.config.GetSchedule()} // name and schedule can't be get from job, so we add them as tag
205+
if scheduleHasSeconds(task.config.GetSchedule()) {
206+
scheduler = scheduler.CronWithSeconds(task.config.GetSchedule())
207+
} else {
208+
scheduler = scheduler.Cron(task.config.GetSchedule())
209+
}
210+
if _, err := scheduler.Tag(tags...).Do(task.Run); err != nil {
211+
log.Error("Unable to register cron task with name: %s Error: %v", task.Name, err)
212+
return err
213+
}
214+
return nil
215+
}
216+
217+
func scheduleHasSeconds(schedule string) bool {
218+
return len(strings.Fields(schedule)) >= 6
219+
}

services/cron/tasks_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package cron
5+
6+
import (
7+
"strconv"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestAddTaskToScheduler(t *testing.T) {
14+
assert.Len(t, scheduler.Jobs(), 0)
15+
defer scheduler.Clear()
16+
17+
// no seconds
18+
err := addTaskToScheduler(&Task{
19+
Name: "task 1",
20+
config: &BaseConfig{
21+
Schedule: "5 4 * * *",
22+
},
23+
})
24+
assert.NoError(t, err)
25+
assert.Len(t, scheduler.Jobs(), 1)
26+
assert.Equal(t, "task 1", scheduler.Jobs()[0].Tags()[0])
27+
assert.Equal(t, "5 4 * * *", scheduler.Jobs()[0].Tags()[1])
28+
29+
// with seconds
30+
err = addTaskToScheduler(&Task{
31+
Name: "task 2",
32+
config: &BaseConfig{
33+
Schedule: "30 5 4 * * *",
34+
},
35+
})
36+
assert.NoError(t, err)
37+
assert.Len(t, scheduler.Jobs(), 2)
38+
assert.Equal(t, "task 2", scheduler.Jobs()[1].Tags()[0])
39+
assert.Equal(t, "30 5 4 * * *", scheduler.Jobs()[1].Tags()[1])
40+
}
41+
42+
func TestScheduleHasSeconds(t *testing.T) {
43+
tests := []struct {
44+
schedule string
45+
hasSecond bool
46+
}{
47+
{"* * * * * *", true},
48+
{"* * * * *", false},
49+
{"5 4 * * *", false},
50+
{"5 4 * * *", false},
51+
{"5,8 4 * * *", false},
52+
{"* * * * * *", true},
53+
{"5,8 4 * * *", false},
54+
}
55+
56+
for i, test := range tests {
57+
t.Run(strconv.Itoa(i), func(t *testing.T) {
58+
assert.Equal(t, test.hasSecond, scheduleHasSeconds(test.schedule))
59+
})
60+
}
61+
}

0 commit comments

Comments
 (0)