Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
startListening(bindings) {
// Get bindings if none are provided
if (!bindings || bindings.length === 0) {
return Promise.resolve();
}
// If not listening start listening
if (!this.__listener) {
assert(this.__bindings.length === 0, 'Hmm... check stopListening');
// Create listener
this.__listener = new taskcluster.WebListener();
this.__listener.on('message', this.handleMessage);
this.__listener.on('error', err => {
debug('Error while listening: %s, %j', err, err);
this.setState({listeningError: err || new Error('Unknown error')});
this.stopListening();
});
// Bind to bindings
const bound = bindings.map(binding => {
this.__bindings.push(binding);
return this.__listener.bind(binding);
});
this.setState({
listening: null,
listeningError: null,
createTaskListener(taskId) {
if (this.taskListener) {
this.taskListener.close();
this.taskListener = null;
}
if (!taskId) {
return;
}
const { queueEvents } = this.props;
const listener = new WebListener();
const routingKey = { taskId };
['taskDefined', 'taskPending', 'taskRunning', 'artifactCreated', 'taskCompleted', 'taskFailed', 'taskException']
.map(binding => listener.bind(queueEvents[binding](routingKey)));
listener.on('message', this.handleTaskMessage);
listener.resume();
this.taskListener = listener;
return listener;
}
export const queueEvents = new taskcluster.QueueEvents();
/**
* Authentication
*/
export const authentication = {
login: credentials => {
queue = new taskcluster.Queue({credentials});
},
};
/**
* WebListener
* Setup bindings and event callbacks
*/
let listener = new taskcluster.WebListener();
export const webListener = {
startListening: (taskGroupId, onMessageAction) => {
const qkey = {taskGroupId};
listener.bind(queueEvents.taskDefined(qkey));
listener.bind(queueEvents.taskPending(qkey));
listener.bind(queueEvents.taskRunning(qkey));
listener.bind(queueEvents.artifactCreated(qkey));
listener.bind(queueEvents.taskCompleted(qkey));
listener.bind(queueEvents.taskFailed(qkey));
listener.bind(queueEvents.taskException(qkey));
listener.on('message', message => {
onMessageAction(message);
});
stopListening() {
listener.close();
listener = new taskcluster.WebListener();
},
};