Documentation ¶
Overview ¶
Package xysched supports to schedule future tasks with a simple syntax.
Example ¶
package main import ( "fmt" "time" "github.com/xybor-x/xysched" ) func main() { // Example 1: You can use the global scheduler throughout program without // creating a new one. var done = make(chan any) var future = xysched.NewTask(func() { fmt.Println("1. bar bar") close(done) }) xysched.Now() <- future <-done // Example 2: Scheduler can schedule one future After or At a time. done = make(chan any) future = xysched.NewTask(func() { fmt.Println("2. barfoo") close(done) }) xysched.After(time.Millisecond) <- future <-done }
Output: 1. bar bar 2. barfoo
Index ¶
- Variables
- func After(d time.Duration) chan<- future
- func At(next time.Time) chan<- future
- func Now() chan<- future
- type Cron
- func (c *Cron) Daily() *Cron
- func (c *Cron) Every(d time.Duration) *Cron
- func (c *Cron) Finish(f any, params ...any) *Task
- func (c *Cron) Hourly() *Cron
- func (c *Cron) Minutely() *Cron
- func (c *Cron) Once() *Cron
- func (c *Cron) Secondly() *Cron
- func (c *Cron) Stop()
- func (c *Cron) String() string
- func (c *Cron) Times(n uint) *Cron
- func (c *Cron) Twice() *Cron
- type Scheduler
- type Task
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
CallError = egen.NewClass("CallError")
)
Errors of package xysched.
Functions ¶
func After ¶
After creates a send-only channel. Sending a future to this channel will add it to scheduler after a duration. If d is negative, After will send the future to scheduler immediately.
NOTE: You should send ONLY ONE future to this channel because it is designed to handle one. If you try sending another, it will be blocked forever. To send other futures to scheduler, let call this method again.
Types ¶
type Cron ¶
type Cron struct { // cron struct is a wrapper of task Task // contains filtered or unexported fields }
Cron is a future which runs a task periodically.
Example ¶
package main import ( "fmt" "time" "github.com/xybor-x/xysched" ) func wait(c chan any, n int) { for i := 0; i < n; i++ { <-c } } func main() { var scheduler = xysched.NewScheduler("") // Example 1: Cron is a future which runs function periodically. By default, // it runs secondly forever. var done = make(chan any) var future = xysched.NewCron(func(a ...any) { fmt.Println(a...) done <- nil }, "1.", "foo", "bar") scheduler.Now() <- future wait(done, 2) scheduler.Stop() scheduler = xysched.NewScheduler("") defer scheduler.Stop() // Example 2: It can modify periodic duration and the maximum times the // function could run. done = make(chan any) future = xysched.NewCron(func() { fmt.Println("2. bar bar") done <- nil }).Every(time.Millisecond).Twice() scheduler.Now() <- future wait(done, 2) // Example 3: Callback, Then, Catch can also be used on cron. done = make(chan any) future = xysched.NewCron(fmt.Println, "3.", "foobar").Times(3) future.Every(time.Millisecond) future.Callback(func() { done <- nil }) scheduler.Now() <- future wait(done, 3) // Example 4: Finish adds a callback future which will be run when cron ran // out of times. done = make(chan any) future = xysched.NewCron(fmt.Println, "4.", "foobar").Twice() future.Every(time.Millisecond) future.Finish(func() { close(done) }) scheduler.Now() <- future wait(done, 1) }
Output: 1. foo bar 1. foo bar 2. bar bar 2. bar bar 3. foobar 3. foobar 3. foobar 4. foobar 4. foobar
func NewCron ¶
NewCron creates a future which calls function f with parameter params periodically. By default, it runs the function forever secondly.
func (*Cron) Finish ¶
Finish sets a callback future which will run after the cron ran out of times. See task.Callback for further details.
func (*Cron) Stop ¶
func (c *Cron) Stop()
Stop early stops the Cron. If the Cron hasn't been scheduled yet, it will be never scheduled. If the Cron was scheduled, next runs will be canceled.
type Scheduler ¶
type Scheduler struct {
// contains filtered or unexported fields
}
Scheduler is used for scheduling future objects.
func NewScheduler ¶
NewScheduler returns Scheduler associated with the name, if it has not yet existed, create a new one.
Any Scheduler with a non-empty name will be associated with its name. Calling this function twice with the same name gives you the same Scheduler. If you want to create different Schedulers each call, use the empty name.
func (*Scheduler) After ¶
After creates a send-only channel. Sending a future to this channel will add it to scheduler after a duration. If d is negative, After will send the future to scheduler immediately.
NOTE: You should send ONLY ONE future to this channel because it is designed to handle one. If you try sending another, it will be blocked forever. To send other futures to scheduler, let call this method again.
func (*Scheduler) At ¶
At is a shortcut of After(time.Until(next)).
NOTE: You should send ONLY ONE future to this channel because it is designed to handle one. If you try sending another, it will be blocked forever. To send other futures to scheduler, let call this method again.
func (*Scheduler) Concurrent ¶
Concurrent limits the number of running futures at the same time. By default, there is no limited.
func (*Scheduler) Now ¶
func (s *Scheduler) Now() chan<- future
Now is a shortcut of After(0).
NOTE: You should send ONLY ONE future to this channel because it is designed to handle one. If you try sending another, it will be blocked forever. To send other futures to scheduler, let call this method again.
type Task ¶
type Task struct {
// contains filtered or unexported fields
}
Task is a future which runs one time.
Example ¶
package main import ( "fmt" "github.com/xybor-x/xysched" ) func panicFunc() { panic("custom panic function") } func main() { var scheduler = xysched.NewScheduler("") defer scheduler.Stop() // Example 1: Task is a simple future used for scheduling to run a function. var done = make(chan any) var future = xysched.NewTask(func(a ...any) { fmt.Println(a...) close(done) }, "1. foo") scheduler.Now() <- future <-done // Example 2: Callback will be run after the task completed. done = make(chan any) future = xysched.NewTask(fmt.Println, "2. foo foo") future.Callback(func() { close(done) }) scheduler.Now() <- future <-done // Example 3: Then adds a callback handling returned values of task after // task completed. done = make(chan any) future = xysched.NewTask(fmt.Sprintf, "3. foo %s", "bar") future.Then(func(s string) { fmt.Println(s) close(done) }) scheduler.Now() <- future <-done // Example 4: Catch adds a callback handling the panicked error of task if // the task panicked. // NOTE: if task panics a non-error interface, it will be wrapped into // xysched.CallError. done = make(chan any) future = xysched.NewTask(panicFunc) future.Then(func() { fmt.Println("4. Then branch") close(done) }) future.Catch(func(e error) { fmt.Println("4. Catch branch", e) close(done) }) scheduler.Now() <- future <-done }
Output: 1. foo 2. foo foo 3. foo bar 4. Catch branch CallError: custom panic function
func NewTask ¶
NewTask creates a future which runs function f with parameters params. This future runs only one time.
func (*Task) Callback ¶
Callback sets a callback future which will run after the task completed. The parameter params only should be used if f is a function. In case f was already a future, DO NOT use.
It returns the callback task if you passed a function or task, otherwise, nil.
func (*Task) Catch ¶
Catch sets a callback task which will be run after this task panicked. The only parameter of the callback task is the panicked error.
It returns the callback task.