1 {-| Generic code to work with jobs, e.g. submit jobs and check their status.
7 Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
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.
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.
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
31 import Control.Concurrent (threadDelay)
33 import Ganeti.BasicTypes
35 import qualified Ganeti.Luxi as L
39 -- | Executes a set of jobs and waits for their completion, returning their
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
48 Bad e -> return . Bad $ "Job submission error: " ++ formatError e
51 waitForJobs jids' client
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
59 Bad e -> return . Bad $ "Checking job status: " ++ formatError e
60 Ok sts' -> if any (<= JOB_STATUS_RUNNING) sts'
62 -- TODO: replace hardcoded value with a better thing
63 threadDelay (1000000 * 15)
64 waitForJobs jids client
65 else return . Ok $ zip jids sts'