auto-update-0.1.2.2: Efficiently run periodic, on-demand actions

Safe Haskell | Safe
Language | Haskell2010

Control.Reaper

Contents

- Settings
- Accessors
- Type
- Creation
- Helper

Description

This module provides the ability to create reapers: dedicated cleanup threads. These threads will automatically spawn and die based on the presence of a workload to process on.

Synopsis

- data ReaperSettings workload item
- defaultReaperSettings :: ReaperSettings [item] item
- reaperAction :: ReaperSettings workload item -> workload -> IO (workload -> workload)
- reaperDelay :: ReaperSettings workload item -> Int
- reaperCons :: ReaperSettings workload item -> item -> workload -> workload
- reaperNull :: ReaperSettings workload item -> workload -> Bool
- reaperEmpty :: ReaperSettings workload item -> workload
- data Reaper workload item = Reaper {
  - reaperAdd :: item -> IO ()
  - reaperRead :: IO workload
  - reaperStop :: IO workload
  }
- mkReaper :: ReaperSettings workload item -> IO (Reaper workload item)
- mkListAction :: (item -> IO (Maybe item')) -> [item] -> IO ([item'] -> [item'])

# Settings

data ReaperSettings workload item

Settings for creating a reaper. This type has two parameters: `workload` gives the entire workload, whereas `item` gives an individual piece of the queue. A common approach is to have `workload` be a list of `item`s. This is encouraged by `defaultReaperSettings` and `mkListAction`.

Since 0.1.1

defaultReaperSettings :: ReaperSettings [item] item

Default `ReaperSettings` value, biased towards having a list of work items.

Since 0.1.1

# Accessors

reaperAction :: ReaperSettings workload item -> workload -> IO (workload -> workload)

The action to perform on a workload. The result of this is a "workload modifying" function. In the common case of using lists, the result should be a difference list that prepends the remaining workload to the temporary workload. For help with setting up such an action, see `mkListAction`.

Default: do nothing with the workload, and then prepend it to the temporary workload. This is incredibly useless; you should definitely override this default.

Since 0.1.1

reaperDelay :: ReaperSettings workload item -> Int

Number of microseconds to delay between calls of `reaperAction`.

Default: 30 seconds.

Since 0.1.1

reaperCons :: ReaperSettings workload item -> item -> workload -> workload

Add an item onto a workload.

Default: list consing.

Since 0.1.1

reaperNull :: ReaperSettings workload item -> workload -> Bool

Check if a workload is empty, in which case the worker thread will shut down.

Default: `null`.

Since 0.1.1

reaperEmpty :: ReaperSettings workload item -> workload

An empty workload.

Default: empty list.

Since 0.1.1

# Type

data Reaper workload item

A data structure to hold reaper APIs.

Constructors

Reaper |
Fields reaperAdd :: item -> IO () Adding an item to the workload reaperRead :: IO workload Reading workload. reaperStop :: IO workload Stopping the reaper thread if exists. The current workload is returned.

# Creation

mkReaper :: ReaperSettings workload item -> IO (Reaper workload item)

Create a reaper addition function. This funciton can be used to add new items to the workload. Spawning of reaper threads will be handled for you automatically.

Since 0.1.1

# Helper

mkListAction :: (item -> IO (Maybe item')) -> [item] -> IO ([item'] -> [item'])

A helper function for creating `reaperAction` functions. You would provide this function with a function to process a single work item and return either a new work item, or `Nothing` if the work item is expired.

Since 0.1.1