ganeti

Safe HaskellSafe-Infered

Ganeti.Utils.AsyncWorker

Description

Provides a general functionality for workers that run on the background and perform some task when triggered.

Each task can process multiple triggers, if they're coming faster than the tasks are being processed.

Properties:

  • If a worked is triggered, it will perform its action eventually. (i.e. it won't miss a trigger).
  • If the worker is busy, the new action will start immediately when it finishes the current one.
  • If the worker is idle, it'll start the action immediately.
  • If the caller uses triggerAndWait, the call will return just after the earliest action following the trigger is finished.
  • If the worker finishes an action and there are no pending triggers since the start of the last action, it becomes idle and waits for a new trigger.

Synopsis

Documentation

data TriggerState i a Source

Constructors

Idle 
Pending i [MVar a] 

addTrigger :: Monoid i => i -> Maybe (MVar a) -> TriggerState i a -> TriggerState i aSource

data AsyncWorker i a Source

Represent an asynchronous worker whose single action execution returns a value of type a.

Constructors

AsyncWorker ThreadId (IORef (TriggerState i a)) (MVar ()) 

mkAsyncWorker :: (Monoid i, MonadBaseControl IO m) => (i -> m a) -> m (AsyncWorker i a)Source

Given an action, construct an AsyncWorker.

mkAsyncWorker_ :: MonadBaseControl IO m => m a -> m (AsyncWorker () a)Source

Given an action, construct an AsyncWorker with no input.

triggerInternal :: (MonadBase IO m, Monoid i) => i -> Maybe (MVar a) -> AsyncWorker i a -> m ()Source

trigger :: (MonadBase IO m, Monoid i) => i -> AsyncWorker i a -> m ()Source

Trigger a worker, letting it run its action asynchronously, but do not wait for the result.

trigger_ :: MonadBase IO m => AsyncWorker () a -> m ()Source

Trigger a worker with no input, letting it run its action asynchronously, but do not wait for the result.

triggerAndWaitMany :: (Traversable t, MonadBase IO m, Monoid i) => i -> t (AsyncWorker i a) -> m (t a)Source

Trigger a list of workers and wait until all the actions following these triggers finish. Returns the results of the actions.

Note that there is a significant difference between triggerAndWaitMany and mapM triggerAndWait. The latter runs all the actions of the workers sequentially, while the former runs them in parallel.

triggerAndWaitMany_ :: (Traversable t, MonadBase IO m) => t (AsyncWorker () a) -> m (t a)Source

Trigger a list of workers with no input and wait until all the actions following these triggers finish. Returns the results of the actions.

See triggetAndWaitMany.

triggerAndWait :: (MonadBase IO m, Monoid i) => i -> AsyncWorker i a -> m aSource

Trigger a worker and wait until the action following this trigger finishes. Return the result of the action.

triggerAndWait_ :: MonadBase IO m => AsyncWorker () a -> m aSource

Trigger a worker with no input and wait until the action following this trigger finishes. Return the result of the action.