forked from dipeshdulal/event-scheduling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (42 loc) · 1.24 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
54
55
56
57
package main
import (
"context"
"github.com/dipeshdulal/event-scheduling/dbdrivers"
"log"
"os"
"os/signal"
"time"
"github.com/joho/godotenv"
"github.com/dipeshdulal/event-scheduling/customevents"
)
var eventListeners = Listeners{
"SendEmail": customevents.SendEmail,
"PayBills": customevents.PayBills,
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
if err := godotenv.Load(); err != nil {
log.Fatal("Error loading .env file ", err)
}
dbc := dbdrivers.GetDbConn()
seeder := dbdrivers.GetDbSeeder()
if err := seeder(dbc); err != nil {
log.Fatal("Failed to seed db", err)
}
scheduler := NewScheduler(dbc, eventListeners)
stopCron := scheduler.StartCron()
defer stopCron()
scheduler.CheckEventsInInterval(ctx, time.Minute)
scheduler.Schedule("SendEmail", "mail: [email protected]", time.Now().Add(1*time.Minute))
scheduler.Schedule("PayBills", "paybills: $4,000 bill", time.Now().Add(2*time.Minute))
scheduler.ScheduleCron("SendEmail", "mail: [email protected]", "* * * * *")
go func() {
for range interrupt {
log.Println("\n❌ Interrupt received closing...")
cancel()
}
}()
<-ctx.Done()
}