-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
158 lines (133 loc) · 3.3 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
package main
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
type JobStatus byte
const (
Unknown JobStatus = iota
Finished
Stopped
Failed
InProgress
)
// Stringify job status
func (b JobStatus) String() string {
return []string{"unknown", "finished", "stopped", "failed", "inprogress"}[int(b)]
}
// Marshal job status type into string
func (b JobStatus) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString("\"")
buffer.WriteString(b.String())
buffer.WriteString("\"")
return buffer.Bytes(), nil
}
// Unmarshal string job status into type
func (b *JobStatus) UnmarshalJSON(data []byte) error {
var v string
json.Unmarshal(data, &v)
*b = map[string]JobStatus{
"unknown": Unknown,
"finished": Finished,
"stopped": Stopped,
"failed": Failed,
"inprogress": InProgress,
}[v]
return nil
}
type Job struct {
name string
dir string
p *Project
interrupt chan bool
params map[string]string
}
// Get status of job
func (b *Job) Status() JobStatus {
data, err := os.ReadFile(filepath.Join(b.dir, "status"))
if err != nil {
return Unknown
}
i, _ := strconv.Atoi(strings.TrimSpace(string(data)))
return JobStatus(byte(i))
}
// Set status of job
func (b *Job) SetStatus(s JobStatus) error {
return os.WriteFile(filepath.Join(b.dir, "status"), []byte(strconv.Itoa(int(s))), 0600)
}
// Get path to console output of job
func (b *Job) OutputPath() string {
return filepath.Join(b.dir, "console.log")
}
// Read content of console output of job
func (b *Job) ReadOutput() (string, error) {
data, err := os.ReadFile(b.OutputPath())
if err != nil {
return "", err
}
return string(data), nil
}
// Get start date of job
func (b *Job) StartDate() time.Time {
s, err := os.Stat(filepath.Join(b.dir, "start"))
if err != nil {
return time.UnixMicro(0)
}
return s.ModTime()
}
// Get end date of job
func (b *Job) EndDate() time.Time {
s, err := os.Stat(filepath.Join(b.dir, "status"))
if err != nil {
return time.UnixMicro(0)
}
return s.ModTime()
}
// Path to workspace
func (b *Job) WorkspacePath() string {
return filepath.Join(b.dir, "workspace")
}
// Make workspace directory
func (b *Job) MkWorkspace() error {
return os.MkdirAll(b.WorkspacePath(), 0755)
}
// Creates file for tracking the time of start
func (b *Job) LogStart() error {
file, err := os.OpenFile(filepath.Join(b.dir, "start"), os.O_RDONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
return file.Close()
}
// Path to artifact archive
func (b *Job) ArtifactPath() string {
return filepath.Join(b.dir, "workspace.tar.gz")
}
// Checks if jobs are equal
func (b *Job) Equals(other *Job) bool {
return b.p != nil && other.p != nil && b.p.name == other.p.name && b.name == other.name
}
// Sets params in expected format, exclude all params with incorrect format
func (b *Job) SetParams(params map[string]string) {
b.params = checkParams(params)
}
// Saves params into file
func (b *Job) SaveParams() error {
return saveParams(filepath.Join(b.dir, "params"), b.params)
}
// Loads params from file into map, if not found, leave method without drama
func (b *Job) LoadParams() {
b.params = loadParams(filepath.Join(b.dir, "params"))
}
func (b *Job) ArtifactSize() int64 {
stat, err := os.Stat(b.ArtifactPath())
if err != nil {
return -1
}
return stat.Size()
}