You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Chrono is a scheduler library which lets you run your task and code periodically
7
+
Chrono is a scheduler library that lets you run your tasks and code periodically. It provides different scheduling functionalities to make it easier to create a scheduling task.
8
8
9
-
## Schedule Task With Fixed Delay
10
-
Scheduling a task at a Fixed Delay can be done with the help of the **ScheduleWithFixedDelay** method.
9
+
## Scheduling a One-Shot Task
10
+
The Schedule method helps us schedule the task to run once at the specified time. In the following example, the task will first be executed 1 second after the current time.
11
+
**WithStartTime** option is used to specify the execution time.
11
12
12
-
## Schedule Task at a Fixed Rate
13
-
Scheduling a task at a Fixed Rate can be done with the help of the **ScheduleAtFixedRate** method.
Let's schedule a task to run at a fixed rate of seconds.
20
+
if err == nil {
21
+
log.Print("Task has been scheduled successfully.")
22
+
}
23
+
```
24
+
25
+
## Scheduling a Task with Fixed Delay
26
+
Let's schedule a task to run with a fixed delay between the finish time of the last execution of the task and the start time of the next execution of the task.
27
+
The fixed delay counts the delay after the completion of the last execution.
log.Print("Task has been scheduled successfully.")
28
39
}
29
40
```
30
41
31
-
The next task will run always after 5 seconds no matter the status of previous task, which may be still running. So even if the previous task isn't done, the next taskwill run.
42
+
Since the task itself takes 3 seconds to complete and we have specified a delay of 5 seconds between the finish time of the last execution of the task and the start time of the next execution of the task, there will be a delay of 8 seconds between each execution.
32
43
44
+
**WithStartTime** and **WithLocation** options can be combined with this.
33
45
34
-
### Scheduling the Task at a Fixed Rate From a Given Date
35
-
Let's schedule a task to run at a fixed rate from a given date.
46
+
##Schedule Task at a Fixed Rate
47
+
Let's schedule a task to run at a fixed rate of seconds.
log.Print("Task has been scheduled successfully.")
58
+
}
59
+
```
60
+
61
+
The next task will run always after 5 seconds no matter the status of the previous task, which may be still running. So even if the previous task isn't done, the next task will run.
62
+
We can also use the **WithStartTime** option to specify the desired first execution time of the task.
When we use this option, the task will run at the specified execution time and subsequently with the given period. In the above example, the task will first be executed 2 seconds after the current time.
73
+
74
+
We can also combine this option with **WithLocation** based on our requirements.
The task will be executed the first time 2 seconds after the current time, and it will continue to be executed according to the given fixed rate.
85
+
In the above example, the task will first be executed at 18:45 of the current date in America/New York time.
86
+
**If the start time is in the past, the task will be executed immediately.**
52
87
53
-
## Schedule Task With Cron Expression
54
-
Sometimes Fixed Delay and Fixed Rate are not enough, and we need the flexibility of a cron expression to schedule our tasks. With the help of the provided **ScheduleWithCron** method, we can schedule a task based on a cron expression.
88
+
## Scheduling a Task using Cron Expression
89
+
Sometimes Fixed Rate and Fixed Delay can not fulfill your needs, and we need the flexibility of cron expressions to schedule the execution of your tasks. With the help of the provided **ScheduleWithCron method**, we can schedule a task based on a cron expression.
In the above example, Task will be scheduled to be executed at 18:45 on the 10th day of every month in America/New York time.
114
+
115
+
**WithStartTimeoption** cannot be used with **ScheduleWithCron**.
116
+
117
+
## Canceling a Scheduled Task
118
+
Schedule methods return an instance of type ScheduledTask, which allows us to cancel a task or to check if the task is canceled. The Cancel method cancels the scheduled task but running tasks won't be interrupted.
The **Shutdown()** method doesn't cause immediate shut down of the Scheduler and returns a channel. It will make the Scheduler stop accepting new tasks and shut down after all running tasks finish their current work.
0 commit comments