Revision b81650b0

b/src/Ganeti/JQScheduler.hs
35 35
import Control.Exception
36 36
import Control.Monad
37 37
import Data.List
38
import Data.Maybe
38 39
import Data.IORef
39 40
import System.INotify
40 41

  
......
159 160
    . logInfo $ "Finished jobs: " ++ jlist
160 161
  mapM_ (maybe (return ()) killINotify . jINotify) finished
161 162

  
163
-- | Watcher task for a job, to update it on file changes. It also
164
-- reinstantiates itself upon receiving an Ignored event.
165
jobWatcher :: JQStatus -> JobWithStat -> Event -> IO ()
166
jobWatcher state jWS e = do
167
  let jid = qjId $ jJob jWS
168
      jids = show $ fromJobId jid
169
  logInfo $ "Scheduler notified of change of job " ++ jids
170
  logDebug $ "Scheulder notify event for " ++ jids ++ ": " ++ show e
171
  let inotify = jINotify jWS
172
  when (e == Ignored  && isJust inotify) $ do
173
    qdir <- queueDir
174
    let fpath = liveJobFile qdir jid
175
    _ <- addWatch (fromJust inotify) [Modify, Delete] fpath
176
           (jobWatcher state jWS)
177
    return ()
178
  updateJob state jWS
179

  
180
-- | Attach the job watcher to a running job.
181
attachWatcher :: JQStatus -> JobWithStat -> IO ()
182
attachWatcher state jWS = when (isNothing $ jINotify jWS) $ do
183
  inotify <- initINotify
184
  qdir <- queueDir
185
  let fpath = liveJobFile qdir . qjId $ jJob jWS
186
      jWS' = jWS { jINotify=Just inotify }
187
  logDebug $ "Attaching queue watcher for " ++ fpath
188
  _ <- addWatch inotify [Modify, Delete] fpath $ jobWatcher state jWS'
189
  modifyJobs state . onRunningJobs $ updateJobStatus jWS'
190

  
162 191
-- | Decide on which jobs to schedule next for execution. This is the
163 192
-- pure function doing the scheduling.
164 193
selectJobsToRun :: Queue -> (Queue, [JobWithStat])
......
187 216
  let jobs = map jJob chosen
188 217
  unless (null chosen) . logInfo . (++) "Starting jobs: " . commaJoin
189 218
    $ map (show . fromJobId . qjId) jobs
219
  mapM_ (attachWatcher qstate) chosen
190 220
  result <- try $ JQ.startJobs jobs
191 221
  either (requeueJobs qstate chosen) return result
192 222

  

Also available in: Unified diff