Skip to content

Commit acf1269

Browse files
committed
fix: support cancel cronjob
1 parent f2a2831 commit acf1269

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

internal/biz/bizchesed/chesed.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func NewChesed(
7373
modelbinah.DownloadEmpty,
7474
nil,
7575
)
76-
err := cron.BySeconds("ChesedScanImage", 60, c.ScanImage, context.Background()) //nolint:mnd //TODO
76+
_, err := cron.NewJobBySeconds("ChesedScanImage", 60, c.ScanImage, context.Background()) //nolint:mnd //TODO
7777
if err != nil {
7878
return nil, err
7979
}

internal/biz/bizyesod/yesod.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ func NewYesod(
6262
feedOwner: feedOwner,
6363
builtinFeedActions: builtinFeedActions,
6464
}
65-
err = cron.BySeconds("YesodPullFeeds", 60, y.PullFeeds, context.Background()) //nolint:mnd // hard code min interval
65+
_, err = cron.NewJobBySeconds(
66+
"YesodPullFeeds",
67+
60, //nolint:mnd // hard code min interval
68+
y.PullFeeds,
69+
context.Background(),
70+
)
6671
if err != nil {
6772
return nil, err
6873
}

internal/lib/libcron/cron.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,32 @@ func (c *Cron) Stop(ctx context.Context) error {
3838
return c.scheduler.StopJobs()
3939
}
4040

41-
func (c *Cron) BySeconds(name string, seconds int, jobFunc interface{}, params ...interface{}) error {
42-
return c.Duration(name, time.Duration(seconds)*time.Second, jobFunc, params...)
41+
func (c *Cron) NewJobBySeconds(name string, seconds int, jobFunc interface{}, params ...interface{}) (*Job, error) {
42+
return c.NewJobByDuration(name, time.Duration(seconds)*time.Second, jobFunc, params...)
4343
}
4444

45-
func (c *Cron) Duration(name string, duration time.Duration, jobFunc interface{}, params ...interface{}) error {
46-
_, err := c.scheduler.NewJob(
45+
func (c *Cron) NewJobByDuration(
46+
name string,
47+
duration time.Duration,
48+
jobFunc interface{},
49+
params ...interface{},
50+
) (*Job, error) {
51+
job, err := c.scheduler.NewJob(
4752
gocron.DurationJob(duration),
4853
gocron.NewTask(jobFunc, params...),
4954
gocron.WithName(name),
5055
)
51-
return err
56+
return &Job{
57+
cron: c,
58+
job: job,
59+
}, err
60+
}
61+
62+
type Job struct {
63+
cron *Cron
64+
job gocron.Job
65+
}
66+
67+
func (c *Job) Cancel() error {
68+
return c.cron.scheduler.RemoveJob(c.job.ID())
5269
}

internal/service/supervisor/supervisor.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var ProviderSet = wire.NewSet(NewSupervisorService)
3737
type SupervisorService struct {
3838
s *bizsupervisor.Supervisor
3939
cron *libcron.Cron
40+
job *libcron.Job
4041
porter *client.Porter
4142
t *data.TipherethRepo
4243
auth *libauth.Auth
@@ -65,6 +66,7 @@ func NewSupervisorService(
6566
res := SupervisorService{
6667
s: s,
6768
cron: cron,
69+
job: nil,
6870
porter: porter,
6971
t: t,
7072
auth: auth,
@@ -80,7 +82,7 @@ func NewSupervisorService(
8082
}
8183

8284
func (s *SupervisorService) Start(ctx context.Context) error {
83-
err := s.cron.Duration(
85+
job, err := s.cron.NewJobByDuration(
8486
"SupervisorService Heartbeat",
8587
s.getHeartbeatInterval(),
8688
func() {
@@ -93,10 +95,17 @@ func (s *SupervisorService) Start(ctx context.Context) error {
9395
if err != nil {
9496
return fmt.Errorf("failed to register cron: %w", err)
9597
}
98+
s.job = job
9699
return nil
97100
}
98101

99102
func (s *SupervisorService) Stop(ctx context.Context) error {
103+
if s.job != nil {
104+
if err := s.job.Cancel(); err != nil {
105+
return fmt.Errorf("failed to stop cron job: %w", err)
106+
}
107+
s.job = nil
108+
}
100109
return nil
101110
}
102111

0 commit comments

Comments
 (0)