forked from rakanalh/scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
53 lines (44 loc) · 1.17 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"io"
"log"
"time"
"github.com/rakanalh/scheduler"
"github.com/rakanalh/scheduler/storage"
)
func TaskWithoutArgs() {
log.Println("TaskWithoutArgs is executed")
}
func TaskWithArgs(message string) {
log.Println("TaskWithArgs is executed. message:", message)
}
func main() {
storage, err := storage.NewPostgresStorage(
storage.PostgresDBConfig{
DbURL: "postgresql://<db-username>:<db-password>@localhost:5432/scheduler?sslmode=disable",
},
)
if err != nil {
log.Fatalf("Couldn't create scheduler storage : %v", err)
}
s := scheduler.New(storage)
go func(s scheduler.Scheduler, store io.Closer) {
time.Sleep(time.Second * 10)
// store.Close()
s.Stop()
}(s, storage)
// Start a task without arguments
if _, err := s.RunAfter(60*time.Second, TaskWithoutArgs); err != nil {
log.Fatal(err)
}
// Start a task with arguments
if _, err := s.RunEvery(5*time.Second, TaskWithArgs, "Hello from recurring task 1"); err != nil {
log.Fatal(err)
}
// Start the same task as above with a different argument
if _, err := s.RunEvery(10*time.Second, TaskWithArgs, "Hello from recurring task 2"); err != nil {
log.Fatal(err)
}
s.Start()
s.Wait()
}