import { ServiceWorkerIPC } from './ServiceWorkerIPC' ; import { ShardManager } from '../sharding/ShardManager' ; import { IPCEvents } from '../util/constants' ; import { transformError } from '../util/util' ; import { IPCResult } from '../' ; export abstract class BaseServiceWorker { /** The worker's service name */ public readonly name : string ; /** The worker's IPC client */ public readonly ipc : ServiceWorkerIPC ; public constructor ( public manager ) { this . name = String ( process . env . SERVICE_NAME ) ; this . ipc = new ServiceWorkerIPC ( this , this . manager . ipcSocket ) ; process . on ( 'exit' , ( ) => { this . ipc . disconnect ( ) ; process . exit ( 0 ) ; } ) ; } public async init ( ) { await this . ipc . init ( ) ; await this . launch ( ) ; } /** Notify the master process that this service is ready (required) */ protected sendReady ( ) { return this . ipc . send ( { op : IPCEvents . READY , d : { name : this . name } } ) ; } /** Can be implemented to allow for graceful shutdown of the service */ public abstract shutdown ( ) ; abstract protected abstract launch ( ) ; abstract public abstract allClustersReady ( ) ; abstract public abstract allMembersCached ( ) ; abstract public abstract getStats ( ) ; public async eval ( script ) { return await eval ( script ) ; } /**
     * Is called when a SERVICE_COMMAND event is received.
     * If the event is receptive then an IPCResult must be returned.
     */ public async handleCommand ( data , receptive ) { if ( receptive ) return this . asError ( new Error ( 'This service is not set up to handle commands' ) ) ; } /** Formats data as a response to an IPC event or command */ public asResponse ( data ) { return { success : true , d : data } ; } /** Formats an error as a response to an IPC event or command */ public asError ( error ) { return { success : false , d : transformError ( error ) } ; } }