screwdriver-queue-service 2.0.38 → 2.0.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "screwdriver-queue-service",
3
- "version": "2.0.38",
3
+ "version": "2.0.41",
4
4
  "description": "Screwdriver Queue Service API",
5
5
  "main": "app.js",
6
6
  "directories": {
@@ -24,7 +24,6 @@
24
24
  "joi": "^17.5.0",
25
25
  "js-yaml": "^3.14.1",
26
26
  "jsonwebtoken": "^8.5.1",
27
- "laabr": "^6.1.3",
28
27
  "node-resque": "^5.5.3",
29
28
  "npm-auto-version": "^1.0.0",
30
29
  "redlock": "^4.2.0",
package/plugins/helper.js CHANGED
@@ -247,6 +247,29 @@ async function updateBuild(updateConfig, retryStrategyFn) {
247
247
  );
248
248
  }
249
249
 
250
+ /**
251
+ * Notify user with job status
252
+ * @param {Number} jobId
253
+ * @param {String} token
254
+ * @param {Object} payload
255
+ * @param {String} apiUri
256
+ * @param {Object} retryStrategyFn
257
+ */
258
+ async function notifyJob(notifyConfig, retryStrategyFn) {
259
+ const { token, apiUri, jobId, payload } = notifyConfig;
260
+
261
+ return request(formatOptions('POST', `${apiUri}/v4/jobs/${jobId}/notify`, token, payload, retryStrategyFn)).then(
262
+ res => {
263
+ logger.info(`POST /v4/jobs/${jobId}/notify completed with attempts, ${res.statusCode}, ${res.attempts}`);
264
+ if ([200, 201, 204].includes(res.statusCode)) {
265
+ return res.body;
266
+ }
267
+
268
+ throw new Error(`Could not notify job ${jobId} with ${res.statusCode}code and ${JSON.stringify(res.body)}`);
269
+ }
270
+ );
271
+ }
272
+
250
273
  /**
251
274
  * Post the webhooks process
252
275
  * @method processHooks
@@ -306,5 +329,6 @@ module.exports = {
306
329
  createBuildEvent,
307
330
  getPipelineAdmin,
308
331
  updateBuild,
332
+ notifyJob,
309
333
  processHooks
310
334
  };
@@ -29,51 +29,70 @@ const BLOCKED_BY_SAME_JOB_WAIT_TIME = 5;
29
29
  async function postBuildEvent(executor, eventConfig) {
30
30
  const { pipeline, job, apiUri, eventId, causeMessage, buildId } = eventConfig;
31
31
  const pipelineId = pipeline.id;
32
+ const jobId = job.id;
32
33
 
33
34
  try {
34
35
  const token = executor.tokenGen({
35
36
  pipelineId,
36
37
  service: 'queue',
37
- jobId: job.id,
38
+ jobId,
38
39
  scmContext: pipeline.scmContext,
39
40
  scope: ['user']
40
41
  });
41
42
 
42
43
  const admin = await helper.getPipelineAdmin(token, apiUri, pipelineId, helper.requestRetryStrategy);
43
44
 
44
- if (admin) {
45
- logger.info(
46
- `POST event for pipeline ${pipelineId}:${job.name}:${job.id}:${buildId} using user ${admin.username}`
47
- );
45
+ logger.info(
46
+ `POST event for pipeline ${pipelineId}:${job.name}:${job.id}:${buildId} using user ${admin.username}`
47
+ );
48
48
 
49
- const jwt = executor.userTokenGen(admin.username, {}, admin.scmContext);
49
+ const jwt = executor.userTokenGen(admin.username, {}, admin.scmContext);
50
50
 
51
- const buildEvent = {
52
- pipelineId,
53
- startFrom: job.name,
54
- creator: {
55
- name: 'Screwdriver scheduler',
56
- username: 'sd:scheduler'
57
- },
58
- causeMessage: causeMessage || 'Automatically started by scheduler'
59
- };
51
+ const buildEvent = {
52
+ pipelineId,
53
+ startFrom: job.name,
54
+ creator: {
55
+ name: 'Screwdriver scheduler',
56
+ username: 'sd:scheduler'
57
+ },
58
+ causeMessage: causeMessage || 'Automatically started by scheduler'
59
+ };
60
60
 
61
- if (eventId) {
62
- buildEvent.parentEventId = eventId;
63
- }
64
- if (buildId) {
65
- buildEvent.buildId = buildId;
66
- }
61
+ if (eventId) {
62
+ buildEvent.parentEventId = eventId;
63
+ }
64
+ if (buildId) {
65
+ buildEvent.buildId = buildId;
66
+ }
67
67
 
68
- await helper.createBuildEvent(apiUri, jwt, buildEvent, helper.requestRetryStrategyPostEvent);
69
- } else {
68
+ await helper.createBuildEvent(apiUri, jwt, buildEvent, helper.requestRetryStrategyPostEvent);
69
+ } catch (err) {
70
+ if (err.statusCode === 404) {
70
71
  logger.error(
71
72
  `POST event for pipeline failed as no admin found: ${pipelineId}:${job.name}:${job.id}:${buildId}`
72
73
  );
73
74
 
74
- throw new Error(`Pipeline admin not found, cannot process build ${buildId}`);
75
+ const pipelineToken = executor.tokenGen({
76
+ pipelineId,
77
+ service: 'queue',
78
+ scmContext: pipeline.scmContext,
79
+ scope: ['pipeline']
80
+ });
81
+
82
+ const status = 'FAILURE';
83
+ const message = `Pipeline ${pipelineId} does not have admin, unable to start job ${job.name}.`;
84
+
85
+ await helper.notifyJob(
86
+ {
87
+ token: pipelineToken,
88
+ apiUri,
89
+ jobId,
90
+ payload: { status, message }
91
+ },
92
+ helper.requestRetryStrategyPostEvent
93
+ );
75
94
  }
76
- } catch (err) {
95
+
77
96
  logger.error(`Error in post build event function ${buildId} ${err}`);
78
97
  throw err;
79
98
  }