-
Notifications
You must be signed in to change notification settings - Fork 4
/
raffle.go
142 lines (128 loc) · 3.22 KB
/
raffle.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2021 MIMIRO AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package jobs
import (
"context"
"sync"
"time"
"github.com/DataDog/datadog-go/v5/statsd"
"go.uber.org/zap"
)
type raffle struct {
logger *zap.SugaredLogger
statsdClient statsd.ClientInterface
ticketsFull int
ticketsIncr int
runningJobs map[string]*runState
runningMu sync.Mutex
}
type runState struct {
started time.Time
isFull bool
isEvent bool
id string
title string
ctx context.Context
cancel context.CancelFunc
}
type ticket struct {
runState *runState
}
func NewRaffle(
ticketsFull int,
ticketsIncr int,
logger *zap.SugaredLogger,
statsdClient statsd.ClientInterface,
) *raffle {
return &raffle{
logger: logger,
statsdClient: statsdClient,
ticketsFull: ticketsFull,
ticketsIncr: ticketsIncr,
runningJobs: make(map[string]*runState),
runningMu: sync.Mutex{},
}
}
func (r *raffle) borrowTicket(job *job) *ticket {
tags := []string{
"application:datahub",
}
r.runningMu.Lock()
defer r.runningMu.Unlock()
_, ok := r.runningJobs[job.id] // is the job running
if ok { // it is, don't give a ticket
return nil
}
if job.pipeline.isFullSync() {
if r.ticketsFull > 0 {
r.ticketsFull--
_ = r.statsdClient.Gauge("jobs.tickets.full", float64(r.ticketsFull), tags, 1)
ctx, cancel := context.WithCancel(context.Background())
state := &runState{
started: time.Now(),
isEvent: job.isEvent,
isFull: true,
id: job.id,
title: job.title,
ctx: ctx,
cancel: cancel,
}
r.runningJobs[job.id] = state
return &ticket{runState: state}
}
} else {
if r.ticketsIncr > 0 {
r.ticketsIncr--
_ = r.statsdClient.Gauge("jobs.tickets.incr", float64(r.ticketsIncr), tags, 1)
ctx, cancel := context.WithCancel(context.Background())
state := &runState{
started: time.Now(),
isEvent: job.isEvent,
isFull: false,
id: job.id,
title: job.title,
ctx: ctx,
cancel: cancel,
}
r.runningJobs[job.id] = state
return &ticket{runState: state}
}
}
return nil
}
func (r *raffle) returnTicket(ticket *ticket) {
tags := []string{
"application:datahub",
}
r.runningMu.Lock()
defer r.runningMu.Unlock()
delete(r.runningJobs, ticket.runState.id)
if ticket.runState.isFull {
r.ticketsFull++
_ = r.statsdClient.Gauge("jobs.tickets.full", float64(r.ticketsFull), tags, 1)
} else {
r.ticketsIncr++
_ = r.statsdClient.Gauge("jobs.tickets.incr", float64(r.ticketsIncr), tags, 1)
}
}
func (r *raffle) runningJob(jobid string) *runState {
state, ok := r.runningJobs[jobid]
if ok {
return state
}
return nil
}
func (r *raffle) getRunningJobs() map[string]*runState {
return r.runningJobs
}