schedule :: Connection -> IO ()
schedule con = wakeupService True $ \request -> do

  -- Thread to detect a change in the set of jobs.
  let loop = do
        mjob <- nextScheduledJob con
        case mjob of
          Nothing -> return ()
          Just job -> do
            putStrLn "The set of scheduled jobs has changed."
            hFlush stdout
            amount <- amountToSleep job
            request amount

        _ <- execute_ con "LISTEN humming_schedule"
        -- TODO The Ruby version waits with a timeout.
        _ <- getNotification con
        _ <- execute_ con "UNLISTEN humming_schedule"
        Q.drainNotifications con
        loop

  _ <- forkIO loop

  return . Client $ runTasks con

-- | Give a chance to tasks to be run. Return possibly a request to receive
-- another wakeup n seconds later.
schedule :: Connection -> IO ()
schedule con = wakeupService True $ \request -> do

  -- Thread to detect a change in the set of jobs.
  let loop = do
        mjob <- nextScheduledJob con
        case mjob of
          Nothing -> return ()
          Just job -> do
            putStrLn "The set of scheduled jobs has changed."
            hFlush stdout
            amount <- amountToSleep job
            request amount

        _ <- execute_ con "LISTEN humming_schedule"
        -- TODO The Ruby version waits with a timeout.
        _ <- getNotification con
        _ <- execute_ con "UNLISTEN humming_schedule"
        Q.drainNotifications con
        loop

  _ <- forkIO loop

  return . Client $ runTasks con

-- | Give a chance to tasks to be run. Return possibly a request to receive
-- another wakeup n seconds later.
schedule con = wakeupService True $ \request -> do

  -- Thread to detect a change in the set of jobs.
  let loop = do
        mjob <- nextScheduledJob con
        case mjob of
          Nothing -> return ()
          Just job -> do
            putStrLn "The set of scheduled jobs has changed."
            hFlush stdout
            amount <- amountToSleep job
            request amount

        _ <- execute_ con "LISTEN humming_schedule"
        -- TODO The Ruby version waits with a timeout.
        _ <- getNotification con
        _ <- execute_ con "UNLISTEN humming_schedule"
        Q.drainNotifications con
        loop

  _ <- forkIO loop

  return . Client $ runTasks con

-- | Give a chance to tasks to be run. Return possibly a request to receive
-- another wakeup n seconds later.