-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueuerTask.go
More file actions
68 lines (56 loc) · 2.06 KB
/
queuerTask.go
File metadata and controls
68 lines (56 loc) · 2.06 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
package queuer
import (
"log"
"log/slog"
"slices"
"github.com/siherrmann/queuer/model"
)
// AddTask adds a new task to the queuer.
// It creates a new task with the provided task interface, adds it to the worker's available tasks,
// and updates the worker in the database.
// The task name is automatically generated based on the task's function name (eg. main.TestTask).
//
// If the task creation fails, it logs a panic error and exits the program.
// It returns the newly created task.
func (q *Queuer) AddTask(task interface{}) *model.Task {
newTask, err := model.NewTask(task)
if err != nil {
log.Panicf("error creating new task: %s", err.Error())
}
if slices.Contains(q.worker.AvailableTasks, newTask.Name) {
log.Panicf("task already exists: %s", newTask.Name)
}
q.tasks[newTask.Name] = newTask
q.worker.AvailableTasks = append(q.worker.AvailableTasks, newTask.Name)
// Update worker in DB
_, err = q.dbWorker.UpdateWorker(q.worker)
if err != nil {
log.Panicf("error updating worker: %s", err.Error())
}
q.log.Info("Task added", slog.String("task_name", newTask.Name))
return newTask
}
// AddTaskWithName adds a new task with a specific name to the queuer.
// It creates a new task with the provided task interface and name, adds it to the worker's available tasks,
// and updates the worker in the database.
//
// If task creation fails, it logs a panic error and exits the program.
// It returns the newly created task.
func (q *Queuer) AddTaskWithName(task interface{}, name string) *model.Task {
newTask, err := model.NewTaskWithName(task, name)
if err != nil {
log.Panicf("error creating new task: %s", err.Error())
}
if slices.Contains(q.worker.AvailableTasks, name) {
log.Panicf("task already exists: %s", newTask.Name)
}
q.tasks[newTask.Name] = newTask
q.worker.AvailableTasks = append(q.worker.AvailableTasks, newTask.Name)
// Update worker in DB
_, err = q.dbWorker.UpdateWorker(q.worker)
if err != nil {
log.Panicf("error updating worker: %s", err.Error())
}
q.log.Info("Task added", slog.String("name", newTask.Name))
return newTask
}