This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathjob_status.go
76 lines (67 loc) · 2.61 KB
/
job_status.go
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
// Copyright 2015 Alex Browne. All rights reserved.
// Use of this source code is governed by the MIT
// license, which can be found in the LICENSE file.
package jobs
import (
"github.com/garyburd/redigo/redis"
)
// Status represents the different statuses a job can have.
type Status string
const (
// StatusSaved is the status of any job that has been saved into the database but not yet queued
StatusSaved Status = "saved"
// StatusQueued is the status of any job that has been queued for execution but not yet selected
StatusQueued Status = "queued"
// StatusExecuting is the status of any job that has been selected for execution and is being delegated
// to some worker and any job that is currently being executed by some worker.
StatusExecuting Status = "executing"
// StatusFinished is the status of any job that has been successfully executed.
StatusFinished Status = "finished"
// StatusFailed is the status of any job that failed to execute and for which there are no remaining retries.
StatusFailed Status = "failed"
// StatusCancelled is the status of any job that was manually cancelled.
StatusCancelled Status = "cancelled"
// StatusDestroyed is the status of any job that has been destroyed, i.e. completely removed
// from the database.
StatusDestroyed Status = "destroyed"
)
// key returns the key used for the sorted set in redis which will hold
// all jobs with this status.
func (status Status) Key() string {
return "jobs:" + string(status)
}
// Count returns the number of jobs that currently have the given status
// or an error if there was a problem connecting to the database.
func (status Status) Count() (int, error) {
conn := redisPool.Get()
defer conn.Close()
return redis.Int(conn.Do("ZCARD", status.Key()))
}
// JobIds returns the ids of all jobs that have the given status, ordered by
// priority or an error if there was a problem connecting to the database.
func (status Status) JobIds() ([]string, error) {
conn := redisPool.Get()
defer conn.Close()
return redis.Strings(conn.Do("ZREVRANGE", status.Key(), 0, -1))
}
// Jobs returns all jobs that have the given status, ordered by priority or
// an error if there was a problem connecting to the database.
func (status Status) Jobs() ([]*Job, error) {
t := newTransaction()
jobs := []*Job{}
t.getJobsByIds(status.Key(), newScanJobsHandler(&jobs))
if err := t.exec(); err != nil {
return nil, err
}
return jobs, nil
}
// possibleStatuses is simply an array of all the possible job statuses.
var possibleStatuses = []Status{
StatusSaved,
StatusQueued,
StatusExecuting,
StatusFinished,
StatusFailed,
StatusCancelled,
StatusDestroyed,
}