Optimise autotools/run-in-tempdir
[ganeti-local] / htools / Ganeti / Jobs.hs
1 {-| Generic code to work with jobs, e.g. submit jobs and check their status.
2
3 -}
4
5 {-
6
7 Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 02110-1301, USA.
23
24 -}
25
26 module Ganeti.Jobs
27   ( execJobsWait
28   , waitForJobs
29   ) where
30
31 import Control.Concurrent (threadDelay)
32
33 import Ganeti.BasicTypes
34 import Ganeti.Errors
35 import qualified Ganeti.Luxi as L
36 import Ganeti.OpCodes
37 import Ganeti.Types
38
39 -- | Executes a set of jobs and waits for their completion, returning their
40 -- status.
41 execJobsWait :: [[MetaOpCode]]        -- ^ The list of jobs
42              -> ([L.JobId] -> IO ())  -- ^ Post-submission callback
43              -> L.Client              -- ^ The Luxi client
44              -> IO (Result [(L.JobId, JobStatus)])
45 execJobsWait opcodes callback client = do
46   jids <- L.submitManyJobs client opcodes
47   case jids of
48     Bad e -> return . Bad $ "Job submission error: " ++ formatError e
49     Ok jids' -> do
50       callback jids'
51       waitForJobs jids' client
52
53 -- | Polls a set of jobs at a fixed interval until all are finished
54 -- one way or another.
55 waitForJobs :: [L.JobId] -> L.Client -> IO (Result [(L.JobId, JobStatus)])
56 waitForJobs jids client = do
57   sts <- L.queryJobsStatus client jids
58   case sts of
59     Bad e -> return . Bad $ "Checking job status: " ++ formatError e
60     Ok sts' -> if any (<= JOB_STATUS_RUNNING) sts'
61             then do
62               -- TODO: replace hardcoded value with a better thing
63               threadDelay (1000000 * 15)
64               waitForJobs jids client
65             else return . Ok $ zip jids sts'