Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
JobQueue.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
40  class JobQueue extends CApplicationComponent
41  {
42  const MAX_DELAY_NOISE_IN_SECONDS = 15;
43 
44  protected $queuedJobs = array();
45 
51  public function add($jobType, $delay = 0, $params = array())
52  {
53  assert('is_string($jobType)');
54  assert('is_int($delay)');
55  assert('is_array($params)');
56  if (!$this->isJobTypeWithDelayAndParamsAlreadyExistInQueueAndReplaceItInQueue($jobType, $delay, $params, true))
57  {
58  $this->queuedJobs[$delay][] = array(
59  'jobType' => $jobType,
60  'params' => $params
61  );
62  }
63  }
64 
73  protected function isJobTypeWithDelayAndParamsAlreadyExistInQueueAndReplaceItInQueue($jobType, $delay, $params,
74  $replaceExistingJobInQueueWithLatterOne = false)
75  {
76  assert('is_string($jobType)');
77  assert('is_int($delay)');
78  assert('is_array($params)');
79  assert('is_bool($replaceExistingJobInQueueWithLatterOne)');
80  foreach ($this->queuedJobs as $existingJobDelay => $queuedJobs)
81  {
82  foreach ($queuedJobs as $key => $queuedJob)
83  {
84  if ($queuedJob['jobType'] == $jobType &&
85  $this->isDelayWithinAcceptableTolerance($existingJobDelay, $delay, self::MAX_DELAY_NOISE_IN_SECONDS))
86  {
87  if ($queuedJob['params'] == $params)
88  {
89  if ($replaceExistingJobInQueueWithLatterOne)
90  {
91  unset($this->queuedJobs[$existingJobDelay][$key]);
92  if (empty($this->queuedJobs[$existingJobDelay]))
93  {
94  unset($this->queuedJobs[$existingJobDelay]);
95  }
96  $this->queuedJobs[$delay][] = array(
97  'jobType' => $jobType,
98  'params' => $params
99  );
100  }
101  return true;
102  }
103  }
104  }
105  }
106  return false;
107  }
108 
116  protected function isDelayWithinAcceptableTolerance($existingJobDelay, $delay, $noise)
117  {
118  if ($existingJobDelay <= $delay &&
119  $existingJobDelay + $noise >= $delay)
120  {
121  return true;
122  }
123  return false;
124  }
125 
126  public function getAll()
127  {
128  return $this->queuedJobs;
129  }
130 
131  public function deleteAll()
132  {
133  $this->queuedJobs = array();
134  }
135 
139  public function processAll()
140  {
141  }
142 
147  public function isEnabled()
148  {
149  return false;
150  }
151 
152  public function getQueueJobLabel()
153  {
154  return Zurmo::t('JobsManagerModule', 'Queue Job');
155  }
156 
157  public function getQueueJobAgainLabel()
158  {
159  return Zurmo::t('JobsManagerModule', 'Queue Job Again');
160  }
161 
162  public function processByJobTypeAndDelay($jobType, $delay, MessageLogger $messageLogger)
163  {
164  }
165 
174  public function resolveToAddJobTypeByModelByDateTimeAttribute(RedBeanModel $model, $attributeName, $jobType, $resolveForOldModel = false)
175  {
176  assert('is_string($attributeName)');
177  assert('is_string($jobType)');
178  if ($model->getIsNewModel() || isset($model->originalAttributeValues[$attributeName]) || $resolveForOldModel)
179  {
180  if (DateTimeUtil::isDateTimeStringNull($model->{$attributeName}))
181  {
182  $secondsFromNow = 0;
183  }
184  else
185  {
186  $processDateTimeStamp = DateTimeUtil::convertDbFormatDateTimeToTimestamp($model->{$attributeName});
187  $secondsFromNow = $processDateTimeStamp - time();
188  }
189  if ($secondsFromNow <= 0)
190  {
191  $delay = 0;
192  }
193  else
194  {
195  $delay = $secondsFromNow;
196  }
197  Yii::app()->jobQueue->add($jobType, $delay + 5);
198  }
199  }
200  }
201 ?>
isEnabled()
Definition: JobQueue.php:147
add($jobType, $delay=0, $params=array())
Definition: JobQueue.php:51
isDelayWithinAcceptableTolerance($existingJobDelay, $delay, $noise)
Definition: JobQueue.php:116
isJobTypeWithDelayAndParamsAlreadyExistInQueueAndReplaceItInQueue($jobType, $delay, $params, $replaceExistingJobInQueueWithLatterOne=false)
Definition: JobQueue.php:73
resolveToAddJobTypeByModelByDateTimeAttribute(RedBeanModel $model, $attributeName, $jobType, $resolveForOldModel=false)
Definition: JobQueue.php:174
processAll()
Definition: JobQueue.php:139
Generated on Wed Sep 30 2020 07:10:28
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.