OpportunisticQMPlugin.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. class OpportunisticQMPlugin extends Plugin {
  3. const PLUGIN_VERSION = '2.0.0';
  4. public $qmkey = false;
  5. public $secs_per_action = 1; // total seconds to run script per action
  6. public $rel_to_pageload = true; // relative to pageload or queue start
  7. public $verbosity = 1;
  8. public function onRouterInitialized($m)
  9. {
  10. $m->connect('main/runqueue',
  11. ['action' => 'runqueue']);
  12. }
  13. /**
  14. * When the page has finished rendering, let's do some cron jobs
  15. * if we have the time.
  16. */
  17. public function onEndActionExecute(Action $action)
  18. {
  19. if ($action instanceof RunqueueAction) {
  20. return true;
  21. }
  22. global $_startTime;
  23. $args = array(
  24. 'qmkey' => common_config('opportunisticqm', 'qmkey'),
  25. 'max_execution_time' => $this->secs_per_action,
  26. 'started_at' => $this->rel_to_pageload ? $_startTime : null,
  27. 'verbosity' => $this->verbosity,
  28. );
  29. $qm = new OpportunisticQueueManager($args);
  30. $qm->runQueue();
  31. return true;
  32. }
  33. public function onPluginVersion(array &$versions)
  34. {
  35. $versions[] = array('name' => 'OpportunisticQM',
  36. 'version' => self::PLUGIN_VERSION,
  37. 'author' => 'Mikael Nordfeldth',
  38. 'homepage' => 'http://www.gnu.org/software/social/',
  39. 'description' =>
  40. // TRANS: Plugin description.
  41. _m('Opportunistic queue manager plugin for background processing.'));
  42. return true;
  43. }
  44. }