Documentation
¶
Overview ¶
Package taskhelper provides struct and methods to help to synchronize different async.Task together and to simply start the async.Task properly This package should mainly used through the package app and not directly by the developer.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func JoinAll ¶
JoinAll is waiting for context to be canceled. A task that is ended and should stop the whole application, must have called the master cancelFunc shared by every TaskRunner which will closed the master context.
Types ¶
type Helper ¶
type Helper interface {
fmt.Stringer
Start(ctx context.Context, cancelFunc context.CancelFunc) error
// Done returns the channel used to wait for the task job to be finalized
Done() <-chan struct{}
}
Helper is an interface that defines a wrapper of the task that would help to execute it. Even if this interface is public, you usually don't have to implement it yourself, you just have to implement a Task or a SimpleTask
func NewCron ¶
NewCron is returning a Helper that will execute the task according to the schedule. If an execution of the task fails, the error is logged, and the task is executed again at the next schedule. The time spent executing the task doesn't delay the next execution. If an execution lasts longer than the time left until the next schedule, the task is executed again right away. It returns an error if cronSchedule is invalid, or if it never matches any date (like "0 0 31 2 *"). cronSchedule is following the standard syntax described here: https://en.wikipedia.org/wiki/Cron. It also supports the predefined scheduling definitions:
@yearly (or @annually) | Run once a year, midnight, Jan. 1st | 0 0 1 1 * @monthly | Run once a month, midnight, first of month | 0 0 1 * * @weekly | Run once a week, midnight between Sat/Sun | 0 0 * * 0 @daily (or @midnight) | Run once a day, midnight | 0 0 * * * @hourly | Run once an hour, beginning of hour | 0 * * * * @every <duration> | Run at a fixed interval, like "@every 1h30m" (see time.ParseDuration)
We are directly relying on what the library https://pkg.go.dev/github.com/flc1125/go-cron/v4 (a fork of https://github.com/robfig/cron) is supporting with cron.ParseStandard.
Example ¶
package main
import (
"context"
"fmt"
"github.com/perses/common/async/taskhelper"
)
type cleanupTask struct{}
func (t *cleanupTask) String() string {
return "cleanup task"
}
func (t *cleanupTask) Execute(_ context.Context, _ context.CancelFunc) error {
return nil
}
func main() {
// run the task every day at 2am. A predefined schedule like "@daily" or "@every 1h30m" is also supported.
helper, err := taskhelper.NewCron(&cleanupTask{}, "0 2 * * *")
if err != nil {
fmt.Println("invalid schedule:", err)
return
}
fmt.Println(helper)
// the cron library only accepts 5 fields
_, err = taskhelper.NewCron(&cleanupTask{}, "0 0 0 1 1 *")
fmt.Println(err != nil)
}
Output: cleanup task true