SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Controllers/WorkerJobController.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Controllers;
  15.  
  16. use Sprout\Helpers\Pdb;
  17. use Sprout\Helpers\Sprout;
  18. use Sprout\Helpers\Worker;
  19.  
  20.  
  21. /**
  22.  * Runs worker jobs (i.e. Helpers which extend {@see WorkerBase})
  23.  */
  24. class WorkerJobController extends Controller
  25. {
  26.  
  27. /**
  28.   * Actually run a worker job
  29.   * This method is almost always called from CLI
  30.   *
  31.   * @param int $job_id ID of the job to run
  32.   * @param string $job_code Random string used to protect against unauthorised access
  33.   */
  34. public function run($job_id, $job_code)
  35. {
  36. $job_id = (int) $job_id;
  37.  
  38. $q = "SELECT class_name, args FROM ~worker_jobs WHERE id = ? AND code = ?";
  39. $job = Pdb::query($q, [$job_id, $job_code], 'row');
  40.  
  41. Worker::start($job_id);
  42.  
  43. $inst = Sprout::instance($job['class_name'], 'Sprout\Helpers\WorkerBase');
  44.  
  45. $args = json_decode($job['args'], true);
  46.  
  47. call_user_func_array(array($inst, 'run'), $args);
  48. }
  49.  
  50. }
  51.