Skip to content
This repository was archived by the owner on Feb 4, 2021. It is now read-only.

Commit 0f79d0e

Browse files
committed
Add job
1 parent 9c3e83b commit 0f79d0e

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

.env.ci

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ REDIS_ADDR=localhost:6379
2525

2626
DEBUG_LOG=true
2727

28+
JOB_INTERVAL_SEC=120
29+
2830
HYDRA_ADMIN_URL=http://hydra:4445

.env.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ REDIS_ADDR=redis:6379
2626

2727
DEBUG_LOG=true
2828

29+
JOB_INTERVAL_SEC=120
30+
2931
HYDRA_ADMIN_URL=http://hydra:4445

app/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type Config struct {
1111
DataBaseURL string `envconfig:"database_url" required:"true"`
1212
RedisAddr string `envconfig:"redis_addr" required:"true"`
1313
DebugLog bool `envconfig:"debug_log"`
14+
JobIntervalSec int `envconfig:"job_interval_sec" required:"true"`
1415
HydraAdminURL string `envconfig:"hydra_admin_url" required:"true"`
1516
MinioPublicURL string `envconfig:"minio_public_url" required:"true"`
1617
MinioEndpoint string `envconfig:"minio_endpoint" required:"true"`

app/job/job.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
}

app/run.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/ProgrammingLab/prolab-accounts/app/config"
1010
"github.com/ProgrammingLab/prolab-accounts/app/di"
1111
"github.com/ProgrammingLab/prolab-accounts/app/interceptor"
12+
"github.com/ProgrammingLab/prolab-accounts/app/job"
1213
"github.com/ProgrammingLab/prolab-accounts/app/server"
1314
)
1415

@@ -56,5 +57,9 @@ func Run() error {
5657
server.NewUserBlogServiceServer(store),
5758
),
5859
)
60+
61+
job.Start(store, cfg)
62+
defer job.Close()
63+
5964
return s.Serve()
6065
}

0 commit comments

Comments
 (0)