You are here

public function Cron::runTasks in Anti Spam by CleanTalk 8.4

Same name and namespace in other branches
  1. 9.1.x src/lib/Cleantalk/Common/Cron.php \Cleantalk\Common\Cron::runTasks()

Run all tasks from $this->tasks_to_run. Saving all results to (array) $this->tasks_completed

Parameters

$tasks:

Return value

void|array Array of completed and uncompleted tasks.

File

src/lib/Cleantalk/Common/Cron.php, line 213

Class

Cron
CleanTalk Cron class

Namespace

Cleantalk\Common

Code

public function runTasks($tasks) {
  if (empty($tasks)) {
    return;
  }
  if (!$this
    ->setCronLastStart()) {
    return;
  }
  foreach ($tasks as $task) {
    if (is_callable($this->tasks[$task]['handler'])) {
      if ($this->debug) {
        error_log(var_export('Task ' . $task . ' will be run.', 1));
      }
      $result = call_user_func_array($this->tasks[$task]['handler'], isset($this->tasks[$task]['params']) ? $this->tasks[$task]['params'] : array());
      if ($this->debug) {
        error_log(var_export('Result:', 1));
        error_log(var_export($result, 1));
      }
      if (empty($result['error'])) {
        $this->tasks_completed[$task] = true;
        if ($this->tasks[$task]['period'] == 0) {

          // One time scheduled event
          unset($this->tasks[$task]);
        }
        else {

          // Multi time scheduled event
          $this->tasks[$task]['next_call'] = time() + $this->tasks[$task]['period'];
        }
      }
      else {
        $this->tasks_completed[$task] = false;
        $this->tasks[$task]['next_call'] = time() + $this->tasks[$task]['period'] / 4;
      }
    }
    else {
      $this->tasks_completed[$task] = false;
    }
  }
  $this
    ->saveTasks($this->tasks);
  return $this->tasks_completed;
}