From 3c38052260ec850aa9aadcb915535a8dcec71fb1 Mon Sep 17 00:00:00 2001 From: Jappie Klooster Date: Wed, 5 Jul 2023 15:22:30 -0400 Subject: [PATCH 1/6] add windows support --- .github/workflows/ci.yml | 2 +- odd-jobs.cabal | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7a3f672..345be33 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,7 +13,7 @@ jobs: ACTIONS_ALLOW_UNSECURE_COMMANDS: true strategy: matrix: - os: [ubuntu-latest] + os: [ubuntu-latest windows] stack: ["2.11.1"] ghc: ["8.8.4", "8.10.7", "9.2.8", "9.4.5"] diff --git a/odd-jobs.cabal b/odd-jobs.cabal index 94af975..974152b 100644 --- a/odd-jobs.cabal +++ b/odd-jobs.cabal @@ -292,7 +292,7 @@ test-suite jobrunner , text-conversions , time , timing-convenience - , unix + , unix-compat , unliftio , unliftio-core , unordered-containers From 2108c8e54077cb737aa220f980da0eccb3f3e457 Mon Sep 17 00:00:00 2001 From: Jappie Klooster Date: Wed, 5 Jul 2023 15:23:38 -0400 Subject: [PATCH 2/6] reword os --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 345be33..f552950 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,7 +13,7 @@ jobs: ACTIONS_ALLOW_UNSECURE_COMMANDS: true strategy: matrix: - os: [ubuntu-latest windows] + os: [ubuntu-latest, windows] stack: ["2.11.1"] ghc: ["8.8.4", "8.10.7", "9.2.8", "9.4.5"] From f462d3861f100e73cc0b394b1ad3cd9ba3fb99bc Mon Sep 17 00:00:00 2001 From: Jappie Klooster Date: Wed, 5 Jul 2023 18:48:51 -0400 Subject: [PATCH 3/6] stub out hdaemon on windows --- odd-jobs.cabal | 16 ++++++++++++---- src/OddJobs/Cli.hs | 10 +++++++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/odd-jobs.cabal b/odd-jobs.cabal index 974152b..4d9f381 100644 --- a/odd-jobs.cabal +++ b/odd-jobs.cabal @@ -81,7 +81,6 @@ library , filepath , friendly-time , generic-deriving - , hdaemonize , hostname , lucid , monad-control @@ -106,6 +105,9 @@ library , unordered-containers , wai , warp + if !os(windows) + build-depends: + , hdaemonize default-language: Haskell2010 executable devel @@ -144,7 +146,6 @@ executable devel , foreign-store , friendly-time , generic-deriving - , hdaemonize , hostname , lucid , monad-control @@ -170,6 +171,9 @@ executable devel , unordered-containers , wai , warp + if !os(windows) + build-depends: + , hdaemonize default-language: Haskell2010 executable odd-jobs-cli-example @@ -198,7 +202,6 @@ executable odd-jobs-cli-example , filepath , friendly-time , generic-deriving - , hdaemonize , hostname , lucid , monad-control @@ -224,6 +227,9 @@ executable odd-jobs-cli-example , unordered-containers , wai , warp + if !os(windows) + build-depends: + , hdaemonize default-language: Haskell2010 test-suite jobrunner @@ -264,7 +270,6 @@ test-suite jobrunner , filepath , friendly-time , generic-deriving - , hdaemonize , hedgehog , hostname , lifted-async @@ -298,4 +303,7 @@ test-suite jobrunner , unordered-containers , wai , warp + if !os(windows) + build-depends: + , hdaemonize default-language: Haskell2010 diff --git a/src/OddJobs/Cli.hs b/src/OddJobs/Cli.hs index 789e7ce..bfb55db 100644 --- a/src/OddJobs/Cli.hs +++ b/src/OddJobs/Cli.hs @@ -2,6 +2,8 @@ {-# LANGUAGE PartialTypeSignatures #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE DataKinds #-} +{-# LANGUAGE CPP #-} + module OddJobs.Cli where import Options.Applicative as Opts @@ -12,7 +14,6 @@ import Data.Functor (void) import Data.Text import OddJobs.Job (startJobRunner, Config(..), LogLevel(..), LogEvent(..)) import OddJobs.Types (UIConfig(..), Seconds(..), delaySeconds) -import qualified System.Posix.Daemonize as Daemonize import System.FilePath (FilePath, takeBaseName, takeDirectory) import System.Posix.Process (getProcessID) import System.Posix.Signals (Handler(CatchOnce), installHandler, sigTERM) @@ -31,6 +32,9 @@ import Data.Text.Encoding (decodeUtf8) import Network.Wai.Handler.Warp as Warp import Debug.Trace import Data.String.Conv (toS) +#ifndef mingw32_HOST_OS +import qualified System.Posix.Daemonize as Daemonize +#endif -- * Introduction -- @@ -147,12 +151,16 @@ defaultStartCommand :: CommonStartArgs -> IO () defaultStartCommand CommonStartArgs{..} mUIArgs cliType = do if startDaemonize then do +#ifdef mingw32_HOST_OS + error "daemons not supported on windows" +#else Daemonize.serviced $ Daemonize.simpleDaemon { Daemonize.program = \() -> withGracefulTermination_ coreStartupFn , Daemonize.name = Just $ takeBaseName startPidFile , Daemonize.pidfileDirectory = Just $ takeDirectory startPidFile } +#endif else coreStartupFn where From a7bbc08afbb2fe0bdd95805ae51e4c06fd7e6622 Mon Sep 17 00:00:00 2001 From: Jappie Klooster Date: Wed, 5 Jul 2023 18:52:52 -0400 Subject: [PATCH 4/6] fix cabal file --- odd-jobs.cabal | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/odd-jobs.cabal b/odd-jobs.cabal index 4d9f381..d2d956f 100644 --- a/odd-jobs.cabal +++ b/odd-jobs.cabal @@ -107,7 +107,7 @@ library , warp if !os(windows) build-depends: - , hdaemonize + hdaemonize default-language: Haskell2010 executable devel @@ -173,7 +173,7 @@ executable devel , warp if !os(windows) build-depends: - , hdaemonize + hdaemonize default-language: Haskell2010 executable odd-jobs-cli-example @@ -229,7 +229,7 @@ executable odd-jobs-cli-example , warp if !os(windows) build-depends: - , hdaemonize + hdaemonize default-language: Haskell2010 test-suite jobrunner @@ -305,5 +305,5 @@ test-suite jobrunner , warp if !os(windows) build-depends: - , hdaemonize + hdaemonize default-language: Haskell2010 From d5e217dae66a0c70d9dc3e3aba4d5a7bf1d09109 Mon Sep 17 00:00:00 2001 From: Jappie Klooster Date: Wed, 5 Jul 2023 18:55:05 -0400 Subject: [PATCH 5/6] fix tests --- odd-jobs.cabal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/odd-jobs.cabal b/odd-jobs.cabal index d2d956f..b8f91f5 100644 --- a/odd-jobs.cabal +++ b/odd-jobs.cabal @@ -297,7 +297,7 @@ test-suite jobrunner , text-conversions , time , timing-convenience - , unix-compat + , unix , unliftio , unliftio-core , unordered-containers From 694e1c19c4b31a18c037d68b9e29302500ea5690 Mon Sep 17 00:00:00 2001 From: Jappie Klooster Date: Wed, 5 Jul 2023 19:13:53 -0400 Subject: [PATCH 6/6] stub out unix --- odd-jobs.cabal | 8 ++++---- src/OddJobs/Cli.hs | 6 +++--- src/OddJobs/Job.hs | 14 +++++++++++++- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/odd-jobs.cabal b/odd-jobs.cabal index b8f91f5..8d0c831 100644 --- a/odd-jobs.cabal +++ b/odd-jobs.cabal @@ -99,7 +99,6 @@ library , text-conversions , time , timing-convenience - , unix , unliftio , unliftio-core , unordered-containers @@ -108,6 +107,7 @@ library if !os(windows) build-depends: hdaemonize + , unix default-language: Haskell2010 executable devel @@ -165,7 +165,6 @@ executable devel , text-conversions , time , timing-convenience - , unix , unliftio , unliftio-core , unordered-containers @@ -174,6 +173,7 @@ executable devel if !os(windows) build-depends: hdaemonize + , unix default-language: Haskell2010 executable odd-jobs-cli-example @@ -221,7 +221,6 @@ executable odd-jobs-cli-example , text-conversions , time , timing-convenience - , unix , unliftio , unliftio-core , unordered-containers @@ -230,6 +229,7 @@ executable odd-jobs-cli-example if !os(windows) build-depends: hdaemonize + , unix default-language: Haskell2010 test-suite jobrunner @@ -297,7 +297,6 @@ test-suite jobrunner , text-conversions , time , timing-convenience - , unix , unliftio , unliftio-core , unordered-containers @@ -306,4 +305,5 @@ test-suite jobrunner if !os(windows) build-depends: hdaemonize + , unix default-language: Haskell2010 diff --git a/src/OddJobs/Cli.hs b/src/OddJobs/Cli.hs index bfb55db..26caa63 100644 --- a/src/OddJobs/Cli.hs +++ b/src/OddJobs/Cli.hs @@ -15,12 +15,9 @@ import Data.Text import OddJobs.Job (startJobRunner, Config(..), LogLevel(..), LogEvent(..)) import OddJobs.Types (UIConfig(..), Seconds(..), delaySeconds) import System.FilePath (FilePath, takeBaseName, takeDirectory) -import System.Posix.Process (getProcessID) -import System.Posix.Signals (Handler(CatchOnce), installHandler, sigTERM) import qualified System.Directory as Dir import qualified System.Exit as Exit import System.Environment (getProgName) -import qualified System.Posix.Signals as Sig import qualified UnliftIO.Async as Async import UnliftIO (bracket_) import Safe (fromJustNote) @@ -34,6 +31,7 @@ import Debug.Trace import Data.String.Conv (toS) #ifndef mingw32_HOST_OS import qualified System.Posix.Daemonize as Daemonize +import System.Posix.Signals (Handler(CatchOnce), installHandler, sigTERM) #endif -- * Introduction @@ -129,7 +127,9 @@ withGracefulTermination ioAction = do var <- newEmptyMVar let terminate = void $ tryPutMVar var () waitForTermination = takeMVar var +#ifndef mingw32_HOST_OS void $ installHandler sigTERM (CatchOnce terminate) Nothing +#endif either (const Nothing) Just <$> race waitForTermination ioAction -- | Like 'withGracefulTermination' but ignoring the return value diff --git a/src/OddJobs/Job.hs b/src/OddJobs/Job.hs index f93f78a..f601dc2 100644 --- a/src/OddJobs/Job.hs +++ b/src/OddJobs/Job.hs @@ -1,5 +1,6 @@ {-# LANGUAGE RankNTypes, FlexibleInstances, FlexibleContexts, PartialTypeSignatures, UndecidableInstances #-} {-# LANGUAGE ExistentialQuantification, RecordWildCards, ScopedTypeVariables #-} +{-# LANGUAGE CPP #-} module OddJobs.Job ( @@ -90,7 +91,6 @@ import Database.PostgreSQL.Simple.Notification import UnliftIO.Async hiding (poll) import UnliftIO.Concurrent (threadDelay, myThreadId) import Data.String -import System.Posix.Process (getProcessID) import Network.HostName (getHostName) import UnliftIO.MVar import Debug.Trace @@ -127,6 +127,9 @@ import Prelude hiding (log) import GHC.Exts (toList) import Database.PostgreSQL.Simple.Types as PGS (Identifier(..)) import Database.PostgreSQL.Simple.ToField as PGS (toField) +#ifndef mingw32_HOST_OS +import System.Posix.Process (getProcessID) +#endif -- | The documentation of odd-jobs currently promotes 'startJobRunner', which -- expects a fairly detailed 'Config' record, as a top-level function for @@ -228,7 +231,16 @@ startJobRunner jm = do jobWorkerName :: IO String jobWorkerName = do +#ifndef mingw32_HOST_OS pid <- getProcessID +#else + -- assign it to a random number for windows. + -- not sure if it impacts anything if this function is called twice, + -- we don't have a global id like on unix for a process + -- so all will be 42 for now (unless this causes issues, then change it) + let pid = 42 +#endif + hname <- getHostName pure $ hname ++ ":" ++ show pid