This commit is contained in:
rogeecn
2025-03-11 09:56:22 +08:00
parent 94dd15b4e9
commit de2ddef3af
4 changed files with 42 additions and 19 deletions

View File

@@ -11,8 +11,11 @@ import (
"github.com/pkg/errors"
"github.com/riverqueue/river"
"github.com/riverqueue/river/riverdriver/riverpgxv5"
"github.com/riverqueue/river/rivertype"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
"go.ipao.vip/atom/container"
"go.ipao.vip/atom/contracts"
"go.ipao.vip/atom/opt"
)
@@ -109,3 +112,20 @@ func (q *Job) StopAndCancel(ctx context.Context) error {
return client.StopAndCancel(ctx)
}
func (q *Job) AddPeriodicJobs(job contracts.CronJob) (rivertype.PeriodicJobHandle, error) {
client, err := q.Client()
if err != nil {
return 0, err
}
return client.PeriodicJobs().Add(river.NewPeriodicJob(
job.Periodic(),
func() (river.JobArgs, *river.InsertOpts) {
return job.JobArgs(), lo.ToPtr(job.InsertOpts())
},
&river.PeriodicJobOpts{
RunOnStart: job.RunOnStart(),
},
)), nil
}