|
| 1 | + |
| 2 | +/** |
| 3 | + * Class for getting notifications about tasks' results. |
| 4 | + */ |
| 5 | +class RemoteJobConnector { |
| 6 | + /** |
| 7 | + * Constructor. |
| 8 | + * @param {RedisClient} redisClient redis client |
| 9 | + * @param {string} wfId workflow ID |
| 10 | + * @param {number} checkInterval loop interval in ms. |
| 11 | + */ |
| 12 | + constructor(redisClient, wfId, checkInterval) { |
| 13 | + this.jobPromiseResolves = {}; |
| 14 | + this.rcl = redisClient; |
| 15 | + this.running = false; |
| 16 | + this.completedNotificationQueueKey = "wf:" + wfId + ":tasksPendingCompletionHandling"; |
| 17 | + this.checkInterval = checkInterval; |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Gives promise, that will be resolved on remote |
| 22 | + * job completion. |
| 23 | + * @param {*} taskId task ID |
| 24 | + */ |
| 25 | + waitForTask(taskId) { |
| 26 | + if (this.jobPromiseResolves[taskId] !== undefined) { |
| 27 | + console.error("[RemoteJobConnector] Task", taskId, "is already observed"); |
| 28 | + return; |
| 29 | + } |
| 30 | + console.log("[RemoteJobConnector] Waiting for task", taskId); |
| 31 | + let promise = new Promise((resolve, reject) => { |
| 32 | + this.jobPromiseResolves[taskId] = resolve; |
| 33 | + }); |
| 34 | + |
| 35 | + return promise; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Runs connector, that fetches notifications about |
| 40 | + * task completions, then makes relevant waiting promises |
| 41 | + * resolved. |
| 42 | + */ |
| 43 | + async run() { |
| 44 | + this.running = true; |
| 45 | + while (true) { |
| 46 | + if (this.running == false) { |
| 47 | + console.log("[RemoteJobConnector] Stopping"); |
| 48 | + break; |
| 49 | + } |
| 50 | + |
| 51 | + let taskId = null; |
| 52 | + try { |
| 53 | + taskId = await new Promise((resolve, reject) => { |
| 54 | + this.rcl.srandmember(this.completedNotificationQueueKey, function(err, reply) { |
| 55 | + err ? reject(err): resolve(reply); |
| 56 | + }); |
| 57 | + }); |
| 58 | + } catch (error) { |
| 59 | + console.error("[RemoteJobConnector] Unable to fetch new complated jobs", error); |
| 60 | + } |
| 61 | + |
| 62 | + if (taskId == null) { |
| 63 | + await new Promise((resolve) => setTimeout(resolve, this.checkInterval)); |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + console.log("[RemoteJobConnector] Got completed job:", taskId); |
| 68 | + |
| 69 | + let taskResult = null; |
| 70 | + try { |
| 71 | + taskResult = await new Promise((resolve, reject) => { |
| 72 | + this.rcl.spop(taskId, function(err, reply) { |
| 73 | + /* Wrap results into array to preserve |
| 74 | + * compatibility with blpop format. */ |
| 75 | + let replyArr = [null, reply]; |
| 76 | + err ? reject(err): resolve(replyArr); |
| 77 | + }); |
| 78 | + }); |
| 79 | + } catch (error) { |
| 80 | + console.error("[RemoteJobConnector] Unable to get result of job", taskId); |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + if (this.jobPromiseResolves[taskId] === undefined) { |
| 85 | + console.error("[RemoteJobConnector] Observer for task", taskId, "not found"); |
| 86 | + continue; |
| 87 | + } |
| 88 | + let promiseResolve = this.jobPromiseResolves[taskId]; |
| 89 | + delete this.jobPromiseResolves[taskId]; |
| 90 | + |
| 91 | + try { |
| 92 | + await new Promise((resolve, reject) => { |
| 93 | + this.rcl.srem(this.completedNotificationQueueKey, taskId, function(err, reply) { |
| 94 | + err ? reject(err): resolve(reply); |
| 95 | + }); |
| 96 | + }); |
| 97 | + } catch (error) { |
| 98 | + console.error("[RemoteJobConnector] Unable to delete job from completed queue", error); |
| 99 | + } |
| 100 | + |
| 101 | + console.log("[RemoteJobConnector] Resolving promise for task", taskId, "| result =", taskResult); |
| 102 | + promiseResolve(taskResult); |
| 103 | + } |
| 104 | + |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Stops connector. |
| 110 | + */ |
| 111 | + async stop() { |
| 112 | + console.log("[RemoteJobConnector] Requesting stop"); |
| 113 | + this.running = false; |
| 114 | + return; |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +module.exports = RemoteJobConnector |
0 commit comments