Skip to content

Commit

Permalink
split off background jobs into shelly-extra
Browse files Browse the repository at this point in the history
* one less dependency (SafeSemaphore) for shelly
  • Loading branch information
gregwebs committed Jun 11, 2012
1 parent 74b2d54 commit b0f85bd
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 54 deletions.
54 changes: 3 additions & 51 deletions Shelly.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ module Shelly
, mv, rm, rm_f, rm_rf, cp, cp_r, mkdir, mkdir_p
, readfile, writefile, appendfile, withTmpDir

-- * Running external commands asynchronously.
, jobs, background, getBgResult, BgResult

-- * exiting the program
, exit, errorExit, terror

Expand All @@ -72,6 +69,9 @@ module Shelly

-- * Re-exported for your convenience
, liftIO, when, unless, FilePath

-- * internal functions for writing extension
, get, put
) where

-- TODO:
Expand Down Expand Up @@ -109,7 +109,6 @@ import Control.Applicative
import Control.Exception hiding (handle)
import Control.Monad.Reader
import Control.Concurrent
import qualified Control.Concurrent.MSem as Sem
import Data.Time.Clock( getCurrentTime, diffUTCTime )

import qualified Data.Text.Lazy.IO as TIO
Expand Down Expand Up @@ -694,53 +693,6 @@ verbosely a = sub $ modify (\x -> x { sPrintStdout = True, sPrintCommands = True
print_stdout :: Bool -> ShIO a -> ShIO a
print_stdout shouldPrint a = sub $ modify (\x -> x { sPrintStdout = shouldPrint }) >> a

-- | Create a 'BgJobManager' that has a 'limit' on the max number of background tasks.
-- an invocation of jobs is independent of any others, and not tied to the ShIO monad in any way.
-- This blocks the execution of the program until all 'background' jobs are finished.
jobs :: Int -> (BgJobManager -> ShIO a) -> ShIO a
jobs limit action = do
unless (limit > 0) $ terror "expected limit to be > 0"
availableJobsSem <- liftIO $ Sem.new limit
res <- action $ BgJobManager availableJobsSem
liftIO $ waitForJobs availableJobsSem
return res
where
waitForJobs sem = do
avail <- Sem.peekAvail sem
if avail == limit then return () else waitForJobs sem

-- | The manager tracks the number of jobs. Register your 'background' jobs with it.
newtype BgJobManager = BgJobManager (Sem.MSem Int)

-- | Type returned by tasks run asynchronously in the background.
newtype BgResult a = BgResult (MVar a)

-- | Returns the promised result from a backgrounded task. Blocks until
-- the task completes.
getBgResult :: BgResult a -> ShIO a
getBgResult (BgResult mvar) = liftIO $ takeMVar mvar

-- | Run the `ShIO` task asynchronously in the background, returns
-- the `BgResult a`, a promise immediately. Run "getBgResult" to wait for the result.
-- The background task will inherit the current ShIO context
-- The 'BjJobManager' ensures the max jobs limit must be sufficient for the parent and all children.
background :: BgJobManager -> ShIO a -> ShIO (BgResult a)
background (BgJobManager manager) proc = do
state <- get
liftIO $ do
-- take up a spot
-- It is important to do this before forkIO:
-- It ensures that that jobs will block and the program won't exit before our jobs are done
-- On the other hand, a user might not expect 'jobs' to block
Sem.wait manager
mvar <- newEmptyMVar -- future result

_<- forkIO $ do
result <- shelly $ (put state >> proc)
Sem.signal manager -- open a spot back up
liftIO $ putMVar mvar result
return $ BgResult mvar


-- | Turn on/off command echoing.
print_commands :: Bool -> ShIO a -> ShIO a
Expand Down
30 changes: 30 additions & 0 deletions shelly-extra/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Copyright (c) 2010, Petr Rockai <[email protected]>

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* Neither the name of Petr Rockai nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 changes: 57 additions & 0 deletions shelly-extra/Shelly/Extra.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{-# LANGUAGE OverloadedStrings #-}
module Shelly.Extra (
-- * Running external commands asynchronously.
jobs, background, getBgResult, BgResult
) where

import Shelly
import Control.Concurrent
import qualified Control.Concurrent.MSem as Sem

-- | Create a 'BgJobManager' that has a 'limit' on the max number of background tasks.
-- an invocation of jobs is independent of any others, and not tied to the ShIO monad in any way.
-- This blocks the execution of the program until all 'background' jobs are finished.
jobs :: Int -> (BgJobManager -> ShIO a) -> ShIO a
jobs limit action = do
unless (limit > 0) $ terror "expected limit to be > 0"
availableJobsSem <- liftIO $ Sem.new limit
res <- action $ BgJobManager availableJobsSem
liftIO $ waitForJobs availableJobsSem
return res
where
waitForJobs sem = do
avail <- Sem.peekAvail sem
if avail == limit then return () else waitForJobs sem

-- | The manager tracks the number of jobs. Register your 'background' jobs with it.
newtype BgJobManager = BgJobManager (Sem.MSem Int)

-- | Type returned by tasks run asynchronously in the background.
newtype BgResult a = BgResult (MVar a)

-- | Returns the promised result from a backgrounded task. Blocks until
-- the task completes.
getBgResult :: BgResult a -> ShIO a
getBgResult (BgResult mvar) = liftIO $ takeMVar mvar

-- | Run the `ShIO` task asynchronously in the background, returns
-- the `BgResult a`, a promise immediately. Run "getBgResult" to wait for the result.
-- The background task will inherit the current ShIO context
-- The 'BjJobManager' ensures the max jobs limit must be sufficient for the parent and all children.
background :: BgJobManager -> ShIO a -> ShIO (BgResult a)
background (BgJobManager manager) proc = do
state <- get
liftIO $ do
-- take up a spot
-- It is important to do this before forkIO:
-- It ensures that that jobs will block and the program won't exit before our jobs are done
-- On the other hand, a user might not expect 'jobs' to block
Sem.wait manager
mvar <- newEmptyMVar -- future result

_<- forkIO $ do
result <- shelly $ (put state >> proc)
Sem.signal manager -- open a spot back up
liftIO $ putMVar mvar result
return $ BgResult mvar

44 changes: 44 additions & 0 deletions shelly-extra/shelly-extra.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Name: shelly

Version: 0.9.8
Synopsis: shelly features that require extra dependencies

Description: Please see the shelly package. Shelly provides a single module for convenient
systems programming in Haskell, similar in spirit to POSIX shells.
.
shelly-extra is designed to be a grab bag for functionality that requires extra dependencies
.
currently contains a background job implementation for performing tasks in parallel


Homepage: https://github.com/yesodweb/Shelly.hs
License: BSD3
License-file: LICENSE
Author: Greg Weber
Maintainer: Greg Weber <[email protected]>
Category: Development
Build-type: Simple
Cabal-version: >=1.8


Library
Exposed-modules: Shelly.Extra

Build-depends: base, shelly >= 0.10 , SafeSemaphore

ghc-options: -Wall

test-suite test
type: exitcode-stdio-1.0
main-is: main.hs
hs-source-dirs: ., test
ghc-options: -Wall

Build-depends: base
, hspec-discover
, hspec >= 1.1
, HUnit

source-repository head
type: git
location: https://github.com/yesodweb/Shelly.hs
4 changes: 1 addition & 3 deletions shelly.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Name: shelly

Version: 0.9.8
Version: 0.10
Synopsis: shell-like (systems) programming in Haskell

Description: Shelly provides a single module for convenient
Expand Down Expand Up @@ -34,7 +34,6 @@ Library

Build-depends: base >= 4 && < 5, time, directory, text, mtl
, process >= 1.0
, SafeSemaphore
, unix-compat < 0.4
, system-filepath < 0.5
, system-fileio < 0.4
Expand All @@ -48,7 +47,6 @@ test-suite test
ghc-options: -Wall

Build-depends: base >= 4 && < 5, time, directory, text, mtl, process
, SafeSemaphore
, unix-compat < 0.4
, system-filepath < 0.5
, system-fileio < 0.4
Expand Down

0 comments on commit b0f85bd

Please sign in to comment.