Class TaskManager

Hierarchy

  • CreateDestroyStartStop
    • TaskManager

Constructors

  • Parameters

    • __namedParameters: {
          activeLimit: number;
          db: DB;
          logger: Logger;
      }
      • activeLimit: number
      • db: DB
      • logger: Logger

    Returns TaskManager

Properties

[initLock]: RWLockWriter
activeLimit: number
activePromises: Map<TaskIdEncoded, PromiseCancellable<any>> = ...
db: DB
generateTaskId: (() => TaskId)

Type declaration

handlers: Map<TaskHandlerId, TaskHandler> = ...
logger: Logger
queueLogger: Logger
queuingLock: Lock = ...

Lock controls whether to run an iteration of the queuing loop

queuingLockReleaser?: ResourceRelease

Releases the queuing lock On the first iteration of the queuing loop the lock may not be acquired yet, and therefore releaser is not set

queuingLoop: null | PromiseCancellable<void> = null

Asynchronous queuing loop This is blocked by the queuingLock The null indicates that the queuing loop isn't running

schedulerLogger: Logger
schedulingLock: Lock = ...

Lock controls whether to run an iteration of the scheduling loop

schedulingLockReleaser?: ResourceRelease

Releases the scheduling lock On the first iteration of the scheduling loop the lock may not be acquired yet, and therefore releaser is not set

schedulingLoop: null | PromiseCancellable<void> = null

Asynchronous scheduling loop This is blocked by the schedulingLock The null indicates that the scheduling loop isn't running

schedulingTimer: null | Timer<void> = null

Timer used to unblock the scheduling loop This releases the schedulingLock if it is locked The null indicates there is no timer running

taskEvents: EventTarget = ...
taskPromises: Map<TaskIdEncoded, PromiseCancellable<any>> = ...
tasksActiveDbPath: readonly (string | Buffer)[] = ...

Tracks actively running tasks Tasks/active/{TaskId} -> null

tasksDbPath: readonly (string | Buffer)[] = ...
tasksLastTaskIdPath: readonly (string | Buffer)[] = ...

Maintain last Task ID to preserve monotonicity across process restarts Tasks/lastTaskId -> {raw(TaskId)}

tasksPathDbPath: readonly (string | Buffer)[] = ...

Tasks indexed path Tasks/path/{...TaskPath}/{TaskId} -> null

tasksQueuedDbPath: readonly (string | Buffer)[] = ...

Queued Tasks This is indexed by TaskId at the end to avoid conflicts Tasks/queued/{lexi(TaskPriority)}/{lexi(TaskTimestamp + TaskDelay)}/{TaskId} -> null

tasksScheduledDbPath: readonly (string | Buffer)[] = ...

Scheduled Tasks This is indexed by TaskId at the end to avoid conflicts Tasks/scheduled/{lexi(TaskTimestamp + TaskDelay)}/{TaskId} -> null

tasksTaskDbPath: readonly (string | Buffer)[] = ...

Tasks collection Tasks/tasks/{TaskId} -> {json(TaskData)}

Accessors

  • get [destroyed](): boolean
  • Returns boolean

  • get [eventHandled](): ReadonlyWeakSet<Event>
  • Returns ReadonlyWeakSet<Event>

  • get [eventHandlers](): ReadonlyMap<string, Set<EventHandlerInfo>>
  • Returns ReadonlyMap<string, Set<EventHandlerInfo>>

  • get [eventTarget](): EventTarget
  • Returns EventTarget

  • get [handleEventError](): ((evt) => void)
  • Returns ((evt) => void)

      • (evt): void
      • Parameters

        • evt: EventError

        Returns void

  • get [running](): boolean
  • Returns boolean

  • get [statusP](): Promise<Status>
  • Returns Promise<Status>

  • get [status](): Status
  • Returns Status

Methods

  • Parameters

    • type: string
    • callback: null | EventListenerOrEventListenerObject
    • Optional options: boolean | AddEventListenerOptions

    Returns void

  • Parameters

    • event: Event

    Returns boolean

  • This is used to garbage collect tasks that have settled Explicit removal of tasks can only be done through task cancellation

    Parameters

    • taskId: TaskId
    • Optional tran: DBTransaction

    Returns Promise<void>

  • Parameters

    • taskId: TaskId
    • Optional tran: DBTransaction

    Returns PromiseCancellable<any>

  • Parameters

    • order: "asc" | "desc" = 'asc'
    • lazy: boolean = false
    • Optional path: TaskPath
    • Optional tran: DBTransaction

    Returns AsyncGenerator<Task, any, unknown>

  • Mutually exclude last task ID mutation Prevents "counter racing" for the last task ID

    Parameters

    • tran: DBTransaction

    Returns Promise<void>

  • Mutual exclusion for task mutation Used to lock:

    • this.updateTask
    • this.queueTask
    • this.startTask
    • this.gcTask
    • this.requeueTask

    Parameters

    • tran: DBTransaction
    • taskId: TaskId

    Returns Promise<void>

  • Transition from scheduled to queued If the task is cancelled, then this does nothing

    Parameters

    Returns Promise<void>

  • Parameters

    • type: string
    • callback: null | EventListenerOrEventListenerObject
    • Optional options: boolean | EventListenerOptions

    Returns void

  • If the process was killed ungracefully then we may need to repair active dangling tasks by moving them back to the queued index

    Returns Promise<void>

  • Schedules a task If this.schedulingLoop isn't running, then this will not attempt to reset the this.schedulingTimer

    Parameters

    • __namedParameters: {
          deadline?: number;
          delay?: number;
          handlerId: TaskHandlerId;
          lazy?: boolean;
          parameters?: TaskParameters;
          path?: TaskPath;
          priority?: number;
      }
      • Optional deadline?: number
      • Optional delay?: number
      • handlerId: TaskHandlerId
      • Optional lazy?: boolean
      • Optional parameters?: TaskParameters
      • Optional path?: TaskPath
      • Optional priority?: number
    • Optional tran: DBTransaction

    Returns Promise<Task>

  • Parameters

    Returns Promise<void>

  • Start scheduling and queuing loop This call is idempotent Use this when Tasks is started in lazy mode

    Returns Promise<void>

  • Transition from queued to active If the task is cancelled, then this does nothing

    Parameters

    Returns Promise<void>

  • Stop the scheduling and queuing loop This call is idempotent

    Returns Promise<void>

  • Same idea as triggerScheduling But this time unlocking the queue to proceed If already unlocked, subsequent unlocking is idempotent The unlocking of the scheduling is delayed Whereas this unlocking is not Remember the queuing just keeps running until finished

    Returns void

  • Triggers the scheduler on a delayed basis If the delay is 0, the scheduler is triggered immediately The scheduling timer is a singleton that can be set by both this.schedulingLoop and this.scheduleTask This ensures that the timer is set to the earliest scheduled task

    Parameters

    • scheduleTime: number

    Returns void

  • Parameters

    • taskId: TaskId
    • taskPatch: Partial<{
          deadline: number;
          delay: number;
          handlerId: TaskHandlerId;
          parameters: TaskParameters;
          path: TaskPath;
          priority: number;
      }>
    • Optional tran: DBTransaction

    Returns Promise<void>

  • Parameters

    • __namedParameters: {
          activeLimit?: number;
          db: DB;
          fresh?: boolean;
          handlers?: Record<TaskHandlerId, TaskHandler>;
          lazy?: boolean;
          logger?: Logger;
      }
      • Optional activeLimit?: number
      • db: DB
      • Optional fresh?: boolean
      • Optional handlers?: Record<TaskHandlerId, TaskHandler>
      • Optional lazy?: boolean
      • Optional logger?: Logger

    Returns Promise<TaskManager>

Generated using TypeDoc