import { Command } from 'clipanion'; import { c } from '#/di'; import { runMigrations } from '#/services/pg/migrations'; // di import "#/services/temporal" import { NativeConnection, Worker } from '@temporalio/worker'; import * as activities from '../activities'; import path from 'path'; import { Client, ScheduleNotFoundError, ScheduleOptions, ScheduleOverlapPolicy } from '@temporalio/client'; import { workflowSyncAllGuilds, workflowSyncGuilds, workflowSyncOnline, workflowSyncGuildLeaderboardInfo } from '#/workflows'; import { PG } from '#/services/pg'; import { config } from '#/config'; const schedules: ScheduleOptions[] = [ { scheduleId: "update-guild-players", action: { type: 'startWorkflow', workflowType: workflowSyncGuilds, taskQueue: 'wynn-worker', }, policies: { overlap: ScheduleOverlapPolicy.SKIP, }, spec: { intervals: [{ every: '15 minutes', }] }, }, { scheduleId: "update_guild_leaderboards", action: { type: 'startWorkflow', workflowType: workflowSyncGuildLeaderboardInfo, taskQueue: 'wynn-worker', }, policies: { overlap: ScheduleOverlapPolicy.SKIP, }, spec: { intervals: [{ every: '5 minutes', }] }, }, { scheduleId: "update-all-guilds", action: { type: 'startWorkflow', workflowType: workflowSyncAllGuilds, taskQueue: 'wynn-worker', }, policies: { overlap: ScheduleOverlapPolicy.SKIP, }, spec: { intervals: [{ every: '1 hour', }] }, }, { scheduleId: "update-online-players", action: { type: 'startWorkflow', workflowType: workflowSyncOnline, taskQueue: 'wynn-worker', }, policies: { overlap: ScheduleOverlapPolicy.SKIP, }, spec: { intervals: [{ every: '31 seconds', }] }, }, ] const addSchedules = async (c: Client) => { for(const o of schedules) { const handle = c.schedule.getHandle(o.scheduleId) try { const desc = await handle.describe(); console.log(desc) }catch(e: any){ if(e instanceof ScheduleNotFoundError) { await c.schedule.create(o) }else { throw e; } } } } export class WorkerCommand extends Command { static paths = [['worker']]; async execute() { const { db } = await c.getAsync(PG); await runMigrations(db); const client = await c.getAsync(Client); // schedules await addSchedules(client); const connection = await NativeConnection.connect({ address: config.TEMPORAL_HOSTPORT, }) const worker = await Worker.create({ connection, namespace: config.TEMPORAL_NAMESPACE, workflowsPath: require.resolve('../workflows'), bundlerOptions: { webpackConfigHook: (config)=>{ if(!config.resolve) config.resolve = {}; if(!config.resolve.alias) config.resolve.alias = {}; config.resolve!.alias = { "#":path.resolve(process.cwd(),'src/'), ...config.resolve!.alias, } return config; }}, taskQueue: 'wynn-worker', stickyQueueScheduleToStartTimeout: 5 * 1000, activities }); await worker.run(); console.log("worked.run exited"); await db.end(); await connection.close(); } }