Skip to content

Commit d4258d4

Browse files
committed
Merge remote-tracking branch 'origin/main' into main
2 parents e0badeb + 6082478 commit d4258d4

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,74 @@
55
[![codecov](https://codecov.io/gh/procyon-projects/chrono/branch/main/graph/badge.svg?token=OREV0YI8VU)](https://codecov.io/gh/procyon-projects/chrono)
66

77
Chrono is a scheduler library which lets you run your task and code periodically
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.
11+
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.
14+
15+
### Scheduling the Task at a Fixed Rate
16+
17+
Let's schedule a task to run at a fixed rate of seconds.
18+
19+
```go
20+
taskScheduler := chrono.NewDefaultTaskScheduler()
21+
22+
task, err := taskScheduler.ScheduleAtFixedRate(func(ctx context.Context) {
23+
log.Print("Fixed Rate of 5 seconds")
24+
}, 5 * time.Second)
25+
26+
if err == nil {
27+
log.Print("Task has been scheduled")
28+
}
29+
```
30+
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 task will run.
32+
33+
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.
36+
37+
```go
38+
taskScheduler := chrono.NewDefaultTaskScheduler()
39+
40+
now := time.Now()
41+
42+
task, err := taskScheduler.ScheduleAtFixedRate(func(ctx context.Context) {
43+
log.Print("Fixed Rate of 5 seconds")
44+
}, 5 * time.Second, WithStartTime(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second() + 2))
45+
46+
if err == nil {
47+
log.Print("Task has been scheduled")
48+
}
49+
```
50+
51+
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.
52+
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.
55+
56+
```go
57+
taskScheduler := chrono.NewDefaultTaskScheduler()
58+
59+
task, err := taskScheduler.ScheduleWithCron(func(ctx context.Context) {
60+
log.Print("Scheduled Task With Cron")
61+
}, "0 45 18 10 * *")
62+
63+
if err == nil {
64+
log.Print("Task has been scheduled")
65+
}
66+
```
67+
68+
In this case, we're scheduling a task to be executed at 18:45 on the 10th day of every month
69+
70+
By default, the local time is used for the cron expression. However, we can use the **WithLocation** option to change this.
71+
72+
```go
73+
task, err := taskScheduler.ScheduleWithCron(func(ctx context.Context) {
74+
log.Print("Scheduled Task With Cron")
75+
}, "0 45 18 10 * *", WithLocation("America/New_York"))
76+
```
77+
78+
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.

0 commit comments

Comments
 (0)