All files / services/scheduler-service/src/temporal client.js

0% Statements 0/22
0% Branches 0/2
0% Functions 0/6
0% Lines 0/22

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                                                                                                                             
const { Client, Connection } = require('@temporalio/client');
const config = require('../config');
 
let temporalClient = null;
 
async function getTemporalClient() {
  if (!temporalClient) {
    const connection = await Connection.connect({ address: config.temporalAddress });
    temporalClient = new Client({ connection });
  }
  return temporalClient;
}
 
async function createSchedule(scheduleId, cronExpression, workflowArgs) {
  const client = await getTemporalClient();
  await client.schedule.create({
    scheduleId,
    spec: {
      cronExpressions: [cronExpression],
    },
    action: {
      type: 'startWorkflow',
      workflowType: 'CrawlerScheduleWorkflow',
      args: [workflowArgs],
      taskQueue: 'scheduler-activities',
      workflowExecutionTimeout: '1 hour',
    },
  });
}
 
async function pauseSchedule(scheduleId) {
  const client = await getTemporalClient();
  const handle = client.schedule.getHandle(scheduleId);
  await handle.pause();
}
 
async function unpauseSchedule(scheduleId) {
  const client = await getTemporalClient();
  const handle = client.schedule.getHandle(scheduleId);
  await handle.unpause();
}
 
async function deleteSchedule(scheduleId) {
  const client = await getTemporalClient();
  const handle = client.schedule.getHandle(scheduleId);
  await handle.delete();
}
 
async function triggerSchedule(scheduleId) {
  const client = await getTemporalClient();
  const handle = client.schedule.getHandle(scheduleId);
  await handle.trigger();
}
 
module.exports = {
  getTemporalClient,
  createSchedule,
  pauseSchedule,
  unpauseSchedule,
  deleteSchedule,
  triggerSchedule,
};