()
| 129 | |
| 130 | |
| 131 | export function bind(){ |
| 132 | let task: ScheduledTask; |
| 133 | |
| 134 | process.on('message', async (message: any) => { |
| 135 | switch(message.command){ |
| 136 | case 'task:start': |
| 137 | try { |
| 138 | task = await startDaemon(message); |
| 139 | } catch (error: any) { |
| 140 | // Report the failure to the parent so it can reject start() with the |
| 141 | // real cause, instead of crashing the daemon with an opaque exit. |
| 142 | if (process.send) process.send({ event: 'daemon:error', jsonError: serializeError(error) }); |
| 143 | } |
| 144 | return task; |
| 145 | case 'task:stop': |
| 146 | if(task) task.stop(); |
| 147 | return task; |
| 148 | case 'task:destroy': |
| 149 | if(task) task.destroy(); |
| 150 | return task; |
| 151 | case 'task:execute': |
| 152 | try { |
| 153 | if (task) await task.execute(); |
| 154 | } catch(error: any){ |
| 155 | logger.debug('Daemon task:execute falied:', error); |
| 156 | } |
| 157 | return task; |
| 158 | } |
| 159 | }); |
| 160 | |
| 161 | // When the parent dies the IPC channel disconnects. Exit instead of lingering |
| 162 | // as an orphan: an orphaned daemon would keep running the schedule on its own, |
| 163 | // and a distributed task's IPC coordination would hang with no parent to reply. |
| 164 | process.on('disconnect', () => process.exit(0)); |
| 165 | } |
| 166 | |
| 167 | bind(); |
no test coverage detected