/
main.go
76 lines (64 loc) · 2.04 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"github.com/dmitrymomot/go-pkg/worker"
"github.com/dmitrymomot/go-utils"
"github.com/hibiken/asynq"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)
func main() {
// Init logger
logger := logrus.WithFields(logrus.Fields{
"app": "worker",
"component": "main",
})
defer func() { logger.Info("Server successfully shutdown") }()
// Redis connect options for asynq client
redisConnOpt, err := asynq.ParseRedisURI("redis://localhost:6379/0") // TODO: Add your redis connection string
if err != nil {
logger.WithError(err).Fatal("failed to parse redis connection string")
}
// Init asynq client
asynqClient, err := asynq.NewClient(redisConnOpt)
defer asynqClient.Close()
// Create a context with a timeout and set the Server's context
ctx, cancel := utils.NewContextWithCancel(logger.WithField("component", "context"))
defer cancel()
// Create a new errgroup
eg, _ := errgroup.WithContext(ctx)
// Create a new scheduler server with the given options
schedulerServer := worker.NewSchedulerServer(
redisConnOpt, logger,
worker.WithSchedulerLocation("UTC"), // options are not required
)
defer schedulerServer.Shutdown()
// Init scheduler handlers
testScheduler := NewScheduler(nil)
// Run the scheduler
eg.Go(schedulerServer.Run(
testScheduler,
// TODO: Add more schedulers here
))
// Create a new queue worker server with the given options
queueServer := worker.NewQueueServer(
redisConnOpt, logger,
worker.WithQueueName("default"), // options are not required
)
defer queueServer.Shutdown()
// Init worker handlers
testWorker := NewWorker(nil)
// Run the worker
eg.Go(queueServer.Run(
testWorker,
// TODO: Add more workers here
))
// Create a new enqueuer
enqueuer := NewEnqueuer(worker.NewEnqueuer(asynqClient))
// TODO: Use enqueuer to enqueue tasks in your app,
// E,g: enqueuer.SendConfirmationEmail(ctx, "test@example", "123456")
_ = enqueuer
// Wait for the server to finish
if err := eg.Wait(); err != nil {
logger.WithError(err).Error("Server stopped with error")
}
}