forked from cybertec-postgresql/pg_timetable
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
51 lines (46 loc) · 1.29 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
package main
import (
"context"
"os"
"time"
"github.com/cybertec-postgresql/pg_timetable/internal/cmdparser"
"github.com/cybertec-postgresql/pg_timetable/internal/pgengine"
"github.com/cybertec-postgresql/pg_timetable/internal/scheduler"
)
/**
* pg_timetable is the daemon application responsible to execute scheduled SQL tasks that cannot be triggered by the
* PostgreSQL server (PostgreSQL does not support time triggers).
*
* This application may run in the same machine as PostgreSQL server and must grant full access permission to the
* timetable tables.
*/
func main() {
ctx := context.Background()
cmdOpts, err := cmdparser.Parse()
if err != nil {
pgengine.LogToDB("PANIC", "Error parsing command line arguments: ", err)
os.Exit(2)
}
connctx, cancel := context.WithTimeout(ctx, 90*time.Second)
defer cancel()
if !pgengine.InitAndTestConfigDBConnection(connctx, *cmdOpts) {
os.Exit(2)
}
defer pgengine.FinalizeConfigDBConnection()
if cmdOpts.Upgrade {
if !pgengine.MigrateDb(ctx) {
os.Exit(3)
}
} else {
if upgrade, err := pgengine.CheckNeedMigrateDb(ctx); upgrade || err != nil {
os.Exit(3)
}
}
if cmdOpts.Init {
os.Exit(0)
}
pgengine.SetupCloseHandler()
for scheduler.Run(ctx) == scheduler.ConnectionDroppped {
pgengine.ReconnectDbAndFixLeftovers(ctx)
}
}