import { SpanOptions, Span } from 'opentracing'; export * from './source_sink_task'; export * from './sqs_consumer_task'; export * from './sns_sqs_queue'; export * from './sns_sqs_consumer_task'; export * from './source_sink_runner'; export * from './sqs_queue'; export * from './clients/aws'; export * from './clients/sqs'; export * from './clients/sns'; export * from './logger'; import { Logger } from './logger'; export declare type TaskWrapper = (task: Task, taskConfig: TaskConfig) => Promise; export declare type TaskExecution = () => Promise; export declare class DelayError extends Error { readonly delaySecs: number; readonly reason: string; constructor(delaySecs: number, reason: string); } export declare class ErrorWithRetryCount extends Error { cause: Error; attemptNum: number; constructor(cause: Error, attemptNum: number); } export interface Task { /** * Executes the task, and returns a boolean representing whether more work could be done. * * If it returns true, the scheduler may immediately reschedule (call it again without delay) to * process more work. */ execute: TaskExecution; /** * Performs any logic necessary for the task's cleanup when the system is shut down. */ shutdown?: () => void; } interface Tracer { /** * Instruments a function by automatically creating a span activated on its scope. */ trace(name: string, options: SpanOptions, fn: (span: Span) => TResult): TResult; } /** * When used as the `wrapExecute`, this function will setup tracing for all scheduled tasks. This * implementation is known to work with Datadog, but other providers that use this API should also work. * * When no tracer is provided, then this will delegate directly to the task.. * * @param tracer a tracer implementation. When not undefined will start a trace for every task. */ export declare function createExecuteWrapper(tracer?: Tracer): TaskWrapper; export interface TaskConfig { /** * Interval in milliseconds between task executions. */ intervalMs: number; /** * Name of task, used to dedupe calls to {@link Scheduler#schedule}, and for logging. */ name?: string; /** * Initial delay before first task execution. Defaults to intervalMs. */ initialDelayMs?: number; /** * When `true`, the scheduler will not wrap this task with the `SchedulerConfig` `wrapExecute` * function, allowing consumers to write their own tracing/metrics logic. Defaults to `false`. */ ignoreExecutionWrapper?: boolean; } export interface MomentDuration { asMilliseconds(): number; } export interface Guard { getExclusive(name: string, ttlSeconds: number): Promise; } export interface SchedulerConfig { /** * Error handler called when a task execution throws. Default implementation rethrows. */ errorHandler?: (err: Error, taskConfig?: TaskConfig, retryCount?: number) => any; wrapExecute?: TaskWrapper; logger?: Logger; /** * If specified, scheduled tasks will have their intervals jittered by this duration. */ jitterSeconds?: number; } export declare class TaskScheduler { private config; private schedules; private timeoutId; private isShutdown; private log; constructor(config?: SchedulerConfig); /** * Schedule a task. The {@link Task} will be executed according to its {@link TaskConfig}. * * The task will be run _up to_ every `intervalMs` milliseconds. A long-running task will * be limited to one-at-a-time, which means it may cause the scheduler to skip one or more executions of that task. * * The task will execute for the first time after `intervalMs` milliseconds. */ schedule(task: Task, configArg: MomentDuration | number | TaskConfig): void; /** * Begin a graceful shutdown of the scheduler. This will wait for all *actively* running * tasks to settle. */ shutdown(): Promise; private scheduleNext; private executeAllReadyTasks; private calculateNextExecutionEpochMs; private wrapExecute; }