-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueuerJob.go
More file actions
566 lines (478 loc) · 17.5 KB
/
queuerJob.go
File metadata and controls
566 lines (478 loc) · 17.5 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package queuer
import (
"database/sql"
"fmt"
"log/slog"
"strings"
"time"
"github.com/google/uuid"
"github.com/siherrmann/queuer/core"
"github.com/siherrmann/queuer/helper"
"github.com/siherrmann/queuer/model"
)
// AddJob adds a job to the queue with the given task and parameters.
// As a task you can either pass a function or a string with the task name
// (necessary if you want to use a task with a name set by you).
// It returns the created job or an error if something goes wrong.
func (q *Queuer) AddJob(task interface{}, parametersKeyed map[string]interface{}, parameters ...interface{}) (*model.Job, error) {
options := q.mergeOptions(nil)
job, err := q.addJob(task, options, parametersKeyed, parameters...)
if err != nil {
q.log.Error("Error adding job", slog.String("error", err.Error()))
return nil, helper.NewError("adding job", err)
}
q.log.Info("Job added", slog.String("job_rid", job.RID.String()))
return job, nil
}
// AddJobTx adds a job to the queue with the given task and parameters within a transaction.
// As a task you can either pass a function or a string with the task name
// (necessary if you want to use a task with a name set by you).
// It returns the created job or an error if something goes wrong.
func (q *Queuer) AddJobTx(tx *sql.Tx, task interface{}, parametersKeyed map[string]interface{}, parameters ...interface{}) (*model.Job, error) {
options := q.mergeOptions(nil)
job, err := q.addJobTx(tx, task, options, parametersKeyed, parameters...)
if err != nil {
q.log.Error("Error adding job with transaction", slog.String("error", err.Error()))
return nil, helper.NewError("adding job", err)
}
q.log.Info("Job added", slog.String("job_rid", job.RID.String()))
return job, nil
}
/*
AddJobWithOptions adds a job with the given task, options, and parameters.
As a task you can either pass a function or a string with the task name
(necessary if you want to use a task with a name set by you).
It returns the created job or an error if something goes wrong.
The options parameter allows you to specify additional options for the job,
such as scheduling, retry policies, and error handling.
If options are nil, the worker's default options will be used.
Example usage:
func AddJobExample(queuer *Queuer, param1 string, param2 int) {
options := &model.Options{
OnError: &model.OnError{
Timeout: 5,
MaxRetries: 2, Runs 3 times, first is not a retry
RetryDelay: 1,
RetryBackoff: model.RETRY_BACKOFF_NONE,
},
Schedule: &model.Schedule{
Start: time.Now().Add(10 * time.Second),
Interval: 5 * time.Second,
MaxCount: 3,
},
}
job, err := queuer.AddJobWithOptions(options, myTaskFunction, param1, param2)
if err != nil {
log.Fatalf("Failed to add job: %v", err)
}
}
*/
func (q *Queuer) AddJobWithOptions(options *model.Options, task interface{}, parametersKeyed map[string]interface{}, parameters ...interface{}) (*model.Job, error) {
q.mergeOptions(options)
job, err := q.addJob(task, options, parametersKeyed, parameters...)
if err != nil {
q.log.Error("Error adding job with options", slog.String("error", err.Error()))
return nil, helper.NewError("adding job", err)
}
q.log.Info("Job with options added", slog.String("job_rid", job.RID.String()))
return job, nil
}
// AddJobWithOptionsTx adds a job with the given task, options, and parameters within a transaction.
// As a task you can either pass a function or a string with the task name
// (necessary if you want to use a task with a name set by you).
// It returns the created job or an error if something goes wrong.
func (q *Queuer) AddJobWithOptionsTx(tx *sql.Tx, options *model.Options, task interface{}, parametersKeyed map[string]interface{}, parameters ...interface{}) (*model.Job, error) {
q.mergeOptions(options)
job, err := q.addJobTx(tx, task, options, parametersKeyed, parameters...)
if err != nil {
q.log.Error("Error adding job with transaction and options", slog.String("error", err.Error()))
return nil, helper.NewError("adding job", err)
}
q.log.Info("Job with options added", slog.String("job_rid", job.RID.String()))
return job, nil
}
// AddJobs adds a batch of jobs to the queue.
// It takes a slice of BatchJob, which contains the task, options, and parameters for each job.
// It returns an error if something goes wrong during the process.
func (q *Queuer) AddJobs(batchJobs []model.BatchJob) error {
var jobs []*model.Job
for _, batchJob := range batchJobs {
options := q.mergeOptions(batchJob.Options)
newJob, err := model.NewJob(batchJob.Task, options, batchJob.ParametersKeyed, batchJob.Parameters...)
if err != nil {
return fmt.Errorf("error creating job with job options: %v", err)
}
jobs = append(jobs, newJob)
}
err := q.dbJob.BatchInsertJobs(jobs)
if err != nil {
q.log.Error("Error inserting jobs", slog.String("error", err.Error()))
return helper.NewError("batch insert", err)
}
q.log.Info("Jobs added", slog.Int("count", len(jobs)))
return nil
}
// WaitForJobAdded waits for any job to start and returns the job.
// It listens for job insert events and returns the job when it is added to the queue.
func (q *Queuer) WaitForJobAdded() *model.Job {
jobStarted := make(chan *model.Job, 1)
outerReady := make(chan struct{})
ready := make(chan struct{})
go func() {
close(outerReady)
q.jobInsertListener.Listen(q.ctx, ready, func(job *model.Job) {
jobStarted <- job
})
}()
<-outerReady
<-ready
for {
select {
case job := <-jobStarted:
return job
case <-q.ctx.Done():
return nil
}
}
}
// WaitForJobFinished waits for a job to finish and returns the job.
// It listens for job delete events and returns the job when it is finished.
// If timeout is reached before the job finishes, it returns nil.
func (q *Queuer) WaitForJobFinished(jobRid uuid.UUID, timeout time.Duration) *model.Job {
// First check if the job is already finished (in archive)
if archivedJob, err := q.dbJob.SelectJobFromArchive(jobRid); err == nil && archivedJob != nil {
return archivedJob
}
jobFinished := make(chan *model.Job, 1)
outerReady := make(chan struct{})
ready := make(chan struct{})
go func() {
close(outerReady)
q.jobDeleteListener.Listen(q.ctx, ready, func(job *model.Job) {
if job.RID == jobRid {
jobFinished <- job
}
})
}()
<-outerReady
<-ready
timeoutChan := time.After(timeout)
for {
select {
case job := <-jobFinished:
return job
case <-timeoutChan:
return nil
case <-q.ctx.Done():
return nil
}
}
}
// CancelJob cancels a job with the given job RID.
// It retrieves the job from the database and cancels it.
// If the job is not found or already cancelled, it returns an error.
func (q *Queuer) CancelJob(jobRid uuid.UUID) (*model.Job, error) {
job, err := q.dbJob.SelectJob(jobRid)
if err != nil {
return nil, helper.NewError("selecting job", err)
}
err = q.cancelJob(job)
if err != nil {
return nil, helper.NewError("cancelling job", err)
}
return job, nil
}
// CancelAllJobsByWorker cancels all jobs assigned to a specific worker by its RID.
// It retrieves all jobs assigned to the worker and cancels each one.
// It returns an error if something goes wrong during the process.
func (q *Queuer) CancelAllJobsByWorker(workerRid uuid.UUID, entries int) error {
jobs, err := q.dbJob.SelectAllJobsByWorkerRID(workerRid, 0, entries)
if err != nil {
return helper.NewError("selecting jobs", err)
}
for _, job := range jobs {
err := q.cancelJob(job)
if err != nil {
return helper.NewError("cancelling job", err)
}
}
return nil
}
// ReaddJobFromArchive readds a job from the archive back to the queue.
func (q *Queuer) ReaddJobFromArchive(jobRid uuid.UUID) (*model.Job, error) {
job, err := q.dbJob.SelectJobFromArchive(jobRid)
if err != nil {
return nil, helper.NewError("selecting job from archive", err)
}
// Readd the job to the queue
newJob, err := q.AddJobWithOptions(job.Options, job.TaskName, job.ParametersKeyed, job.Parameters...)
if err != nil {
return nil, helper.NewError("readding job", err)
}
q.log.Info("Job readded", slog.String("job_rid", newJob.RID.String()))
return newJob, nil
}
// DeleteJob deletes a job by its RID.
func (q *Queuer) DeleteJob(jobRid uuid.UUID) error {
err := q.dbJob.DeleteJob(jobRid)
if err != nil {
return helper.NewError("deleting job", err)
}
q.log.Info("Job deleted", slog.String("job_rid", jobRid.String()))
return nil
}
// GetJob retrieves a job by its RID.
func (q *Queuer) GetJob(jobRid uuid.UUID) (*model.Job, error) {
job, err := q.dbJob.SelectJob(jobRid)
if err != nil {
return nil, helper.NewError("selecting job", err)
}
return job, nil
}
// GetJobs retrieves all jobs in the queue.
func (q *Queuer) GetJobs(lastId int, entries int) ([]*model.Job, error) {
jobs, err := q.dbJob.SelectAllJobs(lastId, entries)
if err != nil {
return nil, helper.NewError("selecting all jobs", err)
}
return jobs, nil
}
// GetJobsBySearch retrieves jobs that match the given search term.
func (q *Queuer) GetJobsBySearch(search string, lastId int, entries int) ([]*model.Job, error) {
jobs, err := q.dbJob.SelectAllJobsBySearch(search, lastId, entries)
if err != nil {
return nil, helper.NewError("selecting jobs by search", err)
}
return jobs, nil
}
// GetJobsByWorkerRID retrieves jobs assigned to a specific worker by its RID.
func (q *Queuer) GetJobsByWorkerRID(workerRid uuid.UUID, lastId int, entries int) ([]*model.Job, error) {
jobs, err := q.dbJob.SelectAllJobsByWorkerRID(workerRid, lastId, entries)
if err != nil {
return nil, helper.NewError("selecting jobs by worker RID", err)
}
return jobs, nil
}
// GetJobEnded retrieves a job that has ended (succeeded, cancelled or failed) by its RID.
func (q *Queuer) GetJobEnded(jobRid uuid.UUID) (*model.Job, error) {
job, err := q.dbJob.SelectJobFromArchive(jobRid)
if err != nil {
return nil, helper.NewError("selecting ended job", err)
}
return job, nil
}
// GetJobsEnded retrieves all jobs that have ended (succeeded, cancelled or failed).
func (q *Queuer) GetJobsEnded(lastId int, entries int) ([]*model.Job, error) {
jobs, err := q.dbJob.SelectAllJobsFromArchive(lastId, entries)
if err != nil {
return nil, helper.NewError("selecting ended jobs", err)
}
return jobs, nil
}
// GetJobsEndedBySearch retrieves ended jobs that match the given search term.
func (q *Queuer) GetJobsEndedBySearch(search string, lastId int, entries int) ([]*model.Job, error) {
jobs, err := q.dbJob.SelectAllJobsFromArchiveBySearch(search, lastId, entries)
if err != nil {
return nil, helper.NewError("selecting ended jobs by search", err)
}
return jobs, nil
}
// Internal
// mergeOptions merges the worker options with optional job options.
func (q *Queuer) mergeOptions(options *model.Options) *model.Options {
q.workerMu.RLock()
workerOptions := q.worker.Options
q.workerMu.RUnlock()
if options != nil && options.OnError == nil {
options.OnError = workerOptions
} else if options == nil && workerOptions != nil {
options = &model.Options{OnError: workerOptions}
}
return options
}
// addJob adds a job to the queue with all necessary parameters.
func (q *Queuer) addJob(task interface{}, options *model.Options, parametersKeyed map[string]interface{}, parameters ...interface{}) (*model.Job, error) {
newJob, err := model.NewJob(task, options, parametersKeyed, parameters...)
if err != nil {
return nil, helper.NewError("creating job", err)
}
job, err := q.dbJob.InsertJob(newJob)
if err != nil {
return nil, helper.NewError("inserting job", err)
}
return job, nil
}
// addJobTx adds a job to the queue with all necessary parameters.
func (q *Queuer) addJobTx(tx *sql.Tx, task interface{}, options *model.Options, parametersKeyed map[string]interface{}, parameters ...interface{}) (*model.Job, error) {
newJob, err := model.NewJob(task, options, parametersKeyed, parameters...)
if err != nil {
return nil, helper.NewError("creating job", err)
}
job, err := q.dbJob.InsertJobTx(tx, newJob)
if err != nil {
return nil, helper.NewError("inserting job", err)
}
return job, nil
}
// runJobInitial is called to run the next job in the queue.
func (q *Queuer) runJobInitial() error {
// Update job status to running with worker.
q.workerMu.RLock()
worker := q.worker
q.workerMu.RUnlock()
jobs, err := q.dbJob.UpdateJobsInitial(worker)
if err != nil {
return helper.NewError("updating jobs initial", err)
} else if len(jobs) == 0 {
return nil
}
for _, job := range jobs {
if job.Options != nil && job.Options.Schedule != nil && job.Options.Schedule.Start.After(time.Now()) {
scheduler, err := core.NewScheduler(
&job.Options.Schedule.Start,
q.runJob,
job,
)
if err != nil {
return helper.NewError("creating scheduler", err)
}
q.log.Info("Scheduling job", slog.String("job_rid", job.RID.String()), slog.String("schedule_start", job.Options.Schedule.Start.String()))
go scheduler.Go(q.ctx)
} else {
go q.runJob(job)
}
}
return nil
}
// waitForJob executes the job and returns the results or an error.
func (q *Queuer) waitForJob(job *model.Job) (results []interface{}, cancelled bool, err error) {
// Run job and update job status to completed with results
// TODO At this point the task should be available in the queuer,
// but we should probably still check if the task is available?
task := q.tasks[job.TaskName]
runner, err := core.NewRunnerFromJob(task, job)
if err != nil {
return nil, false, helper.NewError("creating runner", err)
}
q.activeRunners.Store(job.RID, runner)
go runner.Run(q.ctx)
select {
case err = <-runner.ErrorChannel:
break
case results = <-runner.ResultsChannel:
break
case <-q.ctx.Done():
runner.Cancel()
return nil, true, nil
}
q.activeRunners.Delete(job.RID)
return results, false, err
}
// retryJob retries the job with the given job error.
func (q *Queuer) retryJob(job *model.Job, jobErr error) {
if job.Options == nil || job.Options.OnError == nil || job.Options.OnError.MaxRetries <= 0 {
q.failJob(job, jobErr)
return
}
var err error
var results []interface{}
retryer, err := core.NewRetryer(
func() error {
q.log.Debug("Trying/retrying job", slog.String("job_rid", job.RID.String()))
results, _, err = q.waitForJob(job)
if err != nil {
return helper.NewError("retrying job", err)
}
return nil
},
job.Options.OnError,
)
if err != nil {
jobErr = helper.NewError("creating retryer", fmt.Errorf("%v, job error: %v", err, jobErr))
}
err = retryer.Retry()
if err != nil {
q.failJob(job, helper.NewError("retrying job", fmt.Errorf("%v, job error: %v", err, jobErr)))
} else {
q.succeedJob(job, results)
}
}
// runJob retries the job.
func (q *Queuer) runJob(job *model.Job) {
q.log.Info("Running job", slog.String("job_rid", job.RID.String()))
results, cancelled, err := q.waitForJob(job)
if cancelled {
return
} else if err != nil {
q.retryJob(job, err)
} else {
q.succeedJob(job, results)
}
}
func (q *Queuer) cancelJob(job *model.Job) error {
if job.Status == model.JobStatusRunning || job.Status == model.JobStatusScheduled || job.Status == model.JobStatusQueued {
job.Status = model.JobStatusCancelled
_, err := q.dbJob.UpdateJobFinal(job)
if err != nil && err.(helper.Error).Original != sql.ErrNoRows {
q.log.Error("Error updating job status to cancelled", slog.String("error", err.Error()))
} else if err == nil {
q.log.Info("Job cancelled", slog.String("job_rid", job.RID.String()))
}
}
return nil
}
// succeedJob updates the job status to succeeded and runs the next job if available.
func (q *Queuer) succeedJob(job *model.Job, results []interface{}) {
job.Status = model.JobStatusSucceeded
job.Results = results
q.endJob(job)
}
func (q *Queuer) failJob(job *model.Job, jobErr error) {
job.Status = model.JobStatusFailed
job.Error = jobErr.Error()
q.endJob(job)
}
func (q *Queuer) endJob(job *model.Job) {
q.workerMu.RLock()
workerID := q.worker.ID
q.workerMu.RUnlock()
if job.WorkerID != workerID {
return
}
endedJob, err := q.dbJob.UpdateJobFinal(job)
if err != nil {
if strings.Contains(err.Error(), sql.ErrNoRows.Error()) {
return
}
q.log.Error("Error updating finished job", slog.String("status", job.Status), slog.String("error", err.Error()))
} else {
q.log.Debug("Job ended", slog.String("status", endedJob.Status), slog.String("rid", endedJob.RID.String()))
// Readd scheduled jobs to the queue
if endedJob.Options != nil && endedJob.Options.Schedule != nil && endedJob.ScheduleCount < endedJob.Options.Schedule.MaxCount {
var newScheduledAt time.Time
if len(endedJob.Options.Schedule.NextInterval) > 0 {
// This worker should only have the current job if the NextIntervalFunc is available.
nextIntervalFunc, ok := q.nextIntervalFuncs[endedJob.Options.Schedule.NextInterval]
if !ok {
q.log.Error("NextIntervalFunc not found", slog.String("name", endedJob.Options.Schedule.NextInterval), slog.String("job_rid", endedJob.RID.String()))
return
}
newScheduledAt = nextIntervalFunc(*endedJob.ScheduledAt, endedJob.ScheduleCount)
} else {
newScheduledAt = endedJob.ScheduledAt.Add(time.Duration(endedJob.ScheduleCount) * endedJob.Options.Schedule.Interval)
}
endedJob.ScheduledAt = &newScheduledAt
endedJob.Status = model.JobStatusScheduled
job, err := q.dbJob.InsertJob(endedJob)
if err != nil {
q.log.Error("Error readding scheduled job", slog.String("job_rid", endedJob.RID.String()), slog.String("error", err.Error()))
}
q.log.Info("Job added for next iteration to the queue", slog.String("job_rid", job.RID.String()))
}
}
// Try to run the next job in the queue
err = q.runJobInitial()
if err != nil {
q.log.Error("Error running next job", slog.String("error", err.Error()))
}
}