pocketbase/tools/cron/job.go

42 lines
773 B
Go
Raw Permalink Normal View History

2024-12-22 22:05:38 +08:00
package cron
2024-12-26 04:24:24 +08:00
import "encoding/json"
2024-12-22 22:05:38 +08:00
// Job defines a single registered cron job.
type Job struct {
fn func()
schedule *Schedule
id string
}
// Id returns the cron job id.
func (j *Job) Id() string {
return j.id
}
2024-12-26 04:24:24 +08:00
// Expression returns the plain cron job schedule expression.
func (j *Job) Expression() string {
2024-12-22 22:05:38 +08:00
return j.schedule.rawExpr
}
// Run runs the cron job function.
func (j *Job) Run() {
if j.fn != nil {
j.fn()
}
}
2024-12-26 04:24:24 +08:00
// MarshalJSON implements [json.Marshaler] and export the current
// jobs data into valid JSON.
func (j Job) MarshalJSON() ([]byte, error) {
plain := struct {
Id string `json:"id"`
Expression string `json:"expression"`
}{
Id: j.Id(),
Expression: j.Expression(),
}
return json.Marshal(plain)
}