Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Jobs.hs @ d316b880

History | View | Annotate | Download (2.1 kB)

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