This repository has been archived by the owner on Dec 27, 2018. It is now read-only.
/
job.go
172 lines (139 loc) · 3.04 KB
/
job.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
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
// Copyright 2016 Marin Procureur. All rights reserved.
// Use of gjp source code is governed by a MIT license
// license that can be found in the LICENSE file.
/*
Package gjp stands for Go JobPool, and is willing to be a simple jobpool manager. It maintains
a number of queues determined at the init. No priority whatsoever, just every queues are
processing one job at a time.
*/
package gjp
import (
"encoding/json"
"fmt"
"time"
)
/*
TYPES
*/
type (
// Job
Job struct {
JobRunner `json:"-"` //skip the field for json
Id string `json:"id"`
Name string `json:"name"` //Public property retrievable
Status string `json:"status"` //Status of the current job
Error *JobError `json:"-"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
}
)
/*
JOB STATUS
*/
const (
failed string = "failed"
success string = "done"
waiting string = "queued"
processing string = "proceeded"
)
func newJob(id string, jobRunner JobRunner, jobName string) (job *Job, jobId string) {
job = &Job{
JobRunner: jobRunner,
Name: jobName,
Status: waiting,
Id: id,
}
fmt.Println("New job with Id : ", job.Id)
jobId = job.Id
return
}
//execute the job safely and set the status back for the reportChannel
func (j *Job) executeJob(start time.Time) {
defer catchPanic("Job", j.Name, "failed in executeJob")
//Set the execution time for this job
j.Start = start
j.setJobToProcessing()
go j.NotifyStart(j)
j.Error = j.ExecuteJob(j)
//Set the job status
switch j.Error {
case nil:
j.setJobToSuccess()
break
default:
j.setJobToError()
break
}
j.End = time.Now()
go j.NotifyEnd(j)
return
}
/*
GETTERS & SETTERS
*/
func (j *Job) HasJobErrored() (errored bool) {
fmt.Println("Has job", j.GetJobName(), "errored ? ", j.Error != nil)
if j.Error != nil {
errored = true
go j.NotifyEnd(j)
} else {
errored = false
}
return
}
//create an error well formated
func (j *Job) GetJobError() (errorString string) {
errorString = j.Error.FmtError()
return
}
func (j *Job) getJobStringId() (jobId string) {
jobId = j.Id
return
}
func (j *Job) jobErrored() (jobError bool, error string) {
if j.Status == failed {
jobError = true
error = j.GetJobError()
}
return
}
func (j *Job) GetJobStatus() (jobStatus string) {
jobStatus = j.Status
return
}
func (j *Job) getExecutionTime() (executionTime time.Duration) {
nullTime := time.Time{}
if j.End == nullTime {
executionTime = j.Start.Sub(j.End)
} else {
executionTime = time.Since(j.Start)
}
return
}
func (j *Job) GetJobInfos() (jobjson []byte, err error) {
jobjson, err = json.Marshal(j)
if err != nil {
fmt.Println(err.Error())
}
return
}
func (j *Job) GetJobId() (jobId string) {
jobId = j.Id
return
}
func (j *Job) GetJobName() (jobName string) {
jobName = j.Name
return
}
func (j *Job) setJobToWaiting() {
j.Status = waiting
}
func (j *Job) setJobToError() {
j.Status = failed
}
func (j *Job) setJobToSuccess() {
j.Status = success
}
func (j *Job) setJobToProcessing() {
j.Status = processing
}