|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Queue |
|
17 * @subpackage Message |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: PlatformJob.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_Queue_Message |
|
25 */ |
|
26 require_once 'Zend/Queue/Message.php'; |
|
27 |
|
28 /** |
|
29 * Class for managing Zend Platform JobQueue jobs via Zend_Queue |
|
30 * |
|
31 * @category Zend |
|
32 * @package Zend_Queue |
|
33 * @subpackage Message |
|
34 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
35 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
36 */ |
|
37 class Zend_Queue_Message_PlatformJob extends Zend_Queue_Message |
|
38 { |
|
39 /** |
|
40 * @var ZendApi_Job |
|
41 */ |
|
42 protected $_job; |
|
43 |
|
44 /** |
|
45 * Job identifier |
|
46 * @var string |
|
47 */ |
|
48 protected $_id = null; |
|
49 |
|
50 /** |
|
51 * Constructor |
|
52 * |
|
53 * The constructor should be an array of options. |
|
54 * |
|
55 * If the option 'data' is provided, and is an instance of ZendApi_Job, |
|
56 * that object will be used as the internal job; if that option is not a |
|
57 * ZendApi_Job instance, an exception will be thrown. |
|
58 * |
|
59 * Alternately, you may specify the 'script' parameter, which should be a |
|
60 * JobQueue script the job will request. A new ZendApi_Job object will then |
|
61 * be created using that script and any options you provide. |
|
62 * |
|
63 * @param array $options |
|
64 * @return void |
|
65 * @throws Zend_Queue_Exception |
|
66 */ |
|
67 public function __construct(array $options = array()) |
|
68 { |
|
69 if (isset($options['data'])) { |
|
70 if (!($options['data'] instanceof ZendApi_Job)) { |
|
71 require_once 'Zend/Queue/Exception.php'; |
|
72 throw new Zend_Queue_Exception('Data must be an instance of ZendApi_Job'); |
|
73 } |
|
74 $this->_job = $options['data']; |
|
75 parent::__construct($this->_job->getProperties()); |
|
76 } else { |
|
77 parent::__construct($options); |
|
78 |
|
79 if (!isset($options['script'])) { |
|
80 require_once 'Zend/Queue/Exception.php'; |
|
81 throw new Zend_Queue_Exception('The script is mandatory data'); |
|
82 } |
|
83 |
|
84 $this->_job = new ZendApi_Job($options['script']); |
|
85 $this->_setJobProperties(); |
|
86 } |
|
87 } |
|
88 |
|
89 /** |
|
90 * Set the job identifier |
|
91 * |
|
92 * Used within Zend_Queue only. |
|
93 * |
|
94 * @param string $id |
|
95 * @return Zend_Queue_Message_PlatformJob |
|
96 */ |
|
97 public function setJobId($id) |
|
98 { |
|
99 $this->_id = $id; |
|
100 return $this; |
|
101 } |
|
102 |
|
103 /** |
|
104 * Retrieve the job identifier |
|
105 * |
|
106 * @return string |
|
107 */ |
|
108 public function getJobId() |
|
109 { |
|
110 return (($this->_id) ? $this->_id : $this->_job->getID()); |
|
111 } |
|
112 |
|
113 /** |
|
114 * Retrieve the internal ZendApi_Job instance |
|
115 * |
|
116 * @return ZendApi_Job |
|
117 */ |
|
118 public function getJob() |
|
119 { |
|
120 return $this->_job; |
|
121 } |
|
122 |
|
123 /** |
|
124 * Store queue and data in serialized object |
|
125 * |
|
126 * @return array |
|
127 */ |
|
128 public function __sleep() |
|
129 { |
|
130 return serialize('_job', '_id', '_data'); |
|
131 } |
|
132 |
|
133 /** |
|
134 * Query the class name of the Queue object for which this |
|
135 * Message was created. |
|
136 * |
|
137 * @return string |
|
138 */ |
|
139 public function getQueueClass() |
|
140 { |
|
141 return 'Zend_Queue_Adapter_Platform_JQ'; |
|
142 } |
|
143 |
|
144 /** |
|
145 * Sets properties on the ZendApi_Job instance |
|
146 * |
|
147 * Any options in the {@link $_data} array will be checked. Those matching |
|
148 * options in ZendApi_Job will be used to set those options in that |
|
149 * instance. |
|
150 * |
|
151 * @return void |
|
152 */ |
|
153 protected function _setJobProperties() { |
|
154 |
|
155 if (isset($this->_data['script'])) { |
|
156 $this->_job->setScript($this->_data['script']); |
|
157 } |
|
158 |
|
159 if (isset($this->_data['priority'])) { |
|
160 $this->_job->setJobPriority($this->_data['priority']); |
|
161 } |
|
162 |
|
163 if (isset($this->_data['name'])) { |
|
164 $this->_job->setJobName($this->_data['name']); |
|
165 } |
|
166 |
|
167 if (isset($this->_data['predecessor'])) { |
|
168 $this->_job->setJobDependency($this->_data['predecessor']); |
|
169 } |
|
170 |
|
171 if (isset($this->_data['preserved'])) { |
|
172 $this->_job->setPreserved($this->_data['preserved']); |
|
173 } |
|
174 |
|
175 if (isset($this->_data['user_variables'])) { |
|
176 $this->_job->setUserVariables($this->_data['user_variables']); |
|
177 } |
|
178 |
|
179 if (!empty($this->_data['interval'])) { |
|
180 $endTime = isset($this->_data['end_time']) ? $this->_data['end_time'] : null; |
|
181 $this->_job->setRecurrenceData($this->_data['interval'], $endTime); |
|
182 } elseif (isset($this->_data['interval']) && ($this->_data['interval'] === '')) { |
|
183 $this->_job->setRecurrenceData(0,0); |
|
184 } |
|
185 |
|
186 if (isset($this->_data['scheduled_time'])) { |
|
187 $this->_job->setScheduledTime($this->_data['scheduled_time']); |
|
188 } |
|
189 |
|
190 if (isset($this->_data['application_id'])) { |
|
191 $this->_job->setApplicationID($this->_data['application_id']); |
|
192 } |
|
193 } |
|
194 } |