-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueuerNextInterval.go
More file actions
78 lines (65 loc) · 2.87 KB
/
queuerNextInterval.go
File metadata and controls
78 lines (65 loc) · 2.87 KB
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
package queuer
import (
"log"
"log/slog"
"slices"
"github.com/siherrmann/queuer/helper"
"github.com/siherrmann/queuer/model"
)
// AddNextIntervalFunc adds a NextIntervalFunc to the worker's available next interval functions.
// It takes a NextIntervalFunc and adds it to the worker's AvailableNextIntervalFuncs.
// The function name is derived from the NextIntervalFunc interface using helper.GetTaskNameFromInterface.
// The function is meant to be used before starting the Queuer to ensure that the worker has access to the function.
// It checks if the function is nil or already exists in the worker's available next interval functions.
//
// If the function is nil or already exists, it panics.
// It returns the updated worker after adding the function.
func (q *Queuer) AddNextIntervalFunc(nif model.NextIntervalFunc) *model.Worker {
if nif == nil {
panic("NextIntervalFunc cannot be nil")
}
nifName, err := helper.GetTaskNameFromInterface(nif)
if err != nil {
log.Panicf("error getting function name: %s", err.Error())
}
if slices.Contains(q.worker.AvailableNextIntervalFuncs, nifName) {
log.Panicf("NextIntervalFunc already exists: %s", nifName)
}
q.nextIntervalFuncs[nifName] = nif
q.worker.AvailableNextIntervalFuncs = append(q.worker.AvailableNextIntervalFuncs, nifName)
worker, err := q.dbWorker.UpdateWorker(q.worker)
if err != nil {
log.Panicf("error updating worker: %s", err.Error())
}
q.log.Info("NextInterval function added", slog.String("name", nifName))
return worker
}
// AddNextIntervalFuncWithName adds a NextIntervalFunc to
// the worker's available next interval functions with a specific name.
// It takes a NextIntervalFunc and a name, checks if the function is nil
// or already exists in the worker's available next interval functions,
// and adds it to the worker's AvailableNextIntervalFuncs.
//
// This function is useful when you want to add a NextIntervalFunc
// with a specific name that you control, rather than deriving it from the function itself.
// It ensures that the function is not nil and that the name does not already exist
// in the worker's available next interval functions.
//
// If the function is nil or already exists, it panics.
// It returns the updated worker after adding the function with the specified name.
func (q *Queuer) AddNextIntervalFuncWithName(nif model.NextIntervalFunc, name string) *model.Worker {
if nif == nil {
panic("NextIntervalFunc cannot be nil")
}
if slices.Contains(q.worker.AvailableNextIntervalFuncs, name) {
log.Panicf("NextIntervalFunc with name already exists: %s", name)
}
q.nextIntervalFuncs[name] = nif
q.worker.AvailableNextIntervalFuncs = append(q.worker.AvailableNextIntervalFuncs, name)
worker, err := q.dbWorker.UpdateWorker(q.worker)
if err != nil {
log.Panicf("error updating worker: %s", err.Error())
}
q.log.Info("NextInterval function added", slog.String("name", name))
return worker
}