Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 75x 75x 75x 75x 1x 1x 1x 1x 1x 13x 13x 12x 12x 12x 12x 12x 12x 13x 1x 1x 1x 75x 75x 75x 75x 75x 75x 75x 75x 1x 12x 12x 12x 12x 12x 12x 12x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 232x 507x 507x 507x 232x 232x 75x 75x 232x 232x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 1x 1x | /**
* WorkflowWatcher — suivi temps réel de l'état d'un workflow via son historique Temporal.
*
* Principe (PULL, sans coopération du workflow) :
* - un long-poll par workflow en cours : getWorkflowExecutionHistory({ waitNewEvent, skipArchival })
* - à chaque WORKFLOW_TASK_COMPLETED, on interroge la query `getStatus` du workflow (état exact)
* et on émet la progression via les hooks
* - à l'événement de clôture, on émet la fin via le hook onClose
*
* Le worker Temporal du job-manager n'est pas nécessaire pour ce mécanisme.
*/
const { Connection } = require('@temporalio/client');
const { getTemporalClient } = require('./temporal/client');
const ALL_EVENT = 1; // HistoryEventFilterType.ALL_EVENT
const EVT = {
WORKFLOW_EXECUTION_COMPLETED: 2,
WORKFLOW_EXECUTION_FAILED: 3,
WORKFLOW_EXECUTION_TIMED_OUT: 4,
WORKFLOW_TASK_COMPLETED: 7,
WORKFLOW_EXECUTION_CANCELED: 21,
WORKFLOW_EXECUTION_TERMINATED: 27,
WORKFLOW_EXECUTION_CONTINUED_AS_NEW: 28,
};
const TERMINAL_EVENTS = new Set([
EVT.WORKFLOW_EXECUTION_COMPLETED,
EVT.WORKFLOW_EXECUTION_FAILED,
EVT.WORKFLOW_EXECUTION_TIMED_OUT,
EVT.WORKFLOW_EXECUTION_CANCELED,
EVT.WORKFLOW_EXECUTION_TERMINATED,
EVT.WORKFLOW_EXECUTION_CONTINUED_AS_NEW,
]);
// jobId -> { stopped }
const watches = new Map();
let hooks = {
onProgress: async () => {},
onClose: async () => {},
};
function setHooks(h) {
hooks = { ...hooks, ...h };
}
let _client = null;
async function temporalClient() {
if (!_client) _client = await getTemporalClient();
return _client;
}
function sleep(ms) {
return new Promise((r) => setTimeout(r, ms));
}
/**
* Arme le suivi d'un workflow (idempotent).
*/
function arm({ jobId }) {
if (!jobId || watches.has(jobId)) return false;
const state = { stopped: false };
watches.set(jobId, state);
console.log(`[Watcher] armed for ${jobId}`);
run(jobId, state).catch((e) => {
console.error(`[Watcher] ${jobId} fatal: ${e.message}`);
watches.delete(jobId);
});
return true;
}
function disarm(jobId) {
const state = watches.get(jobId);
if (state) state.stopped = true;
watches.delete(jobId);
}
function armedJobs() {
return Array.from(watches.keys());
}
async function emitProgress(jobId) {
try {
const client = await temporalClient();
const handle = client.workflow.getHandle(jobId);
const status = await handle.query('getStatus');
await hooks.onProgress(jobId, status?.status, status?.progress);
} catch (e) {
// query indisponible (workflow pas encore prêt ou en cours de clôture) : on ignore
}
}
async function run(jobId, state) {
const address = process.env.TEMPORAL_ADDRESS || 'temporal:7233';
let nextPageToken;
let terminal = false;
let connection = null;
while (!state.stopped && !terminal) {
try {
if (!connection) connection = await Connection.connect({ address });
const resp = await connection.workflowService.getWorkflowExecutionHistory({
namespace: 'default',
execution: { workflowId: jobId },
waitNewEvent: true,
historyEventFilterType: ALL_EVENT,
skipArchival: true,
nextPageToken,
});
nextPageToken = resp.nextPageToken;
let sawWorkflowTaskCompleted = false;
for (const ev of resp.history?.events || []) {
if (ev.eventType === EVT.WORKFLOW_TASK_COMPLETED) sawWorkflowTaskCompleted = true;
if (TERMINAL_EVENTS.has(ev.eventType)) terminal = true;
}
if (sawWorkflowTaskCompleted && !terminal) {
await emitProgress(jobId);
}
} catch (e) {
console.warn(`[Watcher] ${jobId} poll error: ${e.message}`);
try {
if (connection) connection.close();
} catch (_) {}
connection = null;
await sleep(1000);
}
}
try {
if (connection) connection.close();
} catch (_) {}
watches.delete(jobId);
if (terminal && !state.stopped) {
try {
await hooks.onClose(jobId);
} catch (e) {
console.error(`[Watcher] ${jobId} onClose error: ${e.message}`);
}
}
console.log(`[Watcher] stopped for ${jobId}${terminal ? ' (closed)' : ''}`);
}
module.exports = { setHooks, arm, disarm, armedJobs };
|