-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs.go
94 lines (74 loc) · 1.67 KB
/
jobs.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package jobs
import (
"context"
"database/sql"
sync "sync"
"github.com/galexrt/fivenet/pkg/mstlystcdata"
"github.com/galexrt/fivenet/pkg/perms"
"github.com/galexrt/fivenet/pkg/server/audit"
"github.com/galexrt/fivenet/pkg/tracker"
"github.com/galexrt/fivenet/query/fivenet/table"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
"go.uber.org/fx"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var (
tUser = table.Users.AS("user")
)
var (
ErrFailedQuery = status.Error(codes.Internal, "errors.JobsService.ErrFailedQuery")
)
type Server struct {
JobsServiceServer
ctx context.Context
logger *zap.Logger
wg sync.WaitGroup
tracer trace.Tracer
db *sql.DB
p perms.Permissions
enricher *mstlystcdata.Enricher
auditer audit.IAuditer
tracker *tracker.Tracker
}
type Params struct {
fx.In
LC fx.Lifecycle
Logger *zap.Logger
TP *tracesdk.TracerProvider
DB *sql.DB
Perms perms.Permissions
Enricher *mstlystcdata.Enricher
Audit audit.IAuditer
Tracker *tracker.Tracker
}
func NewServer(p Params) *Server {
ctx, cancel := context.WithCancel(context.Background())
s := &Server{
ctx: ctx,
logger: p.Logger.Named("jobs"),
wg: sync.WaitGroup{},
tracer: p.TP.Tracer("jobs"),
db: p.DB,
p: p.Perms,
enricher: p.Enricher,
auditer: p.Audit,
tracker: p.Tracker,
}
p.LC.Append(fx.StartHook(func(_ context.Context) error {
s.wg.Add(1)
go func() {
defer s.wg.Done()
s.runTimeclock()
}()
return nil
}))
p.LC.Append(fx.StopHook(func(_ context.Context) error {
cancel()
s.wg.Wait()
return nil
}))
return s
}