UNPKG

node-beanstalk

Version:

The most comprehensive beanstalk client for nodejs

295 lines (294 loc) 11.6 kB
/// <reference types="node" /> import EventEmitter from 'events'; import { BeanstalkJobState, IBeanstalkJobStats, IBeanstalkStats, IBeanstalkTubeStats, IClientCtorOptions, IClientRawReservedJob } from './types'; import { Connection } from './Connection'; export declare class Client extends EventEmitter { private _conn; private readonly _opt; private readonly _queue; constructor(options?: IClientCtorOptions, connection?: Connection); /** * Indicates whether client is waiting for server response. */ get isWorking(): boolean; /** * Amount of requests waiting in queue, including connect and disconnect. */ get queueSize(): number; /** * Indicates whether client is connected to the server. */ get isConnected(): boolean; /** * Establish connection to the server * * @category Client */ connect(): Promise<void>; /** * Disconnect the client from server after all pending requests performed. * * If {force} set to truthy value - only currently running request will be awaited. * * @category Client */ disconnect(force?: boolean): Promise<void>; /** * Subsequent put commands will put jobs into the tube specified by this command. If no use * command has been issued, jobs will be put into the tube named "default". * * @category Producer Commands */ use(tubeName: string): Promise<string>; /** * This command for any process that wants to insert a job into the queue. * * @param payload - Payload of the job. Non string or integer values will be serialized with * [[IClientCtorOptions.serializer]]. Byte size of payload should be less than less than server's * max-job-size (default: 2**16) and client's [[IClientCtorOptions.maxPayloadSize]]. * * @param ttr - Time to run -- is an integer number of seconds to allow a worker * to run this job. This time is counted from the moment a worker reserves * this job. If the worker does not delete, release, or bury the job within * <ttr> seconds, the job will time out and the server will release the job. * The minimum ttr is 1. Maximum ttr is 2**32-1. * * @param priority - Integer < 2**32. Jobs with smaller priority values will be * scheduled before jobs with larger priorities. The most urgent priority is 0; * the least urgent priority is 4,294,967,295. * * @param delay - Integer number of seconds to wait before putting the job in * the ready queue. The job will be in the "delayed" state during this time. * Maximum delay is 2**32-1. * * @category Producer Commands */ put(payload: any, ttr?: number, priority?: number, delay?: number): Promise<{ id: number; state: BeanstalkJobState.buried | BeanstalkJobState.ready | BeanstalkJobState.delayed; }>; /** * This will return a newly-reserved job. If no job is available to be reserved, * beanstalkd will wait to send a response until one becomes available. Once a * job is reserved for the client, the client has limited time to run (TTR) the * job before the job times out. When the job times out, the server will put the * job back into the ready queue. Both the TTR and the actual time left can be * found in response to the [[Client.statsJob]] command. * * If more than one job is ready, beanstalkd will choose the one with the * smallest priority value. Within each priority, it will choose the one that * was received first. * * During the TTR of a reserved job, the last second is kept by the server as a * safety margin, during which the client will not be made to wait for another * job. If the client issues a reserve command during the safety margin, or if * the safety margin arrives while the client is waiting on a reserve command, * the server will respond with: DEADLINE_SOON * * This gives the client a chance to delete or release its reserved job before * the server automatically releases it. * * @category Worker Commands */ reserve(): Promise<null | IClientRawReservedJob>; /** * Same as [[Client.reserve]] but with limited amount of time to wait for the job. * * A timeout value of 0 will cause the server to immediately return either a * response or TIMED_OUT. A positive value of timeout will limit the amount of * time the client will block on the reserve request until a job becomes * available. * * @category Worker Commands */ reserveWithTimeout(timeout: number): Promise<null | IClientRawReservedJob>; /** * A job can be reserved by its id. Once a job is reserved for the client, * the client has limited time to run (TTR) the job before the job times out. * When the job times out, the server will put the job back into the ready queue. * * @category Worker Commands */ reserveJob(jobId: number): Promise<null | IClientRawReservedJob>; /** * The delete command removes a job from the server entirely. It is normally used * by the client when the job has successfully run to completion. A client can * delete jobs that it has reserved, ready jobs, delayed jobs, and jobs that are * buried. * * @category Worker Commands */ delete(jobId: number): Promise<boolean>; /** * The release command puts a reserved job back into the ready queue (and marks * its state as "ready") to be run by any client. It is normally used when the job * fails because of a transitory error. * * @param jobId - job id to release. * @param priority - a new priority to assign to the job. * @param delay - integer number of seconds to wait before putting the job in * the ready queue. The job will be in the "delayed" state during this time. * * @category Worker Commands */ release(jobId: number, priority?: number, delay?: number): Promise<null | BeanstalkJobState.buried | BeanstalkJobState.ready | BeanstalkJobState.delayed>; /** * The bury command puts a job into the "buried" state. Buried jobs are put into a * FIFO linked list and will not be touched by the server again until a client * kicks them with the [[Client.kick]] command * * @param jobId - job id to bury. * @param priority - a new priority to assign to the job. * * @category Worker Commands */ bury(jobId: number, priority?: number): Promise<boolean>; /** * The "touch" command allows a worker to request more time to work on a job. * This is useful for jobs that potentially take a long time, but you still want * the benefits of a TTR pulling a job away from an unresponsive worker. A worker * may periodically tell the server that it's still alive and processing a job * (e.g. it may do this on DEADLINE_SOON). The command postpones the auto * release of a reserved job until TTR seconds from when the command is issued * * @category Worker Commands */ touch(jobId: number): Promise<boolean>; /** * The "watch" command adds the named tube to the watch list for the current * connection. A reserve command will take a job from any of the tubes in the * watch list. For each new connection, the watch list initially consists of one * tube, named "default". * * @category Worker Commands */ watch(tubeName: string): Promise<number>; /** * Removes the named tube from the watch list for the current connection. * * False returned in case of attempt to ignore last tube watched * (`NOT_IGNORED` returned from server). * * @category Worker Commands */ ignore(tubeName: string): Promise<boolean>; /** * Inspect a job with given ID without reserving it. * * @category Other Commands */ peek(jobId: number): Promise<null | IClientRawReservedJob>; /** * Inspect the next ready job. Operates only on the currently used tube. * * @category Other Commands */ peekReady(): Promise<null | IClientRawReservedJob>; /** * Inspect the next delayed job. Operates only on the currently used tube. * * @category Other Commands */ peekDelayed(): Promise<null | IClientRawReservedJob>; /** * Inspect the next buried job. Operates only on the currently used tube. * * @category Other Commands */ peekBuried(): Promise<null | IClientRawReservedJob>; /** * The kick command applies only to the currently used tube. It moves jobs into * the ready queue. If there are any buried jobs, it will only kick buried jobs. * Otherwise it will kick delayed jobs. * * @param bound - integer upper bound on the number of jobs to kick. The server * will kick no more than <bound> jobs. * * @category Other Commands */ kick(bound: number): Promise<number>; /** * The kick-job command is a variant of kick that operates with a single job * identified by its job id. If the given job id exists and is in a buried or * delayed state, it will be moved to the ready queue of the the same tube where it * currently belongs. * * @category Other Commands */ kickJob(jobId: number): Promise<boolean>; /** * The stats command gives statistical information about the system as a whole. * * @category Other Commands */ stats(): Promise<IBeanstalkStats>; /** * The stats-tube command gives statistical information about the specified tube * if it exists. * * @category Other Commands */ statsTube(tubeName: string): Promise<IBeanstalkTubeStats | null>; /** * The stats-job command gives statistical information about the specified job if * it exists. * * @category Other Commands */ statsJob(jobId: number): Promise<IBeanstalkJobStats | null>; /** * The list-tubes command returns a list of all existing tubes. * * @category Other Commands */ listTubes(): Promise<string[]>; /** * The list-tube-used command returns the tube currently being used by the * client. * * @category Other Commands */ listTubeUsed(): Promise<string>; /** * The list-tubes-watched command returns a list tubes currently being watched by * the client. * * @category Other Commands */ listTubesWatched(): Promise<string[]>; /** * The pause-tube command can delay any new job being reserved for a given time. * * @param tubeName - tube to pause * @param delay - integer number of seconds < 2**32 to wait before reserving any more * jobs from the queue * * @category Other Commands */ pauseTube(tubeName: string, delay: number): Promise<boolean>; /** * @category Client */ private waitQueue; /** * Transforms payload to buffer. Also performs size and type checks. * * In case provided payload is not a [[string | number]] it * will be serialized via [[IClientCtorOptions.serializer]] * * @throws {ClientError} * @category Client */ private payloadToBuffer; /** * * @category Client */ private readCommandResponse; /** * Sends command to the server and reads response which then passed to [[Command.handleResponse]]. * * @category Client */ private dispatchCommand; }