This repository was archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcron_job_declarations.go
69 lines (59 loc) · 1.5 KB
/
cron_job_declarations.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
58
59
60
61
62
63
64
65
66
67
68
69
package bux
import (
"context"
"time"
"github.com/BuxOrg/bux/taskmanager"
)
// Cron job names to be used in WithCronCustomPeriod
const (
CronJobNameDraftTransactionCleanUp = "draft_transaction_clean_up"
CronJobNameSyncTransactionBroadcast = "sync_transaction_broadcast"
CronJobNameSyncTransactionSync = "sync_transaction_sync"
CronJobNameCalculateMetrics = "calculate_metrics"
)
type cronJobHandler func(ctx context.Context, client *Client) error
// here is where we define all the cron jobs for the client
func (c *Client) cronJobs() taskmanager.CronJobs {
jobs := taskmanager.CronJobs{}
addJob := func(name string, period time.Duration, task cronJobHandler) {
// handler adds the client pointer to the cronJobTask by using a closure
handler := func(ctx context.Context) (err error) {
if metrics, enabled := c.Metrics(); enabled {
end := metrics.TrackCron(name)
defer func() {
success := err == nil
end(success)
}()
}
err = task(ctx, c)
return
}
jobs[name] = taskmanager.CronJob{
Handler: handler,
Period: period,
}
}
addJob(
CronJobNameDraftTransactionCleanUp,
60*time.Second,
taskCleanupDraftTransactions,
)
addJob(
CronJobNameSyncTransactionBroadcast,
2*time.Minute,
taskBroadcastTransactions,
)
addJob(
CronJobNameSyncTransactionSync,
5*time.Minute,
taskSyncTransactions,
)
if _, enabled := c.Metrics(); enabled {
addJob(
CronJobNameCalculateMetrics,
15*time.Second,
taskCalculateMetrics,
)
}
return jobs
}