@5minds/processcube_app_sdk
Version:
The SDK for ProcessCube Apps
6 lines (3 loc) • 2.61 kB
JavaScript
var a=require("node:process"),p=require("@5minds/processcube_engine_client"),f=require("@5minds/processcube_engine_sdk"),i=new f.Logger("processcube_app_sdk:external_task_worker_process"),d=process.env.PROCESSCUBE_ENGINE_URL??null,e=null,c;process.on("message",async r=>{switch(r.action){case"create":await w(r.payload);break;case"restart":T(r.payload);break;case"updateIdentity":E(r.payload);break}});process.once("SIGTERM",()=>{k("External Task Worker Process received SIGTERM signal")});process.once("SIGINT",()=>{k("External Task Worker Process received SIGINT signal")});process.once("SIGHUP",()=>{k("External Task Worker Process received SIGHUP signal")});process.once("disconnect",()=>{k("External Task Worker Process received disconnect signal")});async function w({topic:r,identity:t,moduleString:o,workerPath:n,workerId:s}){c=r;let l=await m(o,n);l===null&&process.exit(1),"default"in l||(i.error(`Failed starting external task worker ${e?.workerId??""} for topic ${r}. Please export a default handler function. For more information see https://processcube.io/docs/app-sdk/samples/nextjs/external-task-adapter-with-nextjs#external-tasks-entwickeln`,{workerId:e?.workerId,topic:r,pid:a.pid}),process.exit(2));let I={identity:t,...l?.config,workerId:s},g=l.default;u(d,"EngineURL"),e=new p.ExternalTaskWorker(d,r,g,I),e.onWorkerError(y=>{u(e,"externalTaskWorker"),i.error(`External task worker ${e.workerId} for topic ${r} ran into an error`,{error:y,workerId:e.workerId,topic:r,pid:a.pid}),e.stop(),e.dispose(),process.exit(3)}),e.start(),i.info(`Started external task worker ${e.workerId} for topic ${r}`,{workerId:e.workerId,topic:r,pid:a.pid})}function T({topic:r,identity:t,moduleString:o,workerPath:n}){let s=e?.workerId;i.info(`Restarting external task worker ${s??""} for topic ${r}`,{reason:`Code changes in External Task for ${r}`,workerId:s,topic:r,pid:a.pid}),x("External Task Worker restarting"),w({topic:r,identity:t,moduleString:o,workerPath:n,workerId:s})}function E({identity:r}){e&&(e.identity=r)}function k(r){x(r),process.exit()}function x(r){i.info(`Stopping external task worker ${e?.workerId??""} for topic ${c}`,{reason:r,workerId:e?.workerId,topic:c,pid:a.pid}),e?.stop(),e?.dispose()}function m(r,t){try{var o=module.constructor,n=new o;return n._compile(r,t),n.exports}catch(s){return i.error("Could not start external task worker due to error while requiring module from string",{err:s,pid:a.pid}),null}}function u(r,t,o){if(r==null){let n=o==null?"":`
Context:
${JSON.stringify(o,null,2)}`;throw new Error(`Unexpected value: \`${t}\` should not be null here.${n}`)}}