runTasks :: Connection -> IO (Maybe Int)
runTasks con = do
  mtask <- nextScheduledJob con
  case mtask of
    Nothing -> return Nothing
    Just task -> do
      amount <- amountToSleep task
      if amount > 0
        then do
          -- Wakeup is too early.
          putStrLn $ "Next task: " ++ T.unpack (taskMethod task) ++ " @ " ++
            formatTime locale format (taskWhen task)
          hFlush stdout
          return $ Just amount
        else do
          putStrLn $ "Running: " ++ T.unpack (taskMethod task) ++ " @ " ++
            formatTime locale format (taskWhen task)
          hFlush stdout

          _ <- execute con
            "INSERT INTO queue_classic_jobs (q_name, method, args) VALUES (?, ?, ?)"
            (taskQueueName task, taskMethod task, taskArguments task)

          -- Remove this task.
          _ <- execute con
            "DELETE FROM scheduled_jobs WHERE ID=?" [taskId task]

          -- Select next task and return how long to wait.
          mtask' <- nextScheduledJob con
          case mtask' of
            Nothing -> return Nothing
            Just task' -> do
              amount' <- amountToSleep task'
              putStrLn $ "Next task: " ++ T.unpack (taskMethod task') ++ " @ " ++
                formatTime locale format (taskWhen task')
              hFlush stdout
              return $ Just amount'
  where
  locale = defaultTimeLocale
  format = iso8601DateFormat $ Just "%H:%M:%S"
runTasks :: Connection -> IO (Maybe Int)
runTasks con = do
  mtask <- nextScheduledJob con
  case mtask of
    Nothing -> return Nothing
    Just task -> do
      amount <- amountToSleep task
      if amount > 0
        then do
          -- Wakeup is too early.
          putStrLn $ "Next task: " ++ T.unpack (taskMethod task) ++ " @ " ++
            formatTime locale format (taskWhen task)
          hFlush stdout
          return $ Just amount
        else do
          putStrLn $ "Running: " ++ T.unpack (taskMethod task) ++ " @ " ++
            formatTime locale format (taskWhen task)
          hFlush stdout

          _ <- execute con
            "INSERT INTO queue_classic_jobs (q_name, method, args) VALUES (?, ?, ?)"
            (taskQueueName task, taskMethod task, taskArguments task)

          -- Remove this task.
          _ <- execute con
            "DELETE FROM scheduled_jobs WHERE ID=?" [taskId task]

          -- Select next task and return how long to wait.
          mtask' <- nextScheduledJob con
          case mtask' of
            Nothing -> return Nothing
            Just task' -> do
              amount' <- amountToSleep task'
              putStrLn $ "Next task: " ++ T.unpack (taskMethod task') ++ " @ " ++
                formatTime locale format (taskWhen task')
              hFlush stdout
              return $ Just amount'
  where
  locale = defaultTimeLocale
  format = iso8601DateFormat $ Just "%H:%M:%S"
runTasks con = do
  mtask <- nextScheduledJob con
  case mtask of
    Nothing -> return Nothing
    Just task -> do
      amount <- amountToSleep task
      if amount > 0
        then do
          -- Wakeup is too early.
          putStrLn $ "Next task: " ++ T.unpack (taskMethod task) ++ " @ " ++
            formatTime locale format (taskWhen task)
          hFlush stdout
          return $ Just amount
        else do
          putStrLn $ "Running: " ++ T.unpack (taskMethod task) ++ " @ " ++
            formatTime locale format (taskWhen task)
          hFlush stdout

          _ <- execute con
            "INSERT INTO queue_classic_jobs (q_name, method, args) VALUES (?, ?, ?)"
            (taskQueueName task, taskMethod task, taskArguments task)

          -- Remove this task.
          _ <- execute con
            "DELETE FROM scheduled_jobs WHERE ID=?" [taskId task]

          -- Select next task and return how long to wait.
          mtask' <- nextScheduledJob con
          case mtask' of
            Nothing -> return Nothing
            Just task' -> do
              amount' <- amountToSleep task'
              putStrLn $ "Next task: " ++ T.unpack (taskMethod task') ++ " @ " ++
                formatTime locale format (taskWhen task')
              hFlush stdout
              return $ Just amount'
  where
  locale = defaultTimeLocale
  format = iso8601DateFormat $ Just "%H:%M:%S"