فهرست منبع

Ticket #4829 - Background jobs.

AntonLV 1 سال پیش
والد
کامیت
36cd1c1bac
4فایلهای تغییر یافته به همراه197 افزوده شده و 0 حذف شده
  1. 108 0
      inc/classes/BxDolBackgroundJobs.php
  2. 71 0
      inc/classes/BxDolBackgroundJobsQuery.php
  3. 18 0
      inc/classes/BxDolCronBackgroundJobs.php
  4. 0 0
      install/sql/system.sql

+ 108 - 0
inc/classes/BxDolBackgroundJobs.php

@@ -0,0 +1,108 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaCore UNA Core
+ * @{
+ */
+
+class BxDolBackgroundJobs  extends BxDolFactory implements iBxDolSingleton
+{
+    protected $_sObjectLog;
+    protected $_oQuery;
+    
+
+    protected function __construct()
+    {
+        if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
+            trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
+
+        parent::__construct();
+        
+        $this->_sObjectLog = 'sys_background_jobs';
+
+        $this->_oQuery = new BxDolBackgroundJobsQuery();
+    }
+
+    /**
+     * Get singleton instance of the class
+     */
+    public static function getInstance()
+    {
+        if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
+            $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolBackgroundJobs();
+
+        return $GLOBALS['bxDolClasses'][__CLASS__];
+    }
+
+    /**
+     * Prevent cloning the instance
+     */
+    public function __clone()
+    {
+        if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
+            trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
+    }
+
+    public function add($sName, $mixedServiceCall, $iPriority = 0)
+    {
+        if(is_array($mixedServiceCall))
+            $mixedServiceCall = call_user_func_array(['BxDolService', 'getSerializedService'], $mixedServiceCall);
+
+        if(!$this->_oQuery->addJob($sName, $mixedServiceCall, $iPriority))
+            return false;
+
+        bx_log($this->_sObjectLog, "Added: " . $sName);
+
+        return true;
+    }
+
+    public function delete($sName)
+    {
+        if(!$this->_oQuery->deleteJob($sName))
+            return false;
+
+        bx_log($this->_sObjectLog, "Deleted: " . $sName);
+
+        return true;
+    }
+
+    public function process($mixedJob)
+    {
+        if(!empty($mixedJob) && !is_array($mixedJob))
+            $mixedJob = $this->_oQuery->getJobs(['sample' => 'name', 'name' => $mixedJob]);
+
+        if(empty($mixedJob) || !is_array($mixedJob))
+            return false;
+
+        if(empty($mixedJob['service_call']) || !BxDolService::isSerializedService($mixedJob['service_call']))
+            return false;
+
+        BxDolService::callSerialized($mixedJob['service_call']);
+
+        $this->_oQuery->deleteJob($mixedJob['name']);
+
+        bx_log($this->_sObjectLog, "Processed: " . $mixedJob['name']);
+
+        return true;
+    }
+
+    public function processAll()
+    {
+        $aJobs = $this->_oQuery->getJobs(['sample' => 'process', 'with_priority' => true]);
+        if(empty($aJobs) || !is_array($aJobs))
+            return true;
+
+        $iProcessed = 0;
+        foreach($aJobs as $aJob)
+            if($this->process($aJob))
+                $iProcessed += 1;
+
+        bx_log($this->_sObjectLog, "Processed: all (" . $iProcessed . " from " . count($aJobs) . ")");
+
+        return true;
+    }
+}
+
+/** @} */

+ 71 - 0
inc/classes/BxDolBackgroundJobsQuery.php

@@ -0,0 +1,71 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaCore UNA Core
+ * @{
+ */
+
+/**
+ * @see BxDolBackgroundJobs
+ */
+class BxDolBackgroundJobsQuery extends BxDolDb
+{
+    public function __construct()
+    {
+    	parent::__construct();
+    }
+
+    public function getJobs($aParams = [])
+    {
+        $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
+
+    	$sSelectClause = "*";
+    	$sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
+        switch($aParams['sample']) {
+            case 'name':
+                $aMethod['name'] = 'getRow';
+            	$aMethod['params'][1] = [
+                    'name' => $aParams['name']
+                ];
+
+                $sWhereClause = " AND `name`=:name";
+                break;
+
+            case 'process':
+                $sOrderClause = "`added` ASC";
+                if(isset($aParams['with_priority']) && $aParams['with_priority'] === true)
+                    $sOrderClause = "`priority` DESC, " . $sOrderClause;
+                break;
+        }
+
+        $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
+        $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
+
+        $aMethod['params'][0] = "SELECT
+                " . $sSelectClause . "
+            FROM `sys_background_jobs` " . $sJoinClause . "
+            WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
+
+        return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
+    }
+
+    public function addJob($sName, $sServiceCall, $iPriority = 0)
+    {
+        return $this->query("INSERT INTO `sys_background_jobs` SET `name` = :name, `added`=UNIX_TIMESTAMP(), `priority` = :priority, `service_call`=:service_call ON DUPLICATE KEY UPDATE `added`=UNIX_TIMESTAMP(), `priority` = :priority, `service_call`=:service_call", [
+            'name' => $sName,
+            'priority' => $iPriority,
+            'service_call' => $sServiceCall
+        ]) !== false;
+    }
+
+    public function deleteJob($sName)
+    {
+        return $this->query("DELETE FROM `sys_background_jobs` WHERE `name`=:name", [
+            'name' => $sName
+        ]) !== false;
+    }
+}
+
+/** @} */

+ 18 - 0
inc/classes/BxDolCronBackgroundJobs.php

@@ -0,0 +1,18 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+* Copyright (c) UNA, Inc - https://una.io
+* MIT License - https://opensource.org/licenses/MIT
+*
+* @defgroup    UnaCore UNA Core
+* @{
+*/
+
+class BxDolCronBackgroundJobs extends BxDolCron
+{
+    public function processing()
+    {
+        BxDolBackgroundJobs::getInstance()->processAll();
+    }
+}
+
+/** @} */

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 0 - 0
install/sql/system.sql


برخی فایل ها در این مقایسه diff نمایش داده نمی شوند زیرا تعداد فایل ها بسیار زیاد است