/// <reference types="node" />
import { Datastore } from 'interface-datastore';
import { EventEmitter } from 'tsee';
import log from 'loglevel';
export interface Job<T> {
    id: string;
    job: T;
}
/**
 * Events for Queue's EventEmitter
 */
declare type Events<T> = {
    start: () => void;
    trigger_next: (index?: number) => void;
    stop: () => void;
    next: (task: Job<T>) => void;
    empty: () => void;
    push: (task: Job<T>) => void;
    open: (store: Datastore<Buffer>) => void;
    close: () => void;
    delete: (id: string) => void;
    error: (err: Error) => void;
};
/**
 * Persistent queue for running many short tasks.
 * Based on https://github.com/damoclark/node-persistent-queue
 */
export declare class Queue<T = any> extends EventEmitter<Events<T>> {
    store: Datastore<Buffer>;
    batchSize: number;
    /**
     * Instance variable for whether the queue is empty (not known at instantiation).
     */
    private _empty?;
    /**
     * The queue of objects to operate on
     */
    private _queue;
    /**
     * Total number of jobs left to run.
     */
    private _length;
    /**
     * Whether the queue's Datastore is open.
     */
    private _opened;
    /**
     * Whether the queue should process messages.
     */
    run: boolean;
    /**
     *
     * @param store The underlying persistent Datastore.
     * @param batchSize How many objects to retrieve from DB into queue array at a time.
     */
    constructor(store?: Datastore<Buffer>, batchSize?: number);
    get length(): number | undefined;
    /**
     * Open persistent storage.
     */
    open(): Promise<unknown>;
    /**
     * Close persistent storage.
     */
    close(): Promise<void>;
    /**
     * Start processing the queue.
     */
    start(): void;
    /**
     * Stop processing the queue
     */
    stop(): void;
    /**
     * Calling done indicates that the current job is complete (or failed).
     * Generally called from within a 'next' event handler when finished.
     * It will remove the current job from the persistent queue and emit another 'next' event.
     * @param skip Whether to temporarily skip the (probably failed job). If true (default is false) the current job is
     * dropped from the in-memory queue, but will be re-hydrated from persistent storage once the current batch of jobs
     * are complete. Useful to keep a single job from holding up the whole queue, without throwing it out entirely.
     */
    done(skip?: boolean): void;
    /**
     * Called by user from within their 'next' event handler when error occurred and job to remain at head of queue
     * It will leave the current job in the queue and stop the queue
     */
    abort(): void;
    /**
     * Called by user to push a job to the queue
     * @param job Object to be serialized and pushed to queue as CBOR.
     */
    push(job: T, id?: string): Promise<string>;
    /**
     * Control logging level.
     * @param level The logging level to enable.
     */
    setLogLevel(level: log.LogLevelDesc): this;
    /**
     * Is the persistent storage queue empty
     * @throws If open method hasn't been called first
     * @return True if empty, false if jobs still remain
     */
    get isEmpty(): boolean | undefined;
    /**
     * Is the queue started and processing jobs
     * @return True if started, otherwise false
     */
    get isStarted(): boolean;
    /**
     * Is the queue's persistent storage open.
     * @return True if opened, otherwise false
     */
    get isOpen(): boolean;
    /**
     * Returns true if there is a job with 'id' still in queue, otherwise false
     * @param id The job id to search for
     * @return Promise resolves true if the job id is still in the queue, otherwise false
     */
    has(id: string): Promise<unknown>;
    /**
     * Return an array of job id numbers matching the given job data in order of execution.
     * @param job The job to search for.
     */
    getJobIds(job: T): Promise<string[]>;
    /**
     * Return an array of job id numbers matching the given job data in order of execution.
     * @param job The job to search for.
     */
    getFirstJobId(job: T): Promise<string>;
    /**
     * Delete a job from the queue (if it exists).
     * @param id The job id number to delete.
     * @return The id number that was deleted.
     */
    delete(id: string): Promise<unknown>;
    private countQueue;
    private searchQueue;
    /**
     * This function will load from the database, 'size' number of records into queue array.
     * @param limit How many records to hydrate.
     */
    private hydrateQueue;
    /**
     * This function will remove the given or current job from the database and in-memory array.
     * @param id Optional job id number to remove, if omitted, remove current job at front of queue.
     */
    private removeJob;
}
export {};
