@5minds/processcube_app_sdk
Version:
The SDK for ProcessCube Apps
6 lines (3 loc) • 3.88 kB
JavaScript
var o=require("node:process"),T=require("@5minds/processcube_engine_client"),y=require("@5minds/processcube_engine_sdk"),s=new y.Logger("processcube_app_sdk:external_task_worker_process"),g=process.env.PROCESSCUBE_ENGINE_URL??null,r=null,f,l=0,E=!1,_=6,I=parseInt(process.env.PROCESSCUBE_APP_SDK_ETW_RETRY??"",10)||_,$=3e4,m,h,S;function W(e){if(!(e instanceof Error))return!1;let n=e.code;return["ECONNREFUSED","ECONNRESET","ETIMEDOUT","ENOTFOUND","EAI_AGAIN"].includes(n)||e.message?.includes("socket hang up")||e.message?.includes("fetch failed")}process.on("message",e=>{(async()=>{try{switch(e.action){case"create":await p(e.payload);break;case"restart":await O(e.payload);break;case"updateIdentity":U(e.payload);break}}catch(n){s.error("Error handling IPC message",{action:e.action,error:n,pid:o.pid})}})()});process.once("SIGTERM",()=>{u("External Task Worker Process received SIGTERM signal")});process.once("SIGINT",()=>{u("External Task Worker Process received SIGINT signal")});process.once("SIGHUP",()=>{u("External Task Worker Process received SIGHUP signal")});process.once("disconnect",()=>{u("External Task Worker Process received disconnect signal")});process.on("unhandledRejection",e=>{s.error("Unhandled Promise Rejection in External Task Worker Process",{reason:e,pid:o.pid})});process.on("uncaughtException",e=>{s.error("Uncaught Exception in External Task Worker Process",{error:e,pid:o.pid}),process.exit(4)});async function p({topic:e,identity:n,moduleString:t,workerPath:a,workerId:c}){f=e,m=n,h=t,S=a;let d=await M(t,a);d===null&&process.exit(1),"default"in d||(s.error(`Failed starting external task worker ${r?.workerId??""} for topic ${e}. 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:r?.workerId,topic:e,pid:o.pid}),process.exit(2));let N={identity:n,...d?.config,workerId:c},P=d.default;x(g,"EngineURL"),r=new T.ExternalTaskWorker(g,e,P,N),r.onWorkerError((k,i,R)=>{x(r,"externalTaskWorker"),s.error(`External task worker ${r.workerId} for topic ${e} ran into an error`,{errorType:k,error:i instanceof Error?{message:i.message,name:i.name,stack:i.stack,...i.code&&{code:i.code}}:i,externalTaskId:R?.id,workerId:r.workerId,topic:e,pid:o.pid}),r.stop(),r.dispose(),r=null,w(i,e)});try{r.start()}catch(k){s.error(`Failed to start external task worker for topic ${e}`,{error:k,pid:o.pid}),r.stop(),r.dispose(),r=null,w(k,e);return}E||(l=0),E=!1,s.info(`Started external task worker ${r.workerId} for topic ${e}`,{workerId:r.workerId,topic:e,pid:o.pid})}function w(e,n){if(W(e)&&l<I){l++,E=!0;let t=Math.min(1e3*Math.pow(2,l-1),$);s.info(`Connection error for topic ${n}, retrying in ${t}ms (attempt ${l}/${I})`,{topic:n,pid:o.pid,delay:t,attempt:l}),setTimeout(()=>{p({topic:n,identity:m,moduleString:h,workerPath:S})},t);return}process.exit(3)}async function O({topic:e,identity:n,moduleString:t,workerPath:a}){let c=r?.workerId;s.info(`Restarting external task worker ${c??""} for topic ${e}`,{reason:`Code changes in External Task for ${e}`,workerId:c,topic:e,pid:o.pid}),C("External Task Worker restarting"),await p({topic:e,identity:n,moduleString:t,workerPath:a,workerId:c})}function U({identity:e}){r&&(r.identity=e)}function u(e){C(e),process.exit()}function C(e){s.info(`Stopping external task worker ${r?.workerId??""} for topic ${f}`,{reason:e,workerId:r?.workerId,topic:f,pid:o.pid}),r?.stop(),r?.dispose()}function M(e,n){try{var t=module.constructor,a=new t;return a._compile(e,n),a.exports}catch(c){return s.error("Could not start external task worker due to error while requiring module from string",{err:c,pid:o.pid}),null}}function x(e,n,t){if(e==null){let a=t==null?"":`
Context:
${JSON.stringify(t,null,2)}`;throw new Error(`Unexpected value: \`${n}\` should not be null here.${a}`)}}