Skip to content

Commit

Permalink
Make reaper workloads modifiable
Browse files Browse the repository at this point in the history
It is convenient to be able to modify reaper workloads outside of the
`reaperAction` in scenarios where we don't want to wait for `reaperDelay` for
the jobs to be updated. This PR adds an atomic `reaperModify` function to the
reaper API which enables this.

Since this changes the visible `Reaper` constructor, I did a major version bump
(`auto-update` 0.1.6 -> 0.2.0) and added a changelog entry. I also bumped the
`warp` dependency on `auto-update`.
  • Loading branch information
FinleyMcIlwaine committed Apr 26, 2024
1 parent e6fdd9f commit fc127e6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
6 changes: 6 additions & 0 deletions auto-update/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ChangeLog for auto-update

## 0.2.0

* Add `reaperModify` to the `Reaper` API, allowing workload modification outside
of the main `reaperAction` loop.
[#985](https://github.com/yesodweb/wai/pull/985)

## 0.1.6

* Add control of activation on leading vs. trailing edges for Control.Debounce
Expand Down
22 changes: 22 additions & 0 deletions auto-update/Control/Reaper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ data Reaper workload item = Reaper
-- ^ Adding an item to the workload
, reaperRead :: IO workload
-- ^ Reading workload.
, reaperModify :: (workload -> workload) -> IO workload
-- ^ Modify the workload. The resulting workload is returned.
--
-- If there is no reaper thread, the modifier will not be applied and
-- 'reaperEmpty' will be returned.
--
-- If the reaper is currently executing jobs, those jobs will not be in
-- the given workload and the workload might appear empty.
--
-- If all jobs are removed by the modifier, the reaper thread will not be
-- killed. The reaper thread will only terminate if 'reaperKill' is called
-- or the result of 'reaperAction' satisfies 'reaperNull'.
--
-- @since 0.2.0
, reaperStop :: IO workload
-- ^ Stopping the reaper thread if exists.
-- The current workload is returned.
Expand Down Expand Up @@ -136,6 +150,7 @@ mkReaper settings@ReaperSettings{..} = do
Reaper
{ reaperAdd = add settings stateRef tidRef
, reaperRead = readRef stateRef
, reaperModify = modifyRef stateRef
, reaperStop = stop stateRef
, reaperKill = kill tidRef
}
Expand All @@ -145,6 +160,13 @@ mkReaper settings@ReaperSettings{..} = do
case mx of
NoReaper -> return reaperEmpty
Workload wl -> return wl
modifyRef stateRef modifier = atomicModifyIORef' stateRef $ \mx ->
case mx of
NoReaper ->
(NoReaper, reaperEmpty)
Workload wl ->
let !wl' = modifier wl
in (Workload wl', wl')
stop stateRef = atomicModifyIORef' stateRef $ \mx ->
case mx of
NoReaper -> (NoReaper, reaperEmpty)
Expand Down
2 changes: 1 addition & 1 deletion auto-update/auto-update.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: auto-update
version: 0.1.6
version: 0.2.0
synopsis: Efficiently run periodic, on-demand actions
description: API docs and the README are available at <http://www.stackage.org/package/auto-update>.
homepage: https://github.com/yesodweb/wai
Expand Down
2 changes: 1 addition & 1 deletion warp/warp.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Flag x509
Library
Build-Depends: base >= 4.12 && < 5
, array
, auto-update >= 0.1.3 && < 0.2
, auto-update >= 0.2 && < 0.3
, bsb-http-chunked < 0.1
, bytestring >= 0.9.1.4
, case-insensitive >= 0.2
Expand Down

0 comments on commit fc127e6

Please sign in to comment.