2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
41 class SchedulersJob extends SugarBean {
45 var $date_entered = '';
46 var $date_modified = '';
47 var $scheduler_id = '';
48 var $execute_time = '';
50 // standard SugarBean child attrs
51 var $table_name = "schedulers_times";
52 var $object_name = "SchedulersJob";
53 var $module_dir = "SchedulersJobs";
54 var $new_schema = true;
55 var $process_save_dates = true;
57 var $job_name; // the Scheduler's 'name' field
58 var $job; // the Scheduler's 'job' field
59 // object specific attributes
60 var $user; // User object
61 var $scheduler; // Scheduler parent
66 function SchedulersJob($init=true) {
72 $user->retrieve('1'); // Scheduler jobs run as Admin
78 ///////////////////////////////////////////////////////////////////////////
79 //// SCHEDULERSJOB HELPER FUNCTIONS
81 function fireSelf($id) {
83 $sched = new Scheduler();
84 $sched->retrieve($id);
86 $exJob = explode('::', $sched->job);
88 if(is_array($exJob)) {
89 $this->scheduler_id = $sched->id;
90 $this->scheduler = $sched;
91 $this->execute_time = $this->handleDateFormat('now');
94 if($exJob[0] == 'function') {
95 $GLOBALS['log']->debug('----->Scheduler found a job of type FUNCTION');
96 require_once('modules/Schedulers/_AddJobsHere.php');
101 $GLOBALS['log']->debug('----->SchedulersJob firing '.$func);
103 $res = call_user_func($func);
105 $this->setJobFlag(2);
109 $this->setJobFlag(3);
112 } elseif($exJob[0] == 'url') {
113 if(function_exists('curl_init')) {
114 $GLOBALS['log']->debug('----->SchedulersJob found a job of type URL');
115 $this->setJobFlag(1);
117 $GLOBALS['log']->debug('----->SchedulersJob firing URL job: '.$exJob[1]);
118 if($this->fireUrl($exJob[1])) {
119 $this->setJobFlag(2);
123 $this->setJobFlag(3);
127 $this->setJobFlag(4);
136 * handles some date/time foramtting
137 * @param string time Time (usually "now")
138 * @param object user User, usually admin (id = '1')
139 * @return string formatted time.
141 function handleDateFormat($time, $user=null) {
142 $timedate = TimeDate::getInstance();
145 $user = (empty($user)) ? $this->user : $user;
146 $dbTime = $timedate->nowDb();
148 $ret = $timedate->to_display_date_time($dbTime, true, true, $user);
152 function setJobFlag($flag) {
153 $trackerManager = TrackerManager::getInstance();
154 $trackerManager->pause();
155 $status = array (0 => 'ready', 1 => 'in progress', 2 => 'completed', 3 => 'failed', 4 => 'no curl');
156 $statusScheduler = array (0 => 'Active', 1 => 'In Progress', 2 => 'Active', 3 => 'Active', 4 => 'Active');
157 $GLOBALS['log']->info('-----> SchedulersJob setting Job flag: '.$status[$flag].' AND setting Scheduler status to: '.$statusScheduler[$flag]);
159 $time = $this->handleDateFormat('now');
160 $this->status = $status[$flag];
161 $this->scheduler->retrieve($this->scheduler_id);
162 $this->scheduler->status = $statusScheduler[$flag];
163 $this->scheduler->save();
165 $this->retrieve($this->id);
166 $trackerManager->unPause();
170 * This function takes a job_id, and updates schedulers last_run as well as
171 * soft delete the job instance from schedulers_times
172 * @return boolean Success
174 function finishJob() {
175 $trackerManager = TrackerManager::getInstance();
176 $trackerManager->pause();
177 $GLOBALS['log']->debug('----->SchedulersJob updating Job Status and finishing Job execution.');
178 $this->scheduler->retrieve($this->scheduler->id);
179 $this->scheduler->last_run = TimeDate::getInstance()->nowDb();
180 $this->scheduler->save();
181 $trackerManager->unPause();
185 * This function takes a passed URL and cURLs it to fake multi-threading with another httpd instance
186 * @param $job String in URI-clean format
187 * @param $timeout Int value in secs for cURL to timeout. 30 default.
189 //TODO: figure out what error is thrown when no more apache instances can be spun off
190 function fireUrl($job, $timeout=30) {
193 curl_setopt($ch, CURLOPT_URL, $job); // set url
194 curl_setopt($ch, CURLOPT_FAILONERROR, true); // silent failure (code >300);
195 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // do not follow location(); inits - we always use the current
196 curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
197 curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false); // not thread-safe
198 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return into a variable to continue program execution
199 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // never times out - bad idea?
200 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // 5 secs for connect timeout
201 curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); // open brand new conn
202 curl_setopt($ch, CURLOPT_HEADER, true); // do not return header info with result
203 curl_setopt($ch, CURLOPT_NOPROGRESS, true); // do not have progress bar
204 $urlparts = parse_url($job);
205 if(empty($urlparts['port'])) {
206 if($urlparts['scheme'] == 'https'){
207 $urlparts['port'] = 443;
209 $urlparts['port'] = 80;
212 curl_setopt($ch, CURLOPT_PORT, $urlparts['port']); // set port as reported by Server
213 //TODO make the below configurable
214 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // most customers will not have Certificate Authority account
215 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // most customers will not have Certificate Authority account
217 if(constant('PHP_VERSION') > '5.0.0') {
218 curl_setopt($ch, CURLOPT_NOSIGNAL, true); // ignore any cURL signals to PHP (for multi-threading)
220 $result = curl_exec($ch);
221 $cInfo = curl_getinfo($ch); //url,content_type,header_size,request_size,filetime,http_code
222 //ssl_verify_result,total_time,namelookup_time,connect_time
223 //pretransfer_time,size_upload,size_download,speed_download,
224 //speed_upload,download_content_length,upload_content_length
225 //starttransfer_time,redirect_time
228 if($result !== FALSE && $cInfo['http_code'] < 400) {
229 $GLOBALS['log']->debug('----->Firing was successful: ('.$job.') at '.$this->handleDateFormat('now'));
230 $GLOBALS['log']->debug('----->WTIH RESULT: '.strip_tags($result).' AND '.strip_tags(print_r($cInfo, true)));
233 $GLOBALS['log']->fatal('Job errored: ('.$job.') at '.$this->handleDateFormat('now'));
237 //// END SCHEDULERSJOB HELPER FUNCTIONS
238 ///////////////////////////////////////////////////////////////////////////
241 ///////////////////////////////////////////////////////////////////////////
242 //// STANDARD SUGARBEAN OVERRIDES
244 * This function gets DB data and preps it for ListViews
246 function get_list_view_data(){
249 $temp_array = $this->get_list_view_array();
250 $temp_array['JOB_NAME'] = $this->job_name;
251 $temp_array['JOB'] = $this->job;
256 /** method stub for future customization
259 function fill_in_additional_list_fields() {
260 $this->fill_in_additional_detail_fields();
263 function fill_in_additional_detail_fields() {
264 // get the Job Name and Job fields from schedulers table
265 // $q = "SELECT name, job FROM schedulers WHERE id = '".$this->job_id."'";
266 // $result = $this->db->query($q);
267 // $row = $this->db->fetchByAssoc($result);
268 // $this->job_name = $row['name'];
269 // $this->job = $row['job'];
270 // $GLOBALS['log']->info('Assigned Name('.$this->job_name.') and Job('.$this->job.') to Job');
272 // $this->created_by_name = get_assigned_user_name($this->created_by);
273 // $this->modified_by_name = get_assigned_user_name($this->modified_user_id);
278 * returns the bean name - overrides SugarBean's
280 function get_summary_text() {
281 if(isset($this->name))
286 * function overrides the one in SugarBean.php