JobQueue

open class JobQueue<Job: QueueJob>

An in-memory (with optional persistence) queue which can be processed by a JobProcessor

  • Defines the state of the queue.

    See more

    Declaration

    Swift

    public enum State
  • Defines what to do in the case of jobs which failed to process. By default retryStrategy is nil, in which case failed jobs are simply removed from the queue.

    Declaration

    Swift

    public var retryStrategy: JobRetryStrategy?
  • Returns the current JobQueue.State of this instance

    Declaration

    Swift

    private(set) public var state: State
  • Gets the JobProcessor to process the jobs on this instance, if any; otherwise nil. JobQueues without a JobProcessor must manually process and remove jobs from the queue.

    Declaration

    Swift

    public var processor: AnyJobProcessor<Job>?
  • Gets the JobPersister to persist jobs in this queue, if one exists; otherwise nil. JobQueues that do not have a JobPersister will be in-memory only.

    Declaration

    Swift

    public var persister: AnyJobPersister<Job>?
  • Initializes this JobQueue to handle the given type of Job

    Throws

    Throws if a JobPersister is provided and it fails to load its jobs.

    Declaration

    Swift

    public init(handling: Job.Type, name: String, persister: AnyJobPersister<Job>? = nil) throws

    Parameters

    handling

    The expect type of QueueJob this JobQueue will handle

    name

    The name for this queue.

    persister

    A JobPersister that may be used to persist jobs.

  • Returns the number of jobs in this JobQueue awaiting processing.

    Declaration

    Swift

    public var count: Int
  • Adds the provided jobs to this JobQueue, while maintaining their order.

    Remark

    This function is thread safe.

    Declaration

    Swift

    public func add(_ jobs: [Job])

    Parameters

    jobs

    The jobs to be added to this JobQueue.

  • Queues the provided job for immediate processing. That is, the provided job will be inserted at the very front of this JobQueue

    Remark

    This function is thread safe

    Declaration

    Swift

    public func retry(_ job: Job)

    Parameters

    job

    The job to be inserted at the front of this JobQueue

  • Attempts to return without removing the next Job at the front of this JobQueue.

    Remark

    This function is thread safe

    Declaration

    Swift

    public func peek() -> Job?

    Return Value

    The next Job at the front of this JobQueue, if one exists; otherwise nil

  • Removes the next Job from the front of this JobQueue

    Remark

    This function is thread safe

    Declaration

    Swift

    public func remove() -> Job?

    Return Value

    The next Job at the front of this JobQueue, if one exists; otherwise nil

  • Cancels the job in this JobQueue with the provided id, if it exists.

    Attention

    This cannot cancel a matching Job if that Job is already being processed.

    Remark

    This function is thread safe

    Declaration

    Swift

    public func cancel(_ id: String)

    Parameters

    id

    The id of the Job to be removed from this JobQueue

  • Removes all jobs from this JobQueue

    Remark

    This function is thread safe

    Declaration

    Swift

    public func drain()
  • Tells this JobQueue to start processing its Jobs

    Declaration

    Swift

    public func start()
  • Tells this JobQueue to stop processing its Jobs.

    Declaration

    Swift

    public func stop()
  • Subscribes a listener to this JobQueue.

    Attention

    The returned JobQueueNotificationToken is weak, and must be strongly retained by the receiver. As soon as the token goes out of scope, or is set to nil, the observer will no longer recieve notifications.

    Declaration

    Swift

    public func observe(_ block: @escaping (_ queue: JobQueue<Job>, _ jobs: [Job], _ event: JobQueueEvent) -> Void) -> JobQueueNotificationToken<Job>

    Parameters

    block

    The function to be called which will receive the notification

    queue

    The JobQueue from which the notification is broadcast

    jobs

    The jobs affected by the JobQueueEvent

    event

    The JobQueueEvent (such as .added, .removed, etc) affecting the provided jobs

    Return Value

    A JobQueueNotificationToken.