summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfunctor_soup <>2017-04-20 14:01:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2017-04-20 14:01:00 (GMT)
commit6fff9db80e0f12b919db9aeb971e54148d14ed3b (patch)
treef4941ffa94f079feef31eeec833d0a79430cb6d6
parent5c2afb90cf737e2e25aeae716d7277945f5f0cfb (diff)
version 0.1.4.10.1.4.1
-rw-r--r--README.md2
-rw-r--r--TinyScheduler/Jobs.hs32
-rw-r--r--TinyScheduler/SubJobs.hs5
-rw-r--r--TinyScheduler/TimeAtom.hs32
-rw-r--r--tiny-scheduler.cabal4
5 files changed, 44 insertions, 31 deletions
diff --git a/README.md b/README.md
index 76c7889..a65d9c9 100644
--- a/README.md
+++ b/README.md
@@ -64,5 +64,5 @@ main = getCurrentTime >>= (\x ->
4. Complete the example below
-### for a more advanced example (still in progress)
+### for an application based example (still in progress)
go to [https://github.com/functor-soup/tiny-simple-scheduler-example](https://github.com/functor-soup/tiny-simple-scheduler-example)
diff --git a/TinyScheduler/Jobs.hs b/TinyScheduler/Jobs.hs
index 9b9f9ec..237a53a 100644
--- a/TinyScheduler/Jobs.hs
+++ b/TinyScheduler/Jobs.hs
@@ -3,9 +3,7 @@
to calculate thread delay
-}
module TinyScheduler.Jobs
- ( TimeAtom(..)
- , Job(..)
- , makeTimeAtom
+ ( Job(..)
, makeJob
, timeAtomToJob
) where
@@ -13,29 +11,8 @@ module TinyScheduler.Jobs
import Data.Time
import Prelude hiding (id)
import TinyScheduler.Time
-
--- | Main datatype atom
-data TimeAtom = TimeAtom
- { delay_ :: UTCTime -> UTCTime -> [Int]
- }
-
--- | this is the thread delay, calculated as [unit: microseconds)]
--- (startTime - currentTime) + (interval * multiplier)
-calculateDelay :: Interval -> Int -> UTCTime -> UTCTime -> [Int]
-calculateDelay interval hits startDate currentTime =
- let intervalInSeconds = intervalToSecs interval
- delay = fromEnum $ 10 ^^ (-6) * (diffUTCTime startDate currentTime)
- interval_ = (round (intervalInSeconds * 10 ^ (6))) + delay :: Int
- in map (interval_ *) [1 .. hits]
-
-makeTimeAtom :: Int -> Interval -> TimeAtom
-makeTimeAtom x y = TimeAtom (calculateDelay y x)
-
-instance Monoid TimeAtom where
- mempty = TimeAtom (\x y -> [])
- mappend x y =
- let delay = \a b -> ((delay_ x a b) >>= \c -> map (c +) (delay_ y a b))
- in TimeAtom delay
+import TinyScheduler.TimeAtom
+import TinyScheduler.Utils (calculateDelay)
-- | Main datatype to hold job Information
data Job a = Job
@@ -44,8 +21,11 @@ data Job a = Job
, job :: IO a
}
+-- | Convert time atom to Job
timeAtomToJob :: Int -> IO a -> UTCTime -> TimeAtom -> Job a
timeAtomToJob id job start atom = Job {id = id, delay = (delay_ atom start), job = job}
+-- | Function to generate job from information about id, no of hits, interval
+-- | startTine and the a function with side effects
makeJob :: Int -> Int -> Interval -> UTCTime -> IO a -> Job a
makeJob id hits interval startTime job = Job id (calculateDelay interval hits startTime) job
diff --git a/TinyScheduler/SubJobs.hs b/TinyScheduler/SubJobs.hs
index 7927651..c65fe97 100644
--- a/TinyScheduler/SubJobs.hs
+++ b/TinyScheduler/SubJobs.hs
@@ -6,8 +6,8 @@ module TinyScheduler.SubJobs
import Control.Concurrent
import Control.Concurrent.Async
import Data.Time
-import Prelude hiding (id)
import TinyScheduler.Jobs
+import Prelude hiding (id)
data SubJob a = SubJob
{ jobId :: Int
@@ -16,11 +16,12 @@ data SubJob a = SubJob
, job_ :: IO a
}
+-- | Converts each hit of a job into a Subjob
convertJobIntoSubJobs :: UTCTime -> Job a -> [SubJob a]
convertJobIntoSubJobs currentTime x =
let timeDelays = (delay x currentTime)
zippedDelays = zip [1 ..] timeDelays
- in map (\(i, z) -> SubJob (id x) i z (job x)) zippedDelays
+ in map (\(i, z) -> SubJob (id x) i z (job x)) zippedDelays
execSubJob :: SubJob a -> IO a
execSubJob x = threadDelay (delayx x) >> (job_ x)
diff --git a/TinyScheduler/TimeAtom.hs b/TinyScheduler/TimeAtom.hs
new file mode 100644
index 0000000..bffb302
--- /dev/null
+++ b/TinyScheduler/TimeAtom.hs
@@ -0,0 +1,32 @@
+{-| Module for time atoms -}
+module TinyScheduler.TimeAtom
+ ( TimeAtom(..)
+ , makeTimeAtom
+ ) where
+
+import Data.Time
+import TinyScheduler.Time
+import TinyScheduler.Utils (calculateDelay)
+
+-- | Composable Time atom
+data TimeAtom = TimeAtom
+ { delay_ :: UTCTime -> UTCTime -> [Int]
+ }
+
+-- | Exposed function to create time atoms
+makeTimeAtom :: Int -> Interval -> TimeAtom
+makeTimeAtom x y = TimeAtom (calculateDelay y x)
+
+-- | The composability sauce
+-- | if you have two time atoms that when visualized look
+-- | somewhat like shown below
+-- | ......(a).............(a).............
+-- | <>
+-- | ..(b)..(b)............................
+-- | =
+-- |..........(b)..(b)..........(b)..(b)...
+instance Monoid TimeAtom where
+ mempty = TimeAtom (\x y -> [])
+ mappend x y =
+ let delay = \a b -> ((delay_ x a b) >>= \c -> map (c +) (delay_ y a b))
+ in TimeAtom delay
diff --git a/tiny-scheduler.cabal b/tiny-scheduler.cabal
index f0de12b..cea2bc9 100644
--- a/tiny-scheduler.cabal
+++ b/tiny-scheduler.cabal
@@ -1,5 +1,5 @@
name: tiny-scheduler
-version: 0.1.4.0
+version: 0.1.4.1
synopsis: tiny no-brainer job scheduler
description: this is a tiny library to make scheduling jobs to run at predetermined intervals, easier
homepage: https://github.com/functor-soup/tiny-scheduler#readme
@@ -15,7 +15,7 @@ cabal-version: >=1.10
library
hs-source-dirs: .,TinyScheduler
- exposed-modules: TinyScheduler.Jobs, TinyScheduler.SubJobs, TinyScheduler.Time
+ exposed-modules: TinyScheduler.Jobs, TinyScheduler.SubJobs, TinyScheduler.Time, TinyScheduler.TimeAtom
build-depends: base >= 4.7 && < 5, async, time
default-language: Haskell2010