This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
monitor.js
130 lines (108 loc) · 3.98 KB
/
monitor.js
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
/*
* Monitors TaskCluster messages and stores Crater results for later analysis.
*/
'use strict';
var debug = require('debug')(__filename.slice(__dirname.length + 1));
var Promise = require('promise');
var tc = require('taskcluster-client');
var fs = require('fs');
var db = require('./crater-db');
var assert = require('assert');
var util = require('./crater-util');
var defaultPulseCredentialsFile = "./pulse-credentials.json";
var defaultTcCredentialsFile = "./tc-credentials.json";
function main() {
var config = util.loadDefaultConfig();
var pulseCredentials = config.pulseCredentials;
var tcCredentials = config.tcCredentials;
db.connect(config).then(function(dbctx) {
var tcQueue = new tc.Queue({ credentials: tcCredentials });
var pulseListener = new tc.PulseListener({
prefetch: 50, // fetch 50 messages at a time
credentials: pulseCredentials,
queueName: "crater-monitor" // create a durable queue
});
var queueEvents = new tc.QueueEvents();
pulseListener.bind(queueEvents.taskDefined("route.crater.#"));
pulseListener.bind(queueEvents.taskPending("route.crater.#"));
pulseListener.bind(queueEvents.taskRunning("route.crater.#"));
pulseListener.bind(queueEvents.artifactCreated("route.crater.#"));
pulseListener.bind(queueEvents.taskCompleted("route.crater.#"));
pulseListener.bind(queueEvents.taskFailed("route.crater.#"));
pulseListener.bind(queueEvents.taskException("route.crater.#"));
pulseListener.on('message', function(m) {
debug("msg: " + JSON.stringify(m));
var taskId = m.payload.status.taskId;
var state = m.payload.status.state;
assert(taskId);
assert(state);
// Using a single db connection, don't clobber it with concurrency
util.serial(function() {
return new Promise(function(resolve, reject) {
recordResultForTask(dbctx, tcQueue, taskId, state, m);
resolve(null);
}).catch(function(e) { reject(e); })
})
});
pulseListener.resume().then(function() {
debug("listening");
});
}).catch(function(e) { console.log(e); });
}
function recordResultForTask(dbctx, tcQueue, taskId, state, m) {
// Get the task from TC
debug("requesting task for " + taskId);
var task = tcQueue.task(taskId);
task.then(function(task) {
debug("task: " + JSON.stringify(task));
var extra = task.extra.crater;
if (extra.taskType == "crate-build") {
var toolchain = extra.toolchain;
var crateName = extra.crateName;
var crateVers = extra.crateVers;
assert(toolchain);
assert(crateName);
assert(crateVers);
var status = "unknown";
if (state == "completed") {
status = "success";
} else if (state == "failed") {
status = "failure";
} else if (state == "exception") {
status = "exception";
} else /*if (state == "pending" || state == "running")*/ {
status = "unknown";
}
var buildResult = {
toolchain: toolchain,
crateName: crateName,
crateVers: crateVers,
status: status,
taskId: taskId
};
console.log("adding build result: " + JSON.stringify(buildResult));
return db.addBuildResult(dbctx, buildResult);
} else if (extra.taskType == "custom-build") {
if (state == "completed") {
debug("custom build success")
var run = m.payload.status.runs.length - 1;
var toolchain = util.parseToolchain(extra.toolchainGitSha);
var url = "https://queue.taskcluster.net/v1/task/" + taskId +
"/runs/" + run + "/artifacts/public/rustc-dev-x86_64-unknown-linux-gnu.tar.gz";
var custom = {
toolchain: toolchain,
url: url,
taskId: taskId
};
console.log("adding custom toolchain: " + JSON.stringify(custom));
return db.addCustomToolchain(dbctx, custom);
} else if (state == "failure" || state == "exception") {
console.log("custom toolchain build failed: " + taskId);
}
} else {
console.log("unknown task type " + extra.taskType);
return Promise.resolve();
}
}).catch(function(e) { console.log("error: " + e) });
}
main();