OpportunisticQMPlugin.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. class OpportunisticQMPlugin extends Plugin {
  3. const PLUGIN_VERSION = '3.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', ['action' => 'runqueue']);
  11. }
  12. /**
  13. * When the page has finished rendering, let's do some cron jobs
  14. * if we have the time.
  15. */
  16. public function onEndActionExecute(Action $action)
  17. {
  18. if ($action instanceof RunqueueAction) {
  19. return true;
  20. }
  21. global $_startTime;
  22. $args = ['qmkey' => common_config('opportunisticqm', 'qmkey'),
  23. 'max_execution_time' => $this->secs_per_action,
  24. 'started_at' => $this->rel_to_pageload ? $_startTime : null,
  25. 'verbosity' => $this->verbosity];
  26. $qm = new OpportunisticQueueManager($args);
  27. $qm->runQueue();
  28. return true;
  29. }
  30. public function onPluginVersion(array &$versions): bool
  31. {
  32. $versions[] = array('name' => 'OpportunisticQM',
  33. 'version' => self::PLUGIN_VERSION,
  34. 'author' => 'Mikael Nordfeldth',
  35. 'homepage' => GNUSOCIAL_ENGINE_URL,
  36. 'description' =>
  37. // TRANS: Plugin description.
  38. _m('Opportunistic queue manager plugin for background processing.'));
  39. return true;
  40. }
  41. }