Skip to content

Commit

Permalink
Counter: ensure job name set on existing counter (#56)
Browse files Browse the repository at this point in the history
Ensure counter has the job name set when the counter already exists and
is loaded from etcd.

Signed-off-by: joshvanl <[email protected]>
  • Loading branch information
JoshVanL authored Oct 30, 2024
1 parent 8ee6207 commit 468a6e2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
49 changes: 18 additions & 31 deletions internal/counter/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,24 @@ func New(ctx context.Context, opts Options) (Interface, bool, error) {
return nil, false, err
}

c := &counter{
name: opts.Name,
counterKey: counterKey,
jobKey: jobKey,
client: opts.Client,
schedule: opts.Schedule,
job: opts.Job,
yard: opts.Yard,
collector: opts.Collector,
triggerRequest: &api.TriggerRequest{
Name: opts.Name,
Metadata: opts.Job.GetJob().GetMetadata(),
Payload: opts.Job.GetJob().GetPayload(),
},
}

if res.Count == 0 {
c := &counter{
name: opts.Name,
jobKey: jobKey,
counterKey: counterKey,
client: opts.Client,
schedule: opts.Schedule,
job: opts.Job,
count: &stored.Counter{JobPartitionId: opts.Job.GetPartitionId()},
yard: opts.Yard,
collector: opts.Collector,
triggerRequest: &api.TriggerRequest{
Name: opts.Name,
Metadata: opts.Job.GetJob().GetMetadata(),
Payload: opts.Job.GetJob().GetPayload(),
},
}
c.count = &stored.Counter{JobPartitionId: opts.Job.GetPartitionId()}

if ok, err := c.tickNext(); err != nil || !ok {
return nil, false, err
Expand All @@ -131,21 +132,7 @@ func New(ctx context.Context, opts Options) (Interface, bool, error) {
}
}

c := &counter{
counterKey: counterKey,
jobKey: jobKey,
client: opts.Client,
schedule: opts.Schedule,
job: opts.Job,
count: count,
yard: opts.Yard,
collector: opts.Collector,
triggerRequest: &api.TriggerRequest{
Name: opts.Name,
Metadata: opts.Job.GetJob().GetMetadata(),
Payload: opts.Job.GetJob().GetPayload(),
},
}
c.count = count

if ok, err := c.tickNext(); err != nil || !ok {
return nil, false, err
Expand Down
2 changes: 2 additions & 0 deletions internal/counter/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ func Test_New(t *testing.T) {
assert.True(t, ok)
assert.NotNil(t, c)

assert.Equal(t, "1", c.JobName())

cancel()
select {
case err := <-errCh:
Expand Down

0 comments on commit 468a6e2

Please sign in to comment.