Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 828 lines (736 sloc) 27.948 kB
99ab53c @CloudMarc Initial Commit
CloudMarc authored
1 <?php
ba8d418 @thousandsofthem Documentation for IronWorker.class.
thousandsofthem authored
2 /**
3 * PHP client for IronWorker
4 * IronWorker is a massively scalable background processing system.
5 *
6 * @link https://github.com/iron-io/iron_worker_php
7 * @link http://www.iron.io/
68eaa13 @thousandsofthem docs -> dev
thousandsofthem authored
8 * @link http://dev.iron.io/
aa2991e @thousandsofthem New upload() method
thousandsofthem authored
9 * @version 1.1.3
ba8d418 @thousandsofthem Documentation for IronWorker.class.
thousandsofthem authored
10 * @package IronWorkerPHP
11 * @copyright Feel free to copy, steal, take credit for, or whatever you feel like doing with this code. ;)
12 */
13
14 /**
15 * The Http_Exception class represents an HTTP response status that is not 200 OK.
16 */
99ab53c @CloudMarc Initial Commit
CloudMarc authored
17 class Http_Exception extends Exception{
18 const NOT_MODIFIED = 304;
19 const BAD_REQUEST = 400;
20 const NOT_FOUND = 404;
8a3c5b3 @thousandsofthem Typo fix. Bump version to 1.1.2
thousandsofthem authored
21 const NOT_ALLOWED = 405;
99ab53c @CloudMarc Initial Commit
CloudMarc authored
22 const CONFLICT = 409;
23 const PRECONDITION_FAILED = 412;
24 const INTERNAL_ERROR = 500;
e986ea0 @thousandsofthem Fixes #4 API call exponential backoff/retry. Bump version to 1.0.1
thousandsofthem authored
25 const SERVICE_UNAVAILABLE = 503;
99ab53c @CloudMarc Initial Commit
CloudMarc authored
26 }
27
ba8d418 @thousandsofthem Documentation for IronWorker.class.
thousandsofthem authored
28 /**
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
29 * The JSON_Exception class represents an failures of decoding json strings.
30 */
31 class JSON_Exception extends Exception {
32 public $error = null;
33 public $error_code = JSON_ERROR_NONE;
34
35 function __construct($error_code) {
36 $this->error_code = $error_code;
37 switch($error_code) {
38 case JSON_ERROR_DEPTH:
39 $this->error = 'Maximum stack depth exceeded.';
40 break;
41 case JSON_ERROR_CTRL_CHAR:
42 $this->error = "Unexpected control characted found.";
43 break;
44 case JSON_ERROR_SYNTAX:
45 $this->error = "Syntax error, malformed JSON";
46 break;
f2d0c13 @thousandsofthem php 5.2 support
thousandsofthem authored
47 default:
48 $this->error = $error_code;
49 break;
50
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
51 }
52 parent::__construct();
53 }
54
55 function __toString() {
56 return $this->error;
57 }
58 }
59
62a08c6 @thousandsofthem Added functions getArgs() and getPayload() to a worker.
thousandsofthem authored
60
61 class IronWorker_Exception extends Exception{
62
63 }
64
65
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
66 /**
ba8d418 @thousandsofthem Documentation for IronWorker.class.
thousandsofthem authored
67 * Class that wraps IronWorker API calls.
68 */
3ebd66d @thousandsofthem SimpleWorker => IronWorker. Examples moved to separate directory.
thousandsofthem authored
69 class IronWorker{
99ab53c @CloudMarc Initial Commit
CloudMarc authored
70
71 //Header Constants
3ebd66d @thousandsofthem SimpleWorker => IronWorker. Examples moved to separate directory.
thousandsofthem authored
72 const header_user_agent = "IronWorker PHP v0.1";
99ab53c @CloudMarc Initial Commit
CloudMarc authored
73 const header_accept = "application/json";
6617afd @thousandsofthem Initial release.
thousandsofthem authored
74 const header_accept_encoding = "gzip, deflate";
99ab53c @CloudMarc Initial Commit
CloudMarc authored
75 const HTTP_OK = 200;
76 const HTTP_CREATED = 201;
df338e6 @thousandsofthem Code for avoiding 500 error. Bump version to 1.1.1
thousandsofthem authored
77 const HTTP_ACCEPTED = 202;
99ab53c @CloudMarc Initial Commit
CloudMarc authored
78
79 const POST = 'POST';
80 const GET = 'GET';
81 const DELETE = 'DELETE';
82
e986ea0 @thousandsofthem Fixes #4 API call exponential backoff/retry. Bump version to 1.0.1
thousandsofthem authored
83 public $max_retries = 5;
6617afd @thousandsofthem Initial release.
thousandsofthem authored
84 public $debug_enabled = false;
85
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
86 private $required_config_fields = array('token','project_id');
3b70cee @thousandsofthem Make all params except token optional.
thousandsofthem authored
87 private $default_values = array(
88 'protocol' => 'http',
89 'host' => 'worker-aws-us-east-1.iron.io',
90 'port' => '80',
91 'api_version' => '2',
92 );
6617afd @thousandsofthem Initial release.
thousandsofthem authored
93
94 private $url;
95 private $token;
96 private $api_version;
97 private $version;
98 private $project_id;
ed67e6e @thousandsofthem New helper for worker: setProgress()
thousandsofthem authored
99 private $headers;
6617afd @thousandsofthem Initial release.
thousandsofthem authored
100
101 /**
102 * @param string|array $config_file_or_options
390db9d @thousandsofthem PDO example for client php class. PHPdoc comments.
thousandsofthem authored
103 * Array of options or name of config file.
104 * Fields in options array or in config:
ba8d418 @thousandsofthem Documentation for IronWorker.class.
thousandsofthem authored
105 *
390db9d @thousandsofthem PDO example for client php class. PHPdoc comments.
thousandsofthem authored
106 * Required:
c7e9e04 @thousandsofthem New validations, comments. Fixing tests.
thousandsofthem authored
107 * - token
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
108 * - project_id
3ebd66d @thousandsofthem SimpleWorker => IronWorker. Examples moved to separate directory.
thousandsofthem authored
109 * Optional:
c7e9e04 @thousandsofthem New validations, comments. Fixing tests.
thousandsofthem authored
110 * - protocol
111 * - host
112 * - port
113 * - api_version
6617afd @thousandsofthem Initial release.
thousandsofthem authored
114 */
115 function __construct($config_file_or_options){
116 $config = $this->getConfigData($config_file_or_options);
117 $token = $config['token'];
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
118 $project_id = $config['project_id'];
119
3b70cee @thousandsofthem Make all params except token optional.
thousandsofthem authored
120 $protocol = empty($config['protocol']) ? $this->default_values['protocol'] : $config['protocol'];
121 $host = empty($config['host']) ? $this->default_values['host'] : $config['host'];
122 $port = empty($config['port']) ? $this->default_values['port'] : $config['port'];
123 $api_version = empty($config['api_version'])? $this->default_values['api_version'] : $config['api_version'];
124
6617afd @thousandsofthem Initial release.
thousandsofthem authored
125 $this->url = "$protocol://$host:$port/$api_version/";
126 $this->token = $token;
127 $this->api_version = $api_version;
128 $this->version = $api_version;
3ebd66d @thousandsofthem SimpleWorker => IronWorker. Examples moved to separate directory.
thousandsofthem authored
129 $this->project_id = $project_id;
99ab53c @CloudMarc Initial Commit
CloudMarc authored
130 }
131
35794ee @thousandsofthem ZipDirectory feature. PostToTwitter example.
thousandsofthem authored
132 /**
aa2991e @thousandsofthem New upload() method
thousandsofthem authored
133 * Zips and uploads your code
134 *
135 * Shortcut for zipDirectory() + postCode()
136 *
137 * @param string $directory Directory with worker files
138 * @param string $run_filename This file will be launched as worker
139 * @param string $code_name Referenceable (unique) name for your worker
140 * @return bool Result of operation
141 * @throws Exception
142 */
143 public function upload($directory, $run_filename, $code_name){
144 $temp_file = tempnam(sys_get_temp_dir(), 'iron_worker_php');
145 if (!self::zipDirectory($directory, $temp_file, true)){
146 unlink($temp_file);
147 return false;
148 }
149 try{
150 $response = $this->postCode($run_filename, $temp_file, $code_name);
151 $is_ok = ($response->status_code == 200);
152 }catch(Exception $e){
153 unlink($temp_file);
154 throw $e;
155 }
156 return $is_ok;
157 }
158
159 /**
390db9d @thousandsofthem PDO example for client php class. PHPdoc comments.
thousandsofthem authored
160 * Creates a zip archieve from array of file names
ba8d418 @thousandsofthem Documentation for IronWorker.class.
thousandsofthem authored
161 *
162 * Example:
163 * <code>
164 * IronWorker::createZip(dirname(__FILE__), array('HelloWorld.php'), 'worker.zip', true);
165 * </code>
166 *
35794ee @thousandsofthem ZipDirectory feature. PostToTwitter example.
thousandsofthem authored
167 * @static
390db9d @thousandsofthem PDO example for client php class. PHPdoc comments.
thousandsofthem authored
168 * @param string $base_dir Full path to directory which contain files
169 * @param array $files File names, path (both passesed and stored) is relative to $base_dir.
170 * Examples: 'worker.php','lib/file.php'
171 * @param string $destination Zip file name.
172 * @param bool $overwrite Overwite existing file or not.
35794ee @thousandsofthem ZipDirectory feature. PostToTwitter example.
thousandsofthem authored
173 * @return bool
174 */
175 public static function createZip($base_dir, $files = array(), $destination, $overwrite = false) {
99ab53c @CloudMarc Initial Commit
CloudMarc authored
176 //if the zip file already exists and overwrite is false, return false
177 if(file_exists($destination) && !$overwrite) { return false; }
35794ee @thousandsofthem ZipDirectory feature. PostToTwitter example.
thousandsofthem authored
178 if (!empty($base_dir)) $base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
99ab53c @CloudMarc Initial Commit
CloudMarc authored
179 //vars
180 $valid_files = array();
181 //if files were passed in...
182 if(is_array($files)) {
183 //cycle through each file
184 foreach($files as $file) {
185 //make sure the file exists
35794ee @thousandsofthem ZipDirectory feature. PostToTwitter example.
thousandsofthem authored
186 if(file_exists($base_dir.$file)) {
99ab53c @CloudMarc Initial Commit
CloudMarc authored
187 $valid_files[] = $file;
188 }
189 }
190 }
191 if(count($valid_files)) {
192 $zip = new ZipArchive();
193 if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
194 return false;
195 }
196 foreach($valid_files as $file) {
35794ee @thousandsofthem ZipDirectory feature. PostToTwitter example.
thousandsofthem authored
197 $zip->addFile($base_dir.$file, $file);
99ab53c @CloudMarc Initial Commit
CloudMarc authored
198 }
199 $zip->close();
200 return file_exists($destination);
6617afd @thousandsofthem Initial release.
thousandsofthem authored
201 }else{
99ab53c @CloudMarc Initial Commit
CloudMarc authored
202 return false;
203 }
204 }
35794ee @thousandsofthem ZipDirectory feature. PostToTwitter example.
thousandsofthem authored
205
390db9d @thousandsofthem PDO example for client php class. PHPdoc comments.
thousandsofthem authored
206 /**
207 * Creates a zip archieve with all files and folders inside specific directory.
ba8d418 @thousandsofthem Documentation for IronWorker.class.
thousandsofthem authored
208 *
209 * Example:
210 * <code>
211 * IronWorker::zipDirectory(dirname(__FILE__)."/worker/", 'worker.zip', true);
212 * </code>
213 *
390db9d @thousandsofthem PDO example for client php class. PHPdoc comments.
thousandsofthem authored
214 * @static
215 * @param string $directory
216 * @param string $destination
217 * @param bool $overwrite
218 * @return bool
219 */
35794ee @thousandsofthem ZipDirectory feature. PostToTwitter example.
thousandsofthem authored
220 public static function zipDirectory($directory, $destination, $overwrite = false){
221 if (!file_exists($directory) || !is_dir($directory)) return false;
222 $directory = rtrim($directory, DIRECTORY_SEPARATOR);
223
224 $files = self::fileNamesRecursive($directory);
225
226 if (empty($files)) return false;
227
228 return self::createZip($directory, $files, $destination, $overwrite);
229 }
230
6617afd @thousandsofthem Initial release.
thousandsofthem authored
231 public function setProjectId($project_id) {
232 if (!empty($project_id)){
99ab53c @CloudMarc Initial Commit
CloudMarc authored
233 $this->project_id = $project_id;
234 }
c7e9e04 @thousandsofthem New validations, comments. Fixing tests.
thousandsofthem authored
235 if (empty($this->project_id)){
236 throw new InvalidArgumentException("Please set project_id");
237 }
99ab53c @CloudMarc Initial Commit
CloudMarc authored
238 }
6617afd @thousandsofthem Initial release.
thousandsofthem authored
239
240 public function getProjects(){
99ab53c @CloudMarc Initial Commit
CloudMarc authored
241 $this->setJsonHeaders();
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
242 $projects = self::json_decode($this->apiCall(self::GET, 'projects'));
99ab53c @CloudMarc Initial Commit
CloudMarc authored
243 return $projects->projects;
244 }
6617afd @thousandsofthem Initial release.
thousandsofthem authored
245
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
246 public function getTasks(){
6617afd @thousandsofthem Initial release.
thousandsofthem authored
247 $url = "projects/{$this->project_id}/tasks";
99ab53c @CloudMarc Initial Commit
CloudMarc authored
248 $this->setJsonHeaders();
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
249 $task = self::json_decode($this->apiCall(self::GET, $url));
6617afd @thousandsofthem Initial release.
thousandsofthem authored
250 return $task->tasks;
99ab53c @CloudMarc Initial Commit
CloudMarc authored
251 }
6617afd @thousandsofthem Initial release.
thousandsofthem authored
252
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
253 public function getProjectDetails(){
99ab53c @CloudMarc Initial Commit
CloudMarc authored
254 $this->setJsonHeaders();
6617afd @thousandsofthem Initial release.
thousandsofthem authored
255 $url = "projects/{$this->project_id}";
99ab53c @CloudMarc Initial Commit
CloudMarc authored
256 return json_decode($this->apiCall(self::GET, $url));
257 }
6617afd @thousandsofthem Initial release.
thousandsofthem authored
258
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
259 public function getCodes(){
99ab53c @CloudMarc Initial Commit
CloudMarc authored
260 $this->setJsonHeaders();
6617afd @thousandsofthem Initial release.
thousandsofthem authored
261 $url = "projects/{$this->project_id}/codes";
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
262 $codes = self::json_decode($this->apiCall(self::GET, $url));
99ab53c @CloudMarc Initial Commit
CloudMarc authored
263 return $codes->codes;
264 }
6617afd @thousandsofthem Initial release.
thousandsofthem authored
265
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
266 public function getCodeDetails($code_id){
c7e9e04 @thousandsofthem New validations, comments. Fixing tests.
thousandsofthem authored
267 if (empty($code_id)){
268 throw new InvalidArgumentException("Please set code_id");
269 }
99ab53c @CloudMarc Initial Commit
CloudMarc authored
270 $this->setJsonHeaders();
6617afd @thousandsofthem Initial release.
thousandsofthem authored
271 $url = "projects/{$this->project_id}/codes/$code_id";
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
272 return self::json_decode($this->apiCall(self::GET, $url));
99ab53c @CloudMarc Initial Commit
CloudMarc authored
273 }
274
390db9d @thousandsofthem PDO example for client php class. PHPdoc comments.
thousandsofthem authored
275 /**
276 * Uploads your code package
c25744e @thousandsofthem New api call getSchedule(). Humanized package names for examples.
thousandsofthem authored
277 *
278 * @param string $filename This file will be launched as worker
390db9d @thousandsofthem PDO example for client php class. PHPdoc comments.
thousandsofthem authored
279 * @param string $zipFilename zip file containing code to execute
280 * @param string $name referenceable (unique) name for your worker
281 * @return mixed
282 */
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
283 public function postCode($filename, $zipFilename, $name){
62a08c6 @thousandsofthem Added functions getArgs() and getPayload() to a worker.
thousandsofthem authored
284
285 // Add IronWorker functions to the uploaded worker
a2ea2ed @thousandsofthem Introduced runner.php instead of header. Bump version to 1.1.0
thousandsofthem authored
286 $this->addRunnerToArchive($zipFilename, $filename);
62a08c6 @thousandsofthem Added functions getArgs() and getPayload() to a worker.
thousandsofthem authored
287
99ab53c @CloudMarc Initial Commit
CloudMarc authored
288 $this->setPostHeaders();
289 $ts = time();
290 $runtime_type = $this->runtimeFileType($filename);
6617afd @thousandsofthem Initial release.
thousandsofthem authored
291 $sendingData = array(
a2ea2ed @thousandsofthem Introduced runner.php instead of header. Bump version to 1.1.0
thousandsofthem authored
292 "code_name" => $name,
293 "name" => $name,
6617afd @thousandsofthem Initial release.
thousandsofthem authored
294 "standalone" => True,
a2ea2ed @thousandsofthem Introduced runner.php instead of header. Bump version to 1.1.0
thousandsofthem authored
295 "runtime" => $runtime_type,
296 "file_name" => "runner.php",
297 "version" => $this->version,
298 "timestamp" => $ts,
299 "oauth" => $this->token,
6617afd @thousandsofthem Initial release.
thousandsofthem authored
300 "class_name" => $name,
a2ea2ed @thousandsofthem Introduced runner.php instead of header. Bump version to 1.1.0
thousandsofthem authored
301 "options" => array(),
c25744e @thousandsofthem New api call getSchedule(). Humanized package names for examples.
thousandsofthem authored
302 "access_key" => $name
303 );
934a69f @thousandsofthem Upload code: replaced file_get_contents with curl.
thousandsofthem authored
304 $url = "projects/{$this->project_id}/codes";
305 $post = array(
306 "data" => json_encode($sendingData),
307 "file"=>"@".$zipFilename,
308 );
309 $response = $this->apiCall(self::POST, $url, array(), $post);
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
310 return self::json_decode($response);
99ab53c @CloudMarc Initial Commit
CloudMarc authored
311 }
312
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
313 public function deleteCode($code_id){
6617afd @thousandsofthem Initial release.
thousandsofthem authored
314 $url = "projects/{$this->project_id}/codes/$code_id";
99ab53c @CloudMarc Initial Commit
CloudMarc authored
315 return $this->apiCall(self::DELETE, $url);
316 }
317
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
318 public function deleteSchedule($schedule_id){
eb336f4 @thousandsofthem API updated.
thousandsofthem authored
319 $url = "projects/{$this->project_id}/schedules/$schedule_id/cancel";
2c02c0d @thousandsofthem Several new API calls.
thousandsofthem authored
320
321 $request = array(
322 'schedule_id' => $schedule_id
323 );
324
eb336f4 @thousandsofthem API updated.
thousandsofthem authored
325 return $this->apiCall(self::POST, $url, $request);
99ab53c @CloudMarc Initial Commit
CloudMarc authored
326 }
327
c25744e @thousandsofthem New api call getSchedule(). Humanized package names for examples.
thousandsofthem authored
328 /**
329 * Get information about all schedules for project
330 *
331 * @return mixed
332 */
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
333 public function getSchedules(){
99ab53c @CloudMarc Initial Commit
CloudMarc authored
334 $this->setJsonHeaders();
6617afd @thousandsofthem Initial release.
thousandsofthem authored
335 $url = "projects/{$this->project_id}/schedules";
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
336 $schedules = self::json_decode($this->apiCall(self::GET, $url));
99ab53c @CloudMarc Initial Commit
CloudMarc authored
337 return $schedules->schedules;
338 }
6617afd @thousandsofthem Initial release.
thousandsofthem authored
339
340 /**
c25744e @thousandsofthem New api call getSchedule(). Humanized package names for examples.
thousandsofthem authored
341 * Get information about schedule
342 *
343 * @param string $schedule_id Schedule ID
344 * @return mixed
345 * @throws InvalidArgumentException
346 */
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
347 public function getSchedule($schedule_id){
c25744e @thousandsofthem New api call getSchedule(). Humanized package names for examples.
thousandsofthem authored
348 if (empty($schedule_id)){
349 throw new InvalidArgumentException("Please set schedule_id");
350 }
351 $this->setJsonHeaders();
352 $url = "projects/{$this->project_id}/schedules/$schedule_id";
353
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
354 return self::json_decode($this->apiCall(self::GET, $url));
c25744e @thousandsofthem New api call getSchedule(). Humanized package names for examples.
thousandsofthem authored
355 }
356
357 /**
ba8d418 @thousandsofthem Documentation for IronWorker.class.
thousandsofthem authored
358 * Schedules task
c25744e @thousandsofthem New api call getSchedule(). Humanized package names for examples.
thousandsofthem authored
359 *
360 * @param string $name Package name
361 * @param array $payload Payload for task
ba8d418 @thousandsofthem Documentation for IronWorker.class.
thousandsofthem authored
362 * @param int $delay Delay in seconds
363 * @return string Created Schedule id
6617afd @thousandsofthem Initial release.
thousandsofthem authored
364 */
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
365 public function postScheduleSimple($name, $payload = array(), $delay = 1){
366 return $this->postSchedule($name, array('delay' => $delay), $payload);
adf5ef7 @thousandsofthem Support more scheduling variants.
thousandsofthem authored
367 }
6617afd @thousandsofthem Initial release.
thousandsofthem authored
368
adf5ef7 @thousandsofthem Support more scheduling variants.
thousandsofthem authored
369 /**
c25744e @thousandsofthem New api call getSchedule(). Humanized package names for examples.
thousandsofthem authored
370 * Schedules task
371 *
372 * @param string $name Package name
373 * @param array $payload Payload for task
7ac14f1 @thousandsofthem Allow pass $payload to schedule. Time usage simplified for schedule*.
thousandsofthem authored
374 * @param int $start_at Time of first run in unix timestamp format. Example: time()+2*60
adf5ef7 @thousandsofthem Support more scheduling variants.
thousandsofthem authored
375 * @param int $run_every Time in seconds between runs. If omitted, task will only run once.
7ac14f1 @thousandsofthem Allow pass $payload to schedule. Time usage simplified for schedule*.
thousandsofthem authored
376 * @param int $end_at Time tasks will stop being enqueued in unix timestamp format.
adf5ef7 @thousandsofthem Support more scheduling variants.
thousandsofthem authored
377 * @param int $run_times Number of times to run task.
378 * @param int $priority Priority queue to run the job in (0, 1, 2). p0 is default.
ba8d418 @thousandsofthem Documentation for IronWorker.class.
thousandsofthem authored
379 * @return string Created Schedule id
adf5ef7 @thousandsofthem Support more scheduling variants.
thousandsofthem authored
380 */
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
381 public function postScheduleAdvanced($name, $payload = array(), $start_at, $run_every = null, $end_at = null, $run_times = null, $priority = null){
adf5ef7 @thousandsofthem Support more scheduling variants.
thousandsofthem authored
382 $options = array();
7ac14f1 @thousandsofthem Allow pass $payload to schedule. Time usage simplified for schedule*.
thousandsofthem authored
383 $options['start_at'] = self::dateRfc3339($start_at);
adf5ef7 @thousandsofthem Support more scheduling variants.
thousandsofthem authored
384 if (!empty($run_every)) $options['run_every'] = $run_every;
7ac14f1 @thousandsofthem Allow pass $payload to schedule. Time usage simplified for schedule*.
thousandsofthem authored
385 if (!empty($end_at)) $options['end_at'] = self::dateRfc3339($end_at);
adf5ef7 @thousandsofthem Support more scheduling variants.
thousandsofthem authored
386 if (!empty($run_times)) $options['run_times'] = $run_times;
387 if (!empty($priority)) $options['priority'] = $priority;
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
388 return $this->postSchedule($name, $options, $payload);
99ab53c @CloudMarc Initial Commit
CloudMarc authored
389 }
6617afd @thousandsofthem Initial release.
thousandsofthem authored
390
ba8d418 @thousandsofthem Documentation for IronWorker.class.
thousandsofthem authored
391 /**
392 * Queues already uploaded worker
393 *
394 * @param string $name Package name
ff09f6f @thousandsofthem Added new options "priority", "timeout","delay" for postTask()
thousandsofthem authored
395 * @param array $payload Payload for task
396 * @param array $options Optional parameters:
397 * - "priority" priority queue to run the job in (0, 1, 2). 0 is default.
398 * - "timeout" maximum runtime of your task in seconds. Maximum time is 3600 seconds (60 minutes). Default is 3600 seconds (60 minutes).
399 * - "delay" delay before actually queueing the task in seconds. Default is 0 seconds.
ba8d418 @thousandsofthem Documentation for IronWorker.class.
thousandsofthem authored
400 * @return string Created Task ID
401 */
ff09f6f @thousandsofthem Added new options "priority", "timeout","delay" for postTask()
thousandsofthem authored
402 public function postTask($name, $payload = array(), $options = array()){
6617afd @thousandsofthem Initial release.
thousandsofthem authored
403 $url = "projects/{$this->project_id}/tasks";
404
405 $request = array(
406 'tasks' => array(
407 array(
408 "name" => $name,
409 "code_name" => $name,
67eaa54 @thousandsofthem Zip packer fix. Directory referencing fix. Payload for worker.
thousandsofthem authored
410 'payload' => json_encode($payload),
6617afd @thousandsofthem Initial release.
thousandsofthem authored
411 )
412 )
413 );
414
ff09f6f @thousandsofthem Added new options "priority", "timeout","delay" for postTask()
thousandsofthem authored
415 foreach ($options as $k => $v){
416 $request['tasks'][0][$k] = $v;
417 }
418
6617afd @thousandsofthem Initial release.
thousandsofthem authored
419 $this->setCommonHeaders();
420 $res = $this->apiCall(self::POST, $url, $request);
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
421 $tasks = self::json_decode($res);
fd32265 @thousandsofthem Code cleanup, almost all methods work as expected.
thousandsofthem authored
422 return $tasks->tasks[0]->id;
99ab53c @CloudMarc Initial Commit
CloudMarc authored
423 }
6617afd @thousandsofthem Initial release.
thousandsofthem authored
424
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
425 public function getLog($task_id){
c7e9e04 @thousandsofthem New validations, comments. Fixing tests.
thousandsofthem authored
426 if (empty($task_id)){
427 throw new InvalidArgumentException("Please set task_id");
428 }
99ab53c @CloudMarc Initial Commit
CloudMarc authored
429 $this->setJsonHeaders();
cbd59af @thousandsofthem Fixing typos.
thousandsofthem authored
430 $url = "projects/{$this->project_id}/tasks/$task_id/log";
99ab53c @CloudMarc Initial Commit
CloudMarc authored
431 $this->headers['Accept'] = "text/plain";
432 unset($this->headers['Content-Type']);
6617afd @thousandsofthem Initial release.
thousandsofthem authored
433 return $this->apiCall(self::GET, $url);
8192308 @thousandsofthem Checking status for task.
thousandsofthem authored
434 }
435
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
436 public function getTaskDetails($task_id){
8192308 @thousandsofthem Checking status for task.
thousandsofthem authored
437 if (empty($task_id)){
438 throw new InvalidArgumentException("Please set task_id");
439 }
440 $this->setJsonHeaders();
441 $url = "projects/{$this->project_id}/tasks/$task_id";
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
442 return self::json_decode($this->apiCall(self::GET, $url));
99ab53c @CloudMarc Initial Commit
CloudMarc authored
443 }
444
0e65e49 @thousandsofthem Old api calls removed.
thousandsofthem authored
445 /**
446 * Cancels task.
447 *
448 * @param string $task_id Task ID
449 * @return mixed
450 * @throws InvalidArgumentException
451 */
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
452 public function cancelTask($task_id){
4813b55 @thousandsofthem Minor improvements.
thousandsofthem authored
453 if (empty($task_id)){
454 throw new InvalidArgumentException("Please set task_id");
455 }
cbd59af @thousandsofthem Fixing typos.
thousandsofthem authored
456 $url = "projects/{$this->project_id}/tasks/$task_id/cancel";
2c02c0d @thousandsofthem Several new API calls.
thousandsofthem authored
457 $request = array();
458
459 $this->setCommonHeaders();
460 $res = $this->apiCall(self::POST, $url, $request);
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
461 return self::json_decode($res);
2c02c0d @thousandsofthem Several new API calls.
thousandsofthem authored
462 }
463
0e65e49 @thousandsofthem Old api calls removed.
thousandsofthem authored
464 /**
465 * Cancels task.
466 *
467 * @param string $task_id Task ID
468 * @return mixed
469 * @throws InvalidArgumentException
470 */
471 public function deleteTask($task_id){
472 return $this->cancelTask($task_id);
473 }
474
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
475 public function setTaskProgress($task_id, $percent, $msg = ''){
4813b55 @thousandsofthem Minor improvements.
thousandsofthem authored
476 if (empty($task_id)){
477 throw new InvalidArgumentException("Please set task_id");
478 }
cbd59af @thousandsofthem Fixing typos.
thousandsofthem authored
479 $url = "projects/{$this->project_id}/tasks/$task_id/progress";
2c02c0d @thousandsofthem Several new API calls.
thousandsofthem authored
480 $request = array(
481 'percent' => $percent,
482 'msg' => $msg
483 );
484
485 $this->setCommonHeaders();
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
486 return self::json_decode($this->apiCall(self::POST, $url, $request));
2c02c0d @thousandsofthem Several new API calls.
thousandsofthem authored
487 }
488
6617afd @thousandsofthem Initial release.
thousandsofthem authored
489 /* PRIVATE FUNCTIONS */
490
adf5ef7 @thousandsofthem Support more scheduling variants.
thousandsofthem authored
491 /**
492 *
493 * @param string $name
67eaa54 @thousandsofthem Zip packer fix. Directory referencing fix. Payload for worker.
thousandsofthem authored
494 * @param array $options options contain:
495 * start_at OR delay — required - start_at is time of first run. Delay is number of seconds to wait before starting.
496 * run_every — optional - Time in seconds between runs. If omitted, task will only run once.
497 * end_at — optional - Time tasks will stop being enqueued. (Should be a Time or DateTime object.)
498 * run_times — optional - Number of times to run task. For example, if run_times: is 5, the task will run 5 times.
499 * priority — optional - Priority queue to run the job in (0, 1, 2). p0 is default. Run at higher priorities to reduce time jobs may spend in the queue once they come off schedule. Same as priority when queuing up a task.
500 * @param array $payload
adf5ef7 @thousandsofthem Support more scheduling variants.
thousandsofthem authored
501 * @return mixed
502 */
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
503 private function postSchedule($name, $options, $payload = array()){
adf5ef7 @thousandsofthem Support more scheduling variants.
thousandsofthem authored
504 $url = "projects/{$this->project_id}/schedules";
505 $shedule = array(
506 'name' => $name,
507 'code_name' => $name,
67eaa54 @thousandsofthem Zip packer fix. Directory referencing fix. Payload for worker.
thousandsofthem authored
508 'payload' => json_encode($payload),
adf5ef7 @thousandsofthem Support more scheduling variants.
thousandsofthem authored
509 );
510 $request = array(
511 'schedules' => array(
512 array_merge($shedule, $options)
513 )
514 );
515
516 $this->setCommonHeaders();
517 $res = $this->apiCall(self::POST, $url, $request);
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
518 $shedules = self::json_decode($res);
adf5ef7 @thousandsofthem Support more scheduling variants.
thousandsofthem authored
519 return $shedules->schedules[0]->id;
520 }
521
6617afd @thousandsofthem Initial release.
thousandsofthem authored
522 private function compiledHeaders(){
fd32265 @thousandsofthem Code cleanup, almost all methods work as expected.
thousandsofthem authored
523
524 # Set default headers if no headers set.
525 if ($this->headers == null){
526 $this->setCommonHeaders();
527 }
528
6617afd @thousandsofthem Initial release.
thousandsofthem authored
529 $headers = array();
530 foreach ($this->headers as $k => $v){
531 $headers[] = "$k: $v";
532 }
533 return $headers;
534 }
535
536 private function runtimeFileType($name) {
537 if(empty($name)){
538 return false;
539 }
540 $explodedName= explode(".", $name);
541 switch ($explodedName[(count($explodedName)-1)]) {
542 case "php":
543 return "php";
544 case "rb":
545 return "ruby";
546 case "py":
547 return "python";
548 case "javascript":
549 return "javascript";
550 default:
551 return "no_type_found";
552 }
553 }
554
934a69f @thousandsofthem Upload code: replaced file_get_contents with curl.
thousandsofthem authored
555 private function apiCall($type, $url, $params = array(), $raw_post_data = null){
6617afd @thousandsofthem Initial release.
thousandsofthem authored
556 $url = "{$this->url}$url";
557
558 $s = curl_init();
f93d117 @CloudMarc Working thru post code, run task, and get log
CloudMarc authored
559 if (! isset($params['oauth'])) {
560 $params['oauth'] = $this->token;
561 }
6617afd @thousandsofthem Initial release.
thousandsofthem authored
562 switch ($type) {
563 case self::DELETE:
4813b55 @thousandsofthem Minor improvements.
thousandsofthem authored
564 $fullUrl = $url . '?' . http_build_query($params);
565 $this->debug('apiCall fullUrl', $fullUrl);
566 curl_setopt($s, CURLOPT_URL, $fullUrl);
6617afd @thousandsofthem Initial release.
thousandsofthem authored
567 curl_setopt($s, CURLOPT_CUSTOMREQUEST, self::DELETE);
568 break;
569 case self::POST:
4813b55 @thousandsofthem Minor improvements.
thousandsofthem authored
570 $this->debug('apiCall url', $url);
6617afd @thousandsofthem Initial release.
thousandsofthem authored
571 curl_setopt($s, CURLOPT_URL, $url);
572 curl_setopt($s, CURLOPT_POST, true);
934a69f @thousandsofthem Upload code: replaced file_get_contents with curl.
thousandsofthem authored
573 if ($raw_post_data){
574 curl_setopt($s, CURLOPT_POSTFIELDS, $raw_post_data);
575 }else{
576 curl_setopt($s, CURLOPT_POSTFIELDS, json_encode($params));
577 }
6617afd @thousandsofthem Initial release.
thousandsofthem authored
578 break;
579 case self::GET:
f93d117 @CloudMarc Working thru post code, run task, and get log
CloudMarc authored
580 $fullUrl = $url . '?' . http_build_query($params);
581 $this->debug('apiCall fullUrl', $fullUrl);
582 curl_setopt($s, CURLOPT_URL, $fullUrl);
6617afd @thousandsofthem Initial release.
thousandsofthem authored
583 break;
584 }
585
586 curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
587 curl_setopt($s, CURLOPT_HTTPHEADER, $this->compiledHeaders());
e986ea0 @thousandsofthem Fixes #4 API call exponential backoff/retry. Bump version to 1.0.1
thousandsofthem authored
588 return $this->callWithRetries($s);
589 }
590
591 private function callWithRetries($s){
592 for ($retry = 0; $retry < $this->max_retries; $retry++){
593 $_out = curl_exec($s);
594 $status = curl_getinfo($s, CURLINFO_HTTP_CODE);
595 switch ($status) {
596 case self::HTTP_OK:
597 case self::HTTP_CREATED:
df338e6 @thousandsofthem Code for avoiding 500 error. Bump version to 1.1.1
thousandsofthem authored
598 case self::HTTP_ACCEPTED:
e986ea0 @thousandsofthem Fixes #4 API call exponential backoff/retry. Bump version to 1.0.1
thousandsofthem authored
599 curl_close($s);
600 return $_out;
df338e6 @thousandsofthem Code for avoiding 500 error. Bump version to 1.1.1
thousandsofthem authored
601 case Http_Exception::INTERNAL_ERROR:
602 if (strpos($_out, "EOF") !== false){
603 self::waitRandomInterval($retry);
604 }else{
605 curl_close($s);
606 $this->reportHttpError($status, $_out);
607 }
608 break;
e986ea0 @thousandsofthem Fixes #4 API call exponential backoff/retry. Bump version to 1.0.1
thousandsofthem authored
609 case Http_Exception::SERVICE_UNAVAILABLE:
df338e6 @thousandsofthem Code for avoiding 500 error. Bump version to 1.1.1
thousandsofthem authored
610 self::waitRandomInterval($retry);
e986ea0 @thousandsofthem Fixes #4 API call exponential backoff/retry. Bump version to 1.0.1
thousandsofthem authored
611 break;
612 default:
df338e6 @thousandsofthem Code for avoiding 500 error. Bump version to 1.1.1
thousandsofthem authored
613 curl_close($s);
614 $this->reportHttpError($status, $_out);
e986ea0 @thousandsofthem Fixes #4 API call exponential backoff/retry. Bump version to 1.0.1
thousandsofthem authored
615 }
6617afd @thousandsofthem Initial release.
thousandsofthem authored
616 }
df338e6 @thousandsofthem Code for avoiding 500 error. Bump version to 1.1.1
thousandsofthem authored
617 curl_close($s);
618 return $this->reportHttpError(503, "Service unavailable");
619 }
620
621 private function reportHttpError($status, $text){
622 throw new Http_Exception("http error: {$status} | {$text}", $status);
623 }
624
625
626 /**
627 * Wait for a random delay between 0 and (4^currentRetry * 100) milliseconds
628 *
629 * @static
630 * @param int $retry currentRetry
631 */
632 private static function waitRandomInterval($retry){
633 $max_delay = pow(4, $retry)*100*1000;
634 usleep(rand(0, $max_delay));
6617afd @thousandsofthem Initial release.
thousandsofthem authored
635 }
636
637
c7e9e04 @thousandsofthem New validations, comments. Fixing tests.
thousandsofthem authored
638 /**
639 * @param array|string $config_file_or_options
640 * array of options or name of config file
641 * @return array
642 * @throws InvalidArgumentException
643 */
6617afd @thousandsofthem Initial release.
thousandsofthem authored
644 private function getConfigData($config_file_or_options){
645 if (is_string($config_file_or_options)){
646 $ini = parse_ini_file($config_file_or_options, true);
647 if ($ini === false){
648 throw new InvalidArgumentException("Config file $config_file_or_options not found");
649 }
3ebd66d @thousandsofthem SimpleWorker => IronWorker. Examples moved to separate directory.
thousandsofthem authored
650 if (empty($ini['iron_worker'])){
651 throw new InvalidArgumentException("Config file $config_file_or_options has no section 'iron_worker'");
6617afd @thousandsofthem Initial release.
thousandsofthem authored
652 }
3ebd66d @thousandsofthem SimpleWorker => IronWorker. Examples moved to separate directory.
thousandsofthem authored
653 $config = $ini['iron_worker'];
6617afd @thousandsofthem Initial release.
thousandsofthem authored
654 }elseif(is_array($config_file_or_options)){
655 $config = $config_file_or_options;
656 }else{
657 throw new InvalidArgumentException("Wrong parameter type");
658 }
659 foreach ($this->required_config_fields as $field){
660 if (empty($config[$field])){
661 throw new InvalidArgumentException("Required config key missing: '$field'");
662 }
663 }
664 return $config;
665 }
666
667 private function getFileContent($filename){
4813b55 @thousandsofthem Minor improvements.
thousandsofthem authored
668 return file_get_contents($filename);
6617afd @thousandsofthem Initial release.
thousandsofthem authored
669 }
670
671 private function setCommonHeaders(){
672 $this->headers = array(
673 'Authorization' => "OAuth {$this->token}",
674 'User-Agent' => self::header_user_agent,
675 'Content-Type' => 'application/json',
676 'Accept' => self::header_accept,
677 'Accept-Encoding' => self::header_accept_encoding
678 );
679 }
680
681 private function setJsonHeaders(){
682 $this->setCommonHeaders();
683 }
684
685 private function setPostHeaders(){
686 $this->setCommonHeaders();
687 $this->headers['Content-Type'] ='multipart/form-data';
688 }
689
690 private function debug($var_name, $variable){
691 if ($this->debug_enabled){
692 echo "{$var_name}: ".var_export($variable,true)."\n";
693 }
694 }
99ab53c @CloudMarc Initial Commit
CloudMarc authored
695
35794ee @thousandsofthem ZipDirectory feature. PostToTwitter example.
thousandsofthem authored
696 private static function fileNamesRecursive($dir, $base_dir = ''){
697 $dir .= DIRECTORY_SEPARATOR;
698 $files = scandir($dir);
699 $names = array();
700
701 foreach($files as $name){
702 if ($name == '.' || $name == '..' || $name == '.svn') continue;
703 if (is_dir($dir.$name)){
67eaa54 @thousandsofthem Zip packer fix. Directory referencing fix. Payload for worker.
thousandsofthem authored
704 $inner_names = self::fileNamesRecursive($dir.$name, $base_dir.$name.'/');
35794ee @thousandsofthem ZipDirectory feature. PostToTwitter example.
thousandsofthem authored
705 foreach ($inner_names as $iname){
706 $names[] = $iname;
707 }
708 }else{
709 $names[] = $base_dir.$name;
710 }
711 }
712 return $names;
713 }
ba8d418 @thousandsofthem Documentation for IronWorker.class.
thousandsofthem authored
714
715 private static function dateRfc3339($timestamp = 0) {
716
717 if (!$timestamp) {
718 $timestamp = time();
719 }
720 $date = date('Y-m-d\TH:i:s', $timestamp);
721
722 $matches = array();
723 if (preg_match('/^([\-+])(\d{2})(\d{2})$/', date('O', $timestamp), $matches)) {
724 $date .= $matches[1].$matches[2].':'.$matches[3];
725 } else {
726 $date .= 'Z';
727 }
728 return $date;
729 }
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
730
731 private static function json_decode($response){
732 $data = json_decode($response);
f2d0c13 @thousandsofthem php 5.2 support
thousandsofthem authored
733 if (function_exists('json_last_error')){
734 $json_error = json_last_error();
735 if($json_error != JSON_ERROR_NONE) {
736 throw new JSON_Exception($json_error);
737 }
738 }elseif($data === null){
739 throw new JSON_Exception("Common JSON error");
55c09d8 @thousandsofthem Revamp using $project_id. Json exception handling.
thousandsofthem authored
740 }
741 return $data;
742 }
62a08c6 @thousandsofthem Added functions getArgs() and getPayload() to a worker.
thousandsofthem authored
743
744
745 /**
746 * Contain php code that adds to worker before upload
747 *
a2ea2ed @thousandsofthem Introduced runner.php instead of header. Bump version to 1.1.0
thousandsofthem authored
748 * @param string $worker_file_name
62a08c6 @thousandsofthem Added functions getArgs() and getPayload() to a worker.
thousandsofthem authored
749 * @return string
750 */
a2ea2ed @thousandsofthem Introduced runner.php instead of header. Bump version to 1.1.0
thousandsofthem authored
751 private function workerHeader($worker_file_name){
30867ae @iced support for <php-5.3
iced authored
752 $header = <<<EOL
62a08c6 @thousandsofthem Added functions getArgs() and getPayload() to a worker.
thousandsofthem authored
753 <?php
754 /*IRON_WORKER_HEADER*/
755 function getArgs(){
30867ae @iced support for <php-5.3
iced authored
756 global \$argv;
757 \$args = array('task_id' => null, 'dir' => null, 'payload' => array());
758 foreach(\$argv as \$k => \$v){
759 if (empty(\$argv[\$k+1])) continue;
f2d0c13 @thousandsofthem php 5.2 support
thousandsofthem authored
760 if (\$v == '-id') \$args['task_id'] = \$argv[\$k+1];
761 if (\$v == '-d') \$args['dir'] = \$argv[\$k+1];
30867ae @iced support for <php-5.3
iced authored
762 if (\$v == '-payload' && file_exists(\$argv[\$k+1])){
763 \$args['payload'] = json_decode(file_get_contents(\$argv[\$k+1]));
62a08c6 @thousandsofthem Added functions getArgs() and getPayload() to a worker.
thousandsofthem authored
764 }
765 }
30867ae @iced support for <php-5.3
iced authored
766 return \$args;
62a08c6 @thousandsofthem Added functions getArgs() and getPayload() to a worker.
thousandsofthem authored
767 }
768
769 function getPayload(){
30867ae @iced support for <php-5.3
iced authored
770 \$args = getArgs();
771 return \$args['payload'];
ed67e6e @thousandsofthem New helper for worker: setProgress()
thousandsofthem authored
772 }
773
30867ae @iced support for <php-5.3
iced authored
774 function setProgress(\$percent, \$msg = ''){
775 \$args = getArgs();
776 \$task_id = \$args['task_id'];
777 \$base_url = '[URL]';
778 \$project_id = '[PROJECT_ID]';
779 \$headers = [HEADERS];
780
781 \$url = "{\$base_url}projects/\$project_id/tasks/\$task_id/progress";
782 \$params = array(
783 'percent' => \$percent,
784 'msg' => \$msg
ed67e6e @thousandsofthem New helper for worker: setProgress()
thousandsofthem authored
785 );
786
30867ae @iced support for <php-5.3
iced authored
787 \$s = curl_init();
788 curl_setopt(\$s, CURLOPT_URL, \$url);
789 curl_setopt(\$s, CURLOPT_POST, true);
790 curl_setopt(\$s, CURLOPT_POSTFIELDS, json_encode(\$params));
791 curl_setopt(\$s, CURLOPT_RETURNTRANSFER, true);
792 curl_setopt(\$s, CURLOPT_HTTPHEADER, \$headers);
793 \$out = curl_exec(\$s);
794 curl_close(\$s);
795 return json_decode(\$out);
62a08c6 @thousandsofthem Added functions getArgs() and getPayload() to a worker.
thousandsofthem authored
796 }
a2ea2ed @thousandsofthem Introduced runner.php instead of header. Bump version to 1.1.0
thousandsofthem authored
797 require dirname(__FILE__)."/[SCRIPT]";
62a08c6 @thousandsofthem Added functions getArgs() and getPayload() to a worker.
thousandsofthem authored
798 EOL;
ed67e6e @thousandsofthem New helper for worker: setProgress()
thousandsofthem authored
799 $header = str_replace(
a2ea2ed @thousandsofthem Introduced runner.php instead of header. Bump version to 1.1.0
thousandsofthem authored
800 array('[PROJECT_ID]','[URL]','[HEADERS]','[SCRIPT]'),
801 array($this->project_id, $this->url, var_export($this->compiledHeaders(), true), $worker_file_name),
ed67e6e @thousandsofthem New helper for worker: setProgress()
thousandsofthem authored
802 $header
803 );
62a08c6 @thousandsofthem Added functions getArgs() and getPayload() to a worker.
thousandsofthem authored
804 return trim($header," \n\r");
805 }
806
a2ea2ed @thousandsofthem Introduced runner.php instead of header. Bump version to 1.1.0
thousandsofthem authored
807 private function addRunnerToArchive($archive, $worker_file_name){
62a08c6 @thousandsofthem Added functions getArgs() and getPayload() to a worker.
thousandsofthem authored
808 $zip = new ZipArchive;
178e189 @iced support for php on windows - another try
iced authored
809 if (!$zip->open($archive, ZIPARCHIVE::CREATE) === true) {
62a08c6 @thousandsofthem Added functions getArgs() and getPayload() to a worker.
thousandsofthem authored
810 $zip->close();
a2ea2ed @thousandsofthem Introduced runner.php instead of header. Bump version to 1.1.0
thousandsofthem authored
811 throw new IronWorker_Exception("Archive $archive was not found!");
62a08c6 @thousandsofthem Added functions getArgs() and getPayload() to a worker.
thousandsofthem authored
812 }
813
a2ea2ed @thousandsofthem Introduced runner.php instead of header. Bump version to 1.1.0
thousandsofthem authored
814 if ($zip->statName($worker_file_name) === false){
62a08c6 @thousandsofthem Added functions getArgs() and getPayload() to a worker.
thousandsofthem authored
815 $zip->close();
a2ea2ed @thousandsofthem Introduced runner.php instead of header. Bump version to 1.1.0
thousandsofthem authored
816 throw new IronWorker_Exception("File $worker_file_name in archive $archive was not found!");
62a08c6 @thousandsofthem Added functions getArgs() and getPayload() to a worker.
thousandsofthem authored
817 }
818
a2ea2ed @thousandsofthem Introduced runner.php instead of header. Bump version to 1.1.0
thousandsofthem authored
819 if (!$zip->addFromString('runner.php', $this->workerHeader($worker_file_name))){
820 throw new IronWorker_Exception("Adding Runner to the worker failed");
62a08c6 @thousandsofthem Added functions getArgs() and getPayload() to a worker.
thousandsofthem authored
821 }
822
823 $zip->close();
824 return true;
825 }
826
99ab53c @CloudMarc Initial Commit
CloudMarc authored
827 }
Something went wrong with that request. Please try again.