This repository was archived by the owner on Feb 4, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +75
-0
lines changed Expand file tree Collapse file tree 5 files changed +75
-0
lines changed Original file line number Diff line number Diff line change @@ -25,4 +25,6 @@ REDIS_ADDR=localhost:6379
25
25
26
26
DEBUG_LOG = true
27
27
28
+ JOB_INTERVAL_SEC = 120
29
+
28
30
HYDRA_ADMIN_URL = http://hydra:4445
Original file line number Diff line number Diff line change @@ -26,4 +26,6 @@ REDIS_ADDR=redis:6379
26
26
27
27
DEBUG_LOG = true
28
28
29
+ JOB_INTERVAL_SEC = 120
30
+
29
31
HYDRA_ADMIN_URL = http://hydra:4445
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ type Config struct {
11
11
DataBaseURL string `envconfig:"database_url" required:"true"`
12
12
RedisAddr string `envconfig:"redis_addr" required:"true"`
13
13
DebugLog bool `envconfig:"debug_log"`
14
+ JobIntervalSec int `envconfig:"job_interval_sec" required:"true"`
14
15
HydraAdminURL string `envconfig:"hydra_admin_url" required:"true"`
15
16
MinioPublicURL string `envconfig:"minio_public_url" required:"true"`
16
17
MinioEndpoint string `envconfig:"minio_endpoint" required:"true"`
Original file line number Diff line number Diff line change
1
+ package job
2
+
3
+ import (
4
+ "time"
5
+
6
+ "google.golang.org/grpc/grpclog"
7
+
8
+ "github.com/ProgrammingLab/prolab-accounts/app/config"
9
+ "github.com/ProgrammingLab/prolab-accounts/app/di"
10
+ "github.com/ProgrammingLab/prolab-accounts/app/util"
11
+ )
12
+
13
+ var (
14
+ started = false
15
+ stop = make (chan struct {})
16
+ jobs = []Job {}
17
+ )
18
+
19
+ // Job represents job for worker
20
+ type Job func (store di.StoreComponent ) error
21
+
22
+ // Start starts the worker
23
+ func Start (store di.StoreComponent , cfg * config.Config ) {
24
+ if started {
25
+ return
26
+ }
27
+ started = true
28
+
29
+ go func () {
30
+ run (store , time .Duration (cfg .JobIntervalSec )* time .Second )
31
+ }()
32
+ }
33
+
34
+ // Close stops the worker
35
+ func Close () {
36
+ grpclog .Infoln ("worker is stopping(^C to force to stop)" )
37
+ stop <- struct {}{}
38
+ }
39
+
40
+ func run (store di.StoreComponent , interval time.Duration ) {
41
+ defer func () {
42
+ if err := util .ErrorFromRecover (recover ()); err != nil {
43
+ grpclog .Errorf ("job panic: %+v" , err )
44
+ grpclog .Infoln ("worker is restarting..." )
45
+ run (store , interval )
46
+ }
47
+ }()
48
+
49
+ grpclog .Infof ("worker started: interval %v" , interval )
50
+
51
+ for {
52
+ select {
53
+ case <- time .After (interval ):
54
+ for _ , j := range jobs {
55
+ err := j (store )
56
+ if err != nil {
57
+ grpclog .Errorf ("job error: %+v" , err )
58
+ }
59
+ }
60
+ case <- stop :
61
+ grpclog .Infoln ("worker stopped" )
62
+ return
63
+ }
64
+ }
65
+ }
Original file line number Diff line number Diff line change 9
9
"github.com/ProgrammingLab/prolab-accounts/app/config"
10
10
"github.com/ProgrammingLab/prolab-accounts/app/di"
11
11
"github.com/ProgrammingLab/prolab-accounts/app/interceptor"
12
+ "github.com/ProgrammingLab/prolab-accounts/app/job"
12
13
"github.com/ProgrammingLab/prolab-accounts/app/server"
13
14
)
14
15
@@ -56,5 +57,9 @@ func Run() error {
56
57
server .NewUserBlogServiceServer (store ),
57
58
),
58
59
)
60
+
61
+ job .Start (store , cfg )
62
+ defer job .Close ()
63
+
59
64
return s .Serve ()
60
65
}
You can’t perform that action at this time.
0 commit comments