Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Jobs.hs @ b8e76da8

History | View | Annotate | Download (3.3 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
  , execJobsWaitOk
30
  , waitForJobs
31
  ) where
32

    
33
import Control.Concurrent (threadDelay)
34
import Data.List
35

    
36
import Ganeti.BasicTypes
37
import Ganeti.Errors
38
import qualified Ganeti.Luxi as L
39
import Ganeti.OpCodes
40
import Ganeti.Types
41

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

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

    
65
-- | Polls a set of jobs at an increasing interval until all are finished one
66
-- way or another.
67
waitForJobs :: [L.JobId] -> L.Client -> IO (Result [(L.JobId, JobStatus)])
68
waitForJobs jids client = waitForJobs' 500000 15000000
69
  where
70
    waitForJobs' delay maxdelay = do
71
      -- TODO: this should use WaitForJobChange once it's available in Haskell
72
      -- land, instead of a fixed schedule of sleeping intervals.
73
      threadDelay $ min delay maxdelay
74
      sts <- L.queryJobsStatus client jids
75
      case sts of
76
        Bad e -> return . Bad $ "Checking job status: " ++ formatError e
77
        Ok sts' -> if any (<= JOB_STATUS_RUNNING) sts' then
78
                     waitForJobs' (delay * 2) maxdelay
79
                   else
80
                     return . Ok $ zip jids sts'
81

    
82
-- | Execute jobs and return @Ok@ only if all of them succeeded.
83
execJobsWaitOk :: [[MetaOpCode]] -> L.Client -> IO (Result ())
84
execJobsWaitOk opcodes client = do
85
  let nullog = const (return () :: IO ())
86
      failed = filter ((/=) JOB_STATUS_SUCCESS . snd)
87
      fmtfail (i, s) = show (fromJobId i) ++ "=>" ++ jobStatusToRaw s
88
  sts <- execJobsWait opcodes nullog client
89
  case sts of
90
    Bad e -> return $ Bad e
91
    Ok sts' -> return (if null $ failed sts' then
92
                         Ok ()
93
                       else
94
                         Bad ("The following jobs failed: " ++
95
                              (intercalate ", " . map fmtfail $ failed sts')))