-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
110 lines (95 loc) · 2.52 KB
/
main.go
File metadata and controls
110 lines (95 loc) · 2.52 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/fighting-dreamer/go-scheduler/entity"
"github.com/fighting-dreamer/go-scheduler/repo/db"
"github.com/fighting-dreamer/go-scheduler/repo/local"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
)
func localStore() {
// Initialize repositories
jobStore := local.NewLocalJobStore()
executionLogRepo := local.NewLocalExecutionLogRepo()
// Create a new job
newJob := entity.Job{
CronExpression: "*/5 * * * *",
Enabled: true,
}
// Add the job
addedJob, err := jobStore.Add(newJob)
if err != nil {
fmt.Println("Error adding job:", err)
return
}
fmt.Println("Added job:", addedJob)
// Get the job
retrievedJob, err := jobStore.Get(addedJob.ID)
if err != nil {
fmt.Println("Error getting job:", err)
return
}
fmt.Println("Retrieved job:", retrievedJob)
// Toggle job status
status, err := jobStore.ToggleStatus(addedJob.ID)
if err != nil {
fmt.Println("Error toggling job status:", err)
return
}
fmt.Println("Toggled job status, new status (enabled):", status)
// Create an execution log
executionLog := entity.ExecutionLog{
ID: uuid.New().String(),
JobID: addedJob.ID,
ExecutionEpochTime: time.Now().Unix(),
Status: entity.ExecutionStatus_PENDING,
}
// Add the execution log
addedLog, err := executionLogRepo.Add(executionLog)
if err != nil {
fmt.Println("Error adding execution log:", err)
return
}
fmt.Println("Added execution log:", addedLog)
// Get the execution log
retrievedLog, err := executionLogRepo.Get(addedLog.ID)
if err != nil {
fmt.Println("Error getting execution log:", err)
return
}
fmt.Println("Retrieved execution log:", retrievedLog)
}
func usingLocalPostgres() {
ctx := context.Background()
DATABASE_URL := "postgres://postgres:password@localhost:5432/scheduler?sslmode=disable"
pool, err := pgxpool.New(ctx, DATABASE_URL)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to create connection pool: %v\n", err)
os.Exit(1)
}
defer pool.Close()
store := db.NewStore(pool)
var pgUUID pgtype.UUID
err = pgUUID.Scan(uuid.NewString())
if err != nil {
log.Fatal(err)
}
res, err := store.AddJob(ctx, db.AddJobParams{
ID: pgUUID,
CronExpression: "*/1 * * * *",
NextExecutionEpochTime: time.Now().Unix(),
Enabled: true,
})
if err != nil {
log.Fatalf("Add job failed: %v", err)
}
fmt.Println("Added job:", res)
}
func main() {
usingLocalPostgres()
}