Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
JobsManagerUtil.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 
41  {
54  public static function runFromJobManagerCommandOrBrowser($type, $timeLimit, $messageLoggerClassName,
55  & $isJobInProgress, $useMessageStreamer = true, $template = "{message}\n",
56  $lineBreak = "\n")
57  {
58  assert('is_string($type)');
59  assert('is_int($timeLimit)');
60  assert('is_string($messageLoggerClassName) && (
61  is_subclass_of($messageLoggerClassName, "MessageLogger") ||
62  $messageLoggerClassName == "MessageLogger")');
63  assert('is_bool($isJobInProgress)');
64  assert('is_string($template)');
65  assert('is_string($lineBreak)');
66  set_time_limit($timeLimit);
67 
68  $jobManagerFileLogger = Yii::createComponent(
69  array(
70  'class' => 'application.modules.jobsManager.components.JobManagerFileLogger',
71  'maxFileSize' => '5120',
72  'logFile' => $type . '.log',
73  'logPath' => Yii::app()->getRuntimePath() . DIRECTORY_SEPARATOR . 'jobLogs'
74  )
75  );
76 
77  $jobManagerFileMessageStreamer = new JobManagerFileLogRouteMessageStreamer("{message}\n", $jobManagerFileLogger);
78  $messageStreamer = new MessageStreamer($template);
79  $messageStreamer->setExtraRenderBytes(0);
80 
81  if ($useMessageStreamer)
82  {
83  $messageLogger = new $messageLoggerClassName(array($messageStreamer, $jobManagerFileMessageStreamer));
84  }
85  else
86  {
87  $messageLogger = new $messageLoggerClassName(array($jobManagerFileMessageStreamer));
88  }
89 
90  $streamers = array($messageStreamer, $jobManagerFileMessageStreamer);
91  if (Yii::app()->isApplicationInMaintenanceMode())
92  {
93  foreach ($streamers as $streamer)
94  {
95  $streamer->add(Zurmo::t('JobsManagerModule', '{dateTimeString} Application is in maintenanceMode and {type}Job can not be started.',
96  array('{type}' => $type, '{dateTimeString}' => static::getLocalizedDateTimeTimeZoneString())));
97  }
98  }
99  else
100  {
101  foreach ($streamers as $streamer)
102  {
103  $streamer->add(Zurmo::t('JobsManagerModule', 'Script will run at most for {seconds} seconds.',
104  array('{seconds}' => $timeLimit)));
105  $streamer->add(Zurmo::t('JobsManagerModule', 'Sending output to runtime/jobLogs/{type}.log',
106  array('{type}' => $type)));
107  $streamer->add(Zurmo::t('JobsManagerModule', '{dateTimeString} Starting job type: {type}',
108  array('{type}' => $type,
109  '{dateTimeString}' => static::getLocalizedDateTimeTimeZoneString())));
110  }
111 
112  $messageLogger->addInfoMessage(Zurmo::t('JobsManagerModule', 'Script will run at most for {seconds} seconds.',
113  array('{seconds}' => $timeLimit)));
114  $messageLogger->addInfoMessage(Zurmo::t('JobsManagerModule', 'Starting job type: {type}',
115  array('{type}' => $type)));
116  $messageLogger->addDebugMessage('Showing Debug Messages');
117  if ($type == 'Monitor')
118  {
119  static::runMonitorJob($messageLogger, $isJobInProgress);
120  }
121  else
122  {
123  static::runNonMonitorJob($type, $messageLogger, $isJobInProgress);
124  }
125 
126  foreach ($streamers as $streamer)
127  {
128  $streamer->add(Zurmo::t('JobsManagerModule', '{dateTimeString} Ending job type: {type}',
129  array('{type}' => $type,
130  '{dateTimeString}' => static::getLocalizedDateTimeTimeZoneString())));
131  }
132  }
133  }
134 
139  public static function runMonitorJob(MessageLogger $messageLogger, & $isJobInProgress)
140  {
141  assert('is_bool($isJobInProgress)');
142  try
143  {
144  $jobInProcess = JobInProcess::getByType('Monitor');
145  $messageLogger->addInfoMessage("Existing monitor job detected");
146  if (static::isJobInProcessOverThreshold($jobInProcess, 'Monitor'))
147  {
148  $messageLogger->addInfoMessage("Existing monitor job is stuck");
149  self::makeMonitorStuckJobNotification();
150  }
151  else
152  {
153  $isJobInProgress = true;
154  }
155  }
156  catch (NotFoundException $e)
157  {
158  $jobInProcess = new JobInProcess();
159  $jobInProcess->type = 'Monitor';
160  $jobInProcess->save();
161  $startDateTime = $jobInProcess->createdDateTime;
162  $job = new MonitorJob();
163  $job->setMessageLogger($messageLogger);
164  $ranSuccessfully = $job->run();
165  $jobInProcess->delete();
166  $jobLog = new JobLog();
167  $jobLog->type = 'Monitor';
168  $jobLog->startDateTime = $startDateTime;
169  $jobLog->endDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
170  if ($ranSuccessfully)
171  {
172  $messageLogger->addInfoMessage("Monitor Job completed successfully");
173  $jobLog->status = JobLog::STATUS_COMPLETE_WITHOUT_ERROR;
174  }
175  else
176  {
177  $messageLogger->addInfoMessage("Monitor Job completed with errors");
178  $jobLog->status = JobLog::STATUS_COMPLETE_WITH_ERROR;
179  }
180  $jobLog->isProcessed = false;
181  $jobLog->save();
182  }
183  }
184 
185  public static function makeMonitorStuckJobNotification()
186  {
187  $message = new NotificationMessage();
188  $message->textContent = MonitorJob::getStuckStringContent();
189  $message->htmlContent = MonitorJob::getStuckStringContent();
190  $rules = new StuckMonitorJobNotificationRules();
191  NotificationsUtil::submit($message, $rules);
192  }
193 
201  public static function runNonMonitorJob($type, MessageLogger $messageLogger, & $isJobInProgress)
202  {
203  assert('is_string($type) && $type != "Monitor"');
204  assert('is_bool($isJobInProgress)');
205  try
206  {
208  $messageLogger->addInfoMessage("Existing job detected");
209  $isJobInProgress = true;
210  }
211  catch (NotFoundException $e)
212  {
213  $jobInProcess = new JobInProcess();
214  $jobInProcess->type = $type;
215  $jobInProcess->save();
216  $startDateTime = $jobInProcess->createdDateTime;
217  $jobClassName = $type . 'Job';
218  $job = new $jobClassName();
219  $job->setMessageLogger($messageLogger);
220  $ranSuccessfully = $job->run();
221  $errorMessage = $job->getErrorMessage();
222  $jobInProcess->delete();
223  $jobLog = new JobLog();
224  $jobLog->type = $type;
225  $jobLog->startDateTime = $startDateTime;
226  $jobLog->endDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
227  if ($ranSuccessfully)
228  {
229  $messageLogger->addInfoMessage("Job completed successfully");
230  $jobLog->status = JobLog::STATUS_COMPLETE_WITHOUT_ERROR;
231  }
232  else
233  {
234  $messageLogger->addInfoMessage("Job completed with errors");
235  $jobLog->status = JobLog::STATUS_COMPLETE_WITH_ERROR;
236  $jobLog->message = $errorMessage;
237  }
238  $jobLog->isProcessed = false;
239  if (!$jobLog->save())
240  {
241  throw new FailedToSaveModelException();
242  }
243  $stuckJob = StuckJob::getByType($type);
244  $stuckJob->quantity = 0;
245  if (!$stuckJob->save())
246  {
247  throw new FailedToSaveModelException();
248  }
249  }
250  }
251 
259  public static function isJobInProcessOverThreshold(JobInProcess $jobInProcess, $type)
260  {
261  assert('is_string($type) && $type != ""');
262 
263  $createdTimeStamp = DateTimeUtil::convertDbFormatDateTimeToTimestamp($jobInProcess->createdDateTime);
264  $nowTimeStamp = time();
265  $jobClassName = $type . 'Job';
266  $thresholdSeconds = $jobClassName::getRunTimeThresholdInSeconds();
267  if (($nowTimeStamp - $createdTimeStamp) > $thresholdSeconds)
268  {
269  return true;
270  }
271  return false;
272  }
273 
274  protected static function getLocalizedDateTimeTimeZoneString()
275  {
276  $content = DateTimeUtil::convertDbFormattedDateTimeToLocaleFormattedDisplay(
277  DateTimeUtil::convertTimestampToDbFormatDateTime(time()));
278  $content .= ' ' . Yii::app()->user->userModel->timeZone;
279  return $content;
280  }
281  }
282 ?>
static getByType($type)
Definition: StuckJob.php:49
static getStuckStringContent()
Definition: MonitorJob.php:70
static runFromJobManagerCommandOrBrowser($type, $timeLimit, $messageLoggerClassName, &$isJobInProgress, $useMessageStreamer=true, $template="{message}\n", $lineBreak="\n")
static runNonMonitorJob($type, MessageLogger $messageLogger, &$isJobInProgress)
static getByType($type)
addInfoMessage($message)
static isJobInProcessOverThreshold(JobInProcess $jobInProcess, $type)
static submit(NotificationMessage $message, NotificationRules $rules)
static runMonitorJob(MessageLogger $messageLogger, &$isJobInProgress)
Generated on Tue Jul 14 2020 07:10:33
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.