Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit 60a3fd5

Browse files
committed
refactor(BUX-411): localCron as the only cron service
1 parent 26d7c36 commit 60a3fd5

8 files changed

+9
-77
lines changed

bux_suite_mocks_test.go

-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ func (tm *taskManagerMockBase) Close(context.Context) error {
3232

3333
func (tm *taskManagerMockBase) Debug(bool) {}
3434

35-
func (tm *taskManagerMockBase) Engine() taskmanager.Engine {
36-
return taskmanager.Empty
37-
}
38-
3935
func (tm *taskManagerMockBase) Factory() taskmanager.Factory {
4036
return taskmanager.FactoryEmpty
4137
}

client_options.go

-9
Original file line numberDiff line numberDiff line change
@@ -574,15 +574,6 @@ func WithTaskQUsingRedis(config *taskq.QueueOptions, redisOptions *redis.Options
574574
}
575575
}
576576

577-
// WithCronService will set the custom cron service provider
578-
func WithCronService(cronService taskmanager.CronService) ClientOps {
579-
return func(c *clientOptions) {
580-
if cronService != nil && c.taskManager != nil {
581-
c.taskManager.options = append(c.taskManager.options, taskmanager.WithCronService(cronService))
582-
}
583-
}
584-
}
585-
586577
// WithCronCustmPeriod will set the custom cron jobs period which will override the default
587578
func WithCronCustmPeriod(cronJobName string, period time.Duration) ClientOps {
588579
return func(c *clientOptions) {

client_options_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,6 @@ func TestWithTaskQ(t *testing.T) {
485485

486486
tm := tc.Taskmanager()
487487
require.NotNil(t, tm)
488-
assert.Equal(t, taskmanager.TaskQ, tm.Engine())
489488
assert.Equal(t, taskmanager.FactoryMemory, tm.Factory())
490489
})
491490

@@ -517,7 +516,6 @@ func TestWithTaskQ(t *testing.T) {
517516

518517
tm := tc.Taskmanager()
519518
require.NotNil(t, tm)
520-
assert.Equal(t, taskmanager.TaskQ, tm.Engine())
521519
assert.Equal(t, taskmanager.FactoryRedis, tm.Factory())
522520
})
523521
}

taskmanager/cron.go

-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@ package taskmanager
22

33
import "github.com/robfig/cron/v3"
44

5-
// localCron will load a local version of cron if it was not provided by the user
6-
func (c *Client) localCron() {
7-
cr := &cronLocal{}
8-
cr.New()
9-
cr.Start()
10-
c.options.cronService = cr
11-
}
12-
135
// cronLocal is the interface for the "local cron" service
146
type cronLocal struct {
157
cronService *cron.Cron

taskmanager/interface.go

+2-15
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,13 @@ import (
66
taskq "github.com/vmihailenco/taskq/v3"
77
)
88

9-
// TaskService is the task related methods
10-
type TaskService interface {
9+
// ClientInterface is the taskmanager client interface
10+
type ClientInterface interface {
1111
RegisterTask(task *Task) error
1212
ResetCron()
1313
RunTask(ctx context.Context, options *TaskOptions) error
1414
Tasks() map[string]*taskq.Task
1515
CronJobsInit(cronJobsMap CronJobs) error
16-
}
17-
18-
// CronService is the cron service provider
19-
type CronService interface {
20-
AddFunc(spec string, cmd func()) (int, error)
21-
New()
22-
Start()
23-
Stop()
24-
}
25-
26-
// ClientInterface is the taskmanager client interface
27-
type ClientInterface interface {
28-
TaskService
2916
Close(ctx context.Context) error
3017
Debug(on bool)
3118
Factory() Factory

taskmanager/taskmanager.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type (
1818

1919
// clientOptions holds all the configuration for the client
2020
clientOptions struct {
21-
cronService CronService // Internal cron job client
21+
cronService *cronLocal // Internal cron job client
2222
debug bool // For extra logs and additional debug information
2323
logger *zerolog.Logger // Internal logging
2424
newRelicEnabled bool // If NewRelic is enabled (parent application)
@@ -56,14 +56,16 @@ func NewClient(_ context.Context, opts ...ClientOps) (ClientInterface, error) {
5656
// Use NewRelic if it's enabled (use existing txn if found on ctx)
5757
// ctx = client.options.getTxnCtx(ctx)
5858

59+
// Load the TaskQ engine
5960
if err := client.loadTaskQ(); err != nil {
6061
return nil, err
6162
}
6263

63-
// Detect if a cron service provider was set
64-
if client.options.cronService == nil { // Use a local cron
65-
client.localCron()
66-
}
64+
// Create the cron scheduler
65+
cr := &cronLocal{}
66+
cr.New()
67+
cr.Start()
68+
client.options.cronService = cr
6769

6870
// Return the client
6971
return client, nil

taskmanager/taskmanager_options.go

-9
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,3 @@ func WithLogger(customLogger *zerolog.Logger) ClientOps {
6969
}
7070
}
7171
}
72-
73-
// WithCronService will set the cron service
74-
func WithCronService(cronService CronService) ClientOps {
75-
return func(c *clientOptions) {
76-
if cronService != nil {
77-
c.cronService = cronService
78-
}
79-
}
80-
}

taskmanager/taskmanager_options_test.go

-25
Original file line numberDiff line numberDiff line change
@@ -95,28 +95,3 @@ func TestWithLogger(t *testing.T) {
9595
assert.Equal(t, &customLogger, options.logger)
9696
})
9797
}
98-
99-
// TestWithCronService will test the method WithCronService()
100-
func TestWithCronService(t *testing.T) {
101-
t.Parallel()
102-
103-
t.Run("check type", func(t *testing.T) {
104-
opt := WithCronService(nil)
105-
assert.IsType(t, *new(ClientOps), opt)
106-
})
107-
108-
t.Run("test applying nil", func(t *testing.T) {
109-
options := &clientOptions{}
110-
opt := WithCronService(nil)
111-
opt(options)
112-
assert.Nil(t, options.cronService)
113-
})
114-
115-
t.Run("test applying option", func(t *testing.T) {
116-
options := &clientOptions{}
117-
customCron := &cronLocal{}
118-
opt := WithCronService(customCron)
119-
opt(options)
120-
assert.Equal(t, customCron, options.cronService)
121-
})
122-
}

0 commit comments

Comments
 (0)