import { FailoverStrategy } from "./config" ; import { MessageBroker } from "./message_broker" ; import { NullBackend } from "./null_backend" ; import { ResultBackend } from "./result_backend" ; import { Task } from "./task" ; /**
 * Celery client. Supports Redis and RabbitMQ message brokers and Redis and RPC
 * result backends.
 */ export class Client { private readonly backend : ResultBackend ; private readonly brokers : Array < MessageBroker > ; private readonly id : string ; private readonly taskDefaults : TaskDefaults ; private readonly failoverStrategy : FailoverStrategy ; param backend param brokers param failoverStrategy param id param taskDefaults returns public constructor ( { backend = new NullBackend ( ) , brokers , failoverStrategy = getRoundRobinStrategy ( brokers . length ) , id , taskDefaults = { } , } ) { this . brokers = brokers ; this . backend = backend ; this . id = id ; this . taskDefaults = taskDefaults ; this . failoverStrategy = failoverStrategy ; } param name returns public createTask < T > ( name ) { return new Task ( { ... this . taskDefaults , appId : this . id , backend : this . backend , brokers : this . brokers , failoverStrategy : this . failoverStrategy , name , } ) ; } /**
     * Forcefully disconnects from message brokers and the result backend.
     */ public async disconnect ( ) { await disconnectOrEndAll ( this . brokers , this . backend , "disconnect" ) ; } /**
     * Gently disconnect from message brokers and the result backend.
     */ public async end ( ) { await disconnectOrEndAll ( this . brokers , this . backend , "end" ) ; } } /**
 * Creational options for `Client`'s constructor.
 */ export interface ClientOptions { backend ? : ResultBackend ; brokers : Array < MessageBroker > ; failoverStrategy ? : FailoverStrategy ; id : string ; taskDefaults ? : TaskDefaults ; } /**
 * Default options passed to `Task`'s constructor.'
 */ export interface TaskDefaults { deliveryMode ? : "persistent" | "transient" ; hardTimeLimit ? : number ; queue ? : string ; softTimeLimit ? : number ; } param size returns export const getRoundRobinStrategy = ( size ) : FailoverStrategy => { let index = 0 ; return ( brokers ) : MessageBroker => { const broker = brokers [ index ] ; ++ index ; index %= size ; return broker ; } ; } ; param brokers param backend param toInvoke returns const disconnectOrEndAll = async ( brokers , backend , toInvoke ) : Promise < Array < void > > => Promise . all ( brokers . map ( ( b ) => b [ toInvoke ] ( ) ) . concat ( backend [ toInvoke ] ( ) ) ) ;