import { v4 } from "uuid" ; import Base from "./base" ; import Task from "./task" ; import { AsyncResult } from "./result" ; class TaskMessage { constructor ( readonly headers , readonly properties , readonly body , readonly sentEvent ) { } } export default class Client extends Base { private taskProtocols = { 1 : this . asTaskV1 , 2 : this . asTaskV2 } ; get createTaskMessage ( ) : ( ... args : any [ ] ) => TaskMessage { return this . taskProtocols [ this . conf . TASK_PROTOCOL ] ; } public sendTaskMessage ( taskName , message ) { const { headers , properties , body } = message ; const exchange = "" ; this . isReady ( ) . then ( ( ) => this . broker . publish ( body , exchange , this . conf . CELERY_QUEUE , headers , properties ) ) ; } public asTaskV2 ( taskId , taskName , args ? , kwargs ? ) { const message = { headers : { lang : "js" , task : taskName , id : taskId } , properties : { correlationId : taskId , replyTo : "" } , body : [ args , kwargs , { } ] , sentEvent : null } ; return message ; } function returns { String } public asTaskV1 ( taskId , taskName , args ? , kwargs ? ) { const message = { headers : { } , properties : { correlationId : taskId , replyTo : "" } , body : { task : taskName , id : taskId , args : args , kwargs : kwargs } , sentEvent : null } ; return message ; } method param { string } name returns { Task } example public createTask ( name ) { return new Task ( this , name ) ; } param { string } taskId returns { AsyncResult } public asyncResult ( taskId ) { return new AsyncResult ( taskId , this . backend ) ; } public sendTask ( taskName , args ? , kwargs ? , taskId ? ) { taskId = taskId || v4 ( ) ; const message = this . createTaskMessage ( taskId , taskName , args , kwargs ) ; this . sendTaskMessage ( taskName , message ) ; const result = new AsyncResult ( taskId , this . backend ) ; return result ; } }