Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
cron_job_queue_scheduled_job.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/system/cron/cron_job/cron_job.inc';
19 require_once SQ_FUDGE_PATH.'/db_extras/db_extras.inc';
20 
33 {
34 
35 
42  function __construct($assetid=0)
43  {
44  parent::__construct($assetid);
45 
46  }//end constructor
47 
48 
60  function create(&$link)
61  {
62  require_once SQ_CORE_PACKAGE_PATH.'/system/system_asset_fns.inc';
63 
64  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
65  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
66 
67  if ($linkid = parent::create($link)) {
68  if (!system_asset_fns_create_cleanup($this)) {
69  $linkid = FALSE;
70  }
71  }
72 
73  if ($linkid) {
74  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
75  } else {
76  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
77  }
78 
79  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
80  return $linkid;
81 
82  }//end create()
83 
84 
94  function _getName($short_name=FALSE)
95  {
96  return $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->type(), 'name');
97 
98  }//end _getName()
99 
100 
107  function canClone()
108  {
109  return FALSE;
110 
111  }//end canClone()
112 
113 
125  function morph($new_type_code)
126  {
127  trigger_localised_error('CRON0024', E_USER_WARNING, $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->type(), 'name'));
128  return FALSE;
129 
130  }//end morph()
131 
132 
141  function canDelete()
142  {
143  return FALSE;
144 
145  }//end canDelete()
146 
147 
161  function _exec(&$msg)
162  {
163  $jobs = $this->getBulkmailJobs();
164  if (empty($jobs)) return SQ_CRON_JOB_COMPLETED;
165 
166  $now = time();
167 
168  $am = $GLOBALS['SQ_SYSTEM']->am;
169  $cm = $am->getSystemAsset('cron_manager');
170  $bm = $am->getSystemAsset('bulkmail_manager');
171  $last_run = $cm->attr('last_run');
172 
173  $multiplier = Array(
174  'hourly' => (60 * 60),
175  'daily' => (60 * 60 * 24),
176  'weekly' => (60 * 60 * 24 * 7),
177  'monthly' => (60 * 60 * 24 * 30),
178  'yearly' => (60 * 60 * 24 * 365),
179  );
180 
181  foreach ($jobs as $job_id => $schedule) {
182  $frequency = array_get_index(array_keys($schedule), 0);
183  $date_time = $schedule[$frequency];
184  $date_time_ts = strtotime($date_time);
185 
186  // if the starting time of the scheduling of jobs is in future ignore it
187  if ($date_time_ts > $now) continue;
188 
189  // check to see if the jb is already queued up that way even
190  // if it is our time to queue up a job we need not bother
191  $queued_jobs = $bm->getQueuedJobs($job_id);
192  if (!empty($queued_jobs)) continue;
193 
194  $bulkmail_job = $am->getAsset($job_id);
195  $last_job_run = $bulkmail_job->attr('last_scheduled_run');
196 
197  if (!$last_job_run && $date_time_ts < $now) {
198  // if the job isn't scheduled before the first time
199  // it needs to be scheduled is its epoch time
200  $bulkmail_job->processStatusChange(SQ_STATUS_LIVE);
201  $am->acquireLock($bulkmail_job->id, 'all');
202  $bulkmail_job->setAttrValue('last_scheduled_run', $now);
203  $bulkmail_job->saveAttributes();
204  $am->releaseLock($bulkmail_job->id, 'all');
205  continue;
206  }
207 
208  // we have the last time the bulkmail job was queue up time
209  // and we also have the time interval for it to be queued up
210  // calculate and check to see if the frecuency for next
211  // queuing up match our past and current cron manager run
212  $next_run_for_bulkmail = $last_job_run + $multiplier[$frequency];
213 
214  if ($next_run_for_bulkmail < $now && $next_run_for_bulkmail > $last_run) {
215  // we have calculated the next time weneed to queue the bulkmail job
216  // now queue it up if that time is after the last cron run but before now
217  $bulkmail_job->processStatusChange(SQ_STATUS_LIVE);
218  $am->acquireLock($bulkmail_job->id, 'all');
219  $bulkmail_job->setAttrValue('last_scheduled_run', $now);
220  $bulkmail_job->saveAttributes();
221  $am->releaseLock($bulkmail_job->id, 'all');
222  continue;
223  }
224  }
225 
226  return SQ_CRON_JOB_COMPLETED;
227 
228  }//end _exec()
229 
230 
231  function getBulkmailJobs()
232  {
233  $res = Array();
234 
235  $all_bulkmail_jobs = $GLOBALS['SQ_SYSTEM']->am->getTypeAssetids('bulkmail_job');
236  foreach ($all_bulkmail_jobs as $job_id) {
237  $bulkmail_job = $GLOBALS['SQ_SYSTEM']->am->getAsset($job_id);
238  // check to see fi the scheduling is enabled
239  if (!$bulkmail_job->attr('scheduling_status')) continue;
240  $schedule = $bulkmail_job->attr('scheduled_time');
241  // if there is no schedule configured we don't need this either
242  if (empty($schedule)) continue;
243  $res[$job_id] = $schedule;
244 
245  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($bulkmail_job, TRUE);
246  unset($bulkmail_job);
247  }
248 
249  return $res;
250 
251  }//end getBulkmailJobs()
252 
253 
254 }//end class
255 
256 ?>