BxDolBackgroundJobs.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolBackgroundJobs extends BxDolFactory implements iBxDolSingleton
  10. {
  11. protected $_sObjectLog;
  12. protected $_oQuery;
  13. protected function __construct()
  14. {
  15. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  16. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  17. parent::__construct();
  18. $this->_sObjectLog = 'sys_background_jobs';
  19. $this->_oQuery = new BxDolBackgroundJobsQuery();
  20. }
  21. /**
  22. * Get singleton instance of the class
  23. */
  24. public static function getInstance()
  25. {
  26. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  27. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolBackgroundJobs();
  28. return $GLOBALS['bxDolClasses'][__CLASS__];
  29. }
  30. /**
  31. * Prevent cloning the instance
  32. */
  33. public function __clone()
  34. {
  35. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  36. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  37. }
  38. public function add($sName, $mixedServiceCall, $iPriority = 0)
  39. {
  40. if(is_array($mixedServiceCall))
  41. $mixedServiceCall = call_user_func_array(['BxDolService', 'getSerializedService'], $mixedServiceCall);
  42. if(!$this->_oQuery->addJob($sName, $mixedServiceCall, $iPriority))
  43. return false;
  44. bx_log($this->_sObjectLog, "Added: " . $sName);
  45. return true;
  46. }
  47. public function delete($sName)
  48. {
  49. if(!$this->_oQuery->deleteJob($sName))
  50. return false;
  51. bx_log($this->_sObjectLog, "Deleted: " . $sName);
  52. return true;
  53. }
  54. public function process($mixedJob)
  55. {
  56. if(!empty($mixedJob) && !is_array($mixedJob))
  57. $mixedJob = $this->_oQuery->getJobs(['sample' => 'name', 'name' => $mixedJob]);
  58. if(empty($mixedJob) || !is_array($mixedJob))
  59. return false;
  60. if(empty($mixedJob['service_call']) || !BxDolService::isSerializedService($mixedJob['service_call']))
  61. return false;
  62. BxDolService::callSerialized($mixedJob['service_call']);
  63. $this->_oQuery->deleteJob($mixedJob['name']);
  64. bx_log($this->_sObjectLog, "Processed: " . $mixedJob['name']);
  65. return true;
  66. }
  67. public function processAll()
  68. {
  69. $aJobs = $this->_oQuery->getJobs(['sample' => 'process', 'with_priority' => true]);
  70. if(empty($aJobs) || !is_array($aJobs))
  71. return true;
  72. $iProcessed = 0;
  73. foreach($aJobs as $aJob)
  74. if($this->process($aJob))
  75. $iProcessed += 1;
  76. bx_log($this->_sObjectLog, "Processed: all (" . $iProcessed . " from " . count($aJobs) . ")");
  77. return true;
  78. }
  79. }
  80. /** @} */