SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Controllers/Admin/WorkerJobAdminController.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\Admin;
  15.  
  16. use Kohana;
  17.  
  18. use Sprout\Helpers\AdminAuth;
  19. use Sprout\Helpers\Constants;
  20. use Sprout\Helpers\Csrf;
  21. use Sprout\Helpers\Json;
  22. use Sprout\Helpers\Notification;
  23. use Sprout\Helpers\Pdb;
  24. use Sprout\Helpers\RefineBar;
  25. use Sprout\Helpers\RefineWidgetSelect;
  26. use Sprout\Helpers\RefineWidgetTextbox;
  27. use Sprout\Helpers\Sprout;
  28. use Sprout\Helpers\Url;
  29. use Sprout\Helpers\View;
  30. use Sprout\Helpers\Worker;
  31.  
  32.  
  33. /**
  34. * Handles most processing for Worker Jobs
  35. **/
  36. class WorkerJobAdminController extends ListAdminController
  37. {
  38. protected $controller_name = 'worker_job';
  39. protected $friendly_name = 'Worker Jobs';
  40. protected $navigation_name = 'Dev tools';
  41. protected $add_defaults = array(
  42. 'active' => 1,
  43. );
  44. protected $main_order = 'item.date_added DESC';
  45. protected $main_delete = false;
  46.  
  47.  
  48. /**
  49.   * Constructor
  50.   **/
  51. public function __construct()
  52. {
  53. $this->main_columns = [
  54. 'Name' => 'name',
  55. 'Status' => 'status',
  56. 'Date' => 'date_added',
  57. ];
  58.  
  59. $this->refine_bar = new RefineBar();
  60. $this->refine_bar->setGroup('Job');
  61. $this->refine_bar->addWidget(new RefineWidgetTextbox('name', 'Name'));
  62. $this->refine_bar->addWidget(new RefineWidgetSelect('status', 'Status', Constants::$job_status));
  63.  
  64. parent::__construct();
  65. }
  66.  
  67.  
  68. /**
  69.   * Returns the contents of the navigation pane for the list
  70.   **/
  71. public function _getTools()
  72. {
  73. return null;
  74. }
  75.  
  76. public function _getNavigation()
  77. {
  78. $nav = new View('sprout/dbtools/navigation');
  79. return $nav->render();
  80. }
  81.  
  82.  
  83. /**
  84.   * Return the fields to show in the sidebar when adding or editing a record.
  85.   * These fields are shown under a heading of "Visibility"
  86.   *
  87.   * Key is the field name, value is the field label
  88.   *
  89.   * @return array
  90.   */
  91. public function _getVisibilityFields()
  92. {
  93. return [];
  94. }
  95.  
  96.  
  97. public function _getAddForm() { return "<p><i>You can't add these!</i></p>"; }
  98. public function _addSave(&$item_id) { return false; }
  99. public function _editSave($item_id) { return false; }
  100.  
  101.  
  102. /**
  103.   * UI for running worker jobs manually
  104.   */
  105. public function _extraManualRun()
  106. {
  107. $view = new View('sprout/admin/worker_job_manual_run');
  108.  
  109. return [
  110. 'title' => 'Worker job manual run',
  111. 'content' => $view->render(),
  112. ];
  113. }
  114.  
  115.  
  116. /**
  117.   * Manually runs a worker job
  118.   * Extra args can be provided as required.
  119.   **/
  120. public function manualRunAction()
  121. {
  122. AdminAuth::checkLogin();
  123. Csrf::checkOrDie();
  124.  
  125. if (empty($_POST['class_name'])) {
  126. Notification::error('You must provide a valid class name');
  127. Url::redirect('admin/extra/worker_job/manual_run');
  128. }
  129.  
  130. if (!empty($_POST['args'])) {
  131. $args = @json_decode($_POST['args'], true);
  132. if (empty($args)) {
  133. Notification::error('Unable to parse arguments JSON');
  134. Url::redirect('admin/extra/worker_job/manual_run');
  135. }
  136. } else {
  137. $args = [];
  138. }
  139.  
  140. // Instance class - this may throw an exception if class not found or invalid
  141. $inst = Sprout::instance(
  142. $_POST['class_name'],
  143. ['Sprout\\Helpers\\WorkerBase']
  144. );
  145.  
  146. // Set up worker environment
  147. header('Content-type: text/plain');
  148. Kohana::closeBuffers();
  149.  
  150. // Output the class and the args
  151. echo str_pad('Class:', 10), $_POST['class_name'], PHP_EOL;
  152. foreach ($args as $index => $arg) {
  153. if (is_array($arg)) $arg = '[array]';
  154. if (is_object($arg)) $arg = '[object]';
  155. echo str_pad("Arg {$index}:", 10), $arg, PHP_EOL;
  156. }
  157. echo str_repeat('-', 80), PHP_EOL;
  158.  
  159. Worker::$starttime = time();
  160. call_user_func_array(array($inst, 'run'), $args);
  161. }
  162.  
  163.  
  164. /**
  165.   * Return the status and log for the worker job edit view ajax update
  166.   **/
  167. public function jsonStatus($job_id)
  168. {
  169. AdminAuth::checkLogin();
  170. $job_id = (int) $job_id;
  171.  
  172. $q = "SELECT status, metric1val, metric2val, metric3val, log FROM ~worker_jobs WHERE id = ?";
  173. $row = Pdb::q($q, [$job_id], 'row');
  174.  
  175. Json::out($row);
  176. }
  177.  
  178. }
  179.  
  180.  
  181.