Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Jobs.hs @ 3f68544e

History | View | Annotate | Download (2.5 kB)

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
  ( submitJobs
28
  , execJobsWait
29
  , waitForJobs
30
  ) where
31

    
32
import Control.Concurrent (threadDelay)
33

    
34
import Ganeti.BasicTypes
35
import Ganeti.Errors
36
import qualified Ganeti.Luxi as L
37
import Ganeti.OpCodes
38
import Ganeti.Types
39

    
40
-- | Submits a set of jobs and returns their job IDs without waiting for
41
-- completion.
42
submitJobs :: [[MetaOpCode]] -> L.Client -> IO (Result [L.JobId])
43
submitJobs opcodes client = do
44
  jids <- L.submitManyJobs client opcodes
45
  return (case jids of
46
            Bad e    -> Bad $ "Job submission error: " ++ formatError e
47
            Ok jids' -> Ok jids')
48

    
49
-- | Executes a set of jobs and waits for their completion, returning their
50
-- status.
51
execJobsWait :: [[MetaOpCode]]        -- ^ The list of jobs
52
             -> ([L.JobId] -> IO ())  -- ^ Post-submission callback
53
             -> L.Client              -- ^ The Luxi client
54
             -> IO (Result [(L.JobId, JobStatus)])
55
execJobsWait opcodes callback client = do
56
  jids <- submitJobs opcodes client
57
  case jids of
58
    Bad e -> return $ Bad e
59
    Ok jids' -> do
60
      callback jids'
61
      waitForJobs jids' client
62

    
63
-- | Polls a set of jobs at a fixed interval until all are finished
64
-- one way or another.
65
waitForJobs :: [L.JobId] -> L.Client -> IO (Result [(L.JobId, JobStatus)])
66
waitForJobs jids client = do
67
  sts <- L.queryJobsStatus client jids
68
  case sts of
69
    Bad e -> return . Bad $ "Checking job status: " ++ formatError e
70
    Ok sts' -> if any (<= JOB_STATUS_RUNNING) sts'
71
            then do
72
              -- TODO: replace hardcoded value with a better thing
73
              threadDelay (1000000 * 15)
74
              waitForJobs jids client
75
            else return . Ok $ zip jids sts'