1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Softius\JenkinsJobMonitor; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Request; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class JobMonitor |
9
|
|
|
* @package Softius\JenkinsJobMonitor |
10
|
|
|
*/ |
11
|
|
|
class JobMonitor |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Job name as configured in Jenkins |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $jobName; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The name to be displayed rather than the build number |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $displayName; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Long description of the build |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $description; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* When the job was started running, in milliseconds |
33
|
|
|
* @var integer |
34
|
|
|
*/ |
35
|
|
|
private $startedOn; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Returns total duration of job execution, in milliseconds |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
private $duration; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Integer indicating the error code. 0 is success and everything else is failure |
45
|
|
|
* @var integer |
46
|
|
|
*/ |
47
|
|
|
private $exitCode; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Build log |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $log; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* JobMonitor constructor. |
57
|
|
|
* @param string $jobName |
58
|
|
|
* @param string $displayName |
59
|
|
|
* @param string $description |
60
|
|
|
*/ |
61
|
|
|
public function __construct($jobName, $displayName = null, $description = null) |
62
|
|
|
{ |
63
|
|
|
$this->jobName = $jobName; |
64
|
|
|
$this->displayName = $displayName; |
65
|
|
|
$this->description = $description; |
66
|
|
|
|
67
|
|
|
$this->exitCode = $this->startedOn = $this->duration = null; |
68
|
|
|
$this->clearLog(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Starts job monitoring |
73
|
|
|
*/ |
74
|
|
|
public function start() |
75
|
|
|
{ |
76
|
|
|
$this->startedOn = round(microtime(true) * 1000); |
|
|
|
|
77
|
|
|
$this->duration = null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Stops job monitoring |
82
|
|
|
* @param string $exitCode |
83
|
|
|
* @param string $log if log is provided, existing log data will be overwritten |
84
|
|
|
* @see setLog |
85
|
|
|
* @see appendLog |
86
|
|
|
*/ |
87
|
|
|
public function stop($exitCode, $log = null) |
88
|
|
|
{ |
89
|
|
|
$completedOn = round(microtime(true) * 1000); |
90
|
|
|
$this->setDuration(($completedOn - $this->startedOn)); |
91
|
|
|
$this->setExitCode($exitCode); |
92
|
|
|
if ($log) { |
|
|
|
|
93
|
|
|
$this->setLog($log); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param int $exitCode |
99
|
|
|
*/ |
100
|
|
|
public function setExitCode($exitCode) |
101
|
|
|
{ |
102
|
|
|
$this->exitCode = $exitCode; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns exit code |
107
|
|
|
* @return int |
108
|
|
|
*/ |
109
|
|
|
public function getExitCode() |
110
|
|
|
{ |
111
|
|
|
return $this->exitCode; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns true only and only this job is completed |
116
|
|
|
* @return boolean |
117
|
|
|
*/ |
118
|
|
|
public function isCompleted() |
119
|
|
|
{ |
120
|
|
|
return $this->duration !== null; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Returns true only and only this job is completed and successfull |
125
|
|
|
* @return boolean |
126
|
|
|
*/ |
127
|
|
|
public function isSuccessful() |
128
|
|
|
{ |
129
|
|
|
return $this->isCompleted() && $this->exitCode == 0; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param integer $duration Duration in milliseconds |
134
|
|
|
*/ |
135
|
|
|
public function setDuration($duration) |
136
|
|
|
{ |
137
|
|
|
$this->duration = $duration; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns total duration of job execution, in milliseconds |
142
|
|
|
* @return integer |
143
|
|
|
*/ |
144
|
|
|
public function getDuration() |
145
|
|
|
{ |
146
|
|
|
return $this->duration; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param $log |
151
|
|
|
*/ |
152
|
|
|
public function setLog($log) |
153
|
|
|
{ |
154
|
|
|
$this->log = $log; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Resets logs to empty string |
159
|
|
|
*/ |
160
|
|
|
public function clearLog() |
161
|
|
|
{ |
162
|
|
|
$this->log = null; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Appends provided log information to existing log |
167
|
|
|
* @param $log |
168
|
|
|
*/ |
169
|
|
|
public function appendLog($log) |
170
|
|
|
{ |
171
|
|
|
$this->log = $this->log.$log; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Returns the log of this job |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
public function getLog() |
179
|
|
|
{ |
180
|
|
|
return $this->log; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
private function getRequestBody() |
187
|
|
|
{ |
188
|
|
|
$xmlTemplate = '<run><log encoding="hexBinary">%s</log><result>%d</result>%s</run>'; |
189
|
|
|
$encodedLog = current(unpack('H*', $this->log)); |
190
|
|
|
$xmlElements = null; |
191
|
|
|
if ($this->isCompleted()) { |
192
|
|
|
$xmlElements .= sprintf('<duration>%d</duration>', $this->getDuration()); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if ($this->displayName !== null) { |
196
|
|
|
$xmlElements .= sprintf('<displayName>%s</displayName>', $this->displayName); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if ($this->description !== null) { |
200
|
|
|
$xmlElements .= sprintf('<description>%s</description>', $this->description); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$xmlRequest = sprintf($xmlTemplate, $encodedLog, $this->exitCode, $xmlElements); |
204
|
|
|
return $xmlRequest; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Constructs and returns the URI, where the monitor data must sent |
209
|
|
|
* |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
|
|
private function getRequestUri() |
213
|
|
|
{ |
214
|
|
|
return sprintf('/job/%s/postBuildResult', $this->jobName); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Constructs and returns the Monitor request to be to Jenkins |
219
|
|
|
* |
220
|
|
|
* @return Psr\Http\Message\RequestInterface |
221
|
|
|
*/ |
222
|
|
|
public function getRequest() |
223
|
|
|
{ |
224
|
|
|
return new Request('POST', $this->getRequestUri(), [], $this->getRequestBody()); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.