diff --git a/SimpleWorker.class.php b/SimpleWorker.class.php index 47077bf..036e76e 100644 --- a/SimpleWorker.class.php +++ b/SimpleWorker.class.php @@ -68,7 +68,7 @@ function __construct($config_file_or_options){ /** * @static * @param string $base_dir full path to directory which contain files - * @param array $files file names should refer to $base_dir, + * @param array $files file names, should refer to $base_dir, * examples: 'worker.php','lib/file.php' * @param string $destination zip file name * @param bool $overwrite @@ -181,7 +181,7 @@ public function getCodeDetails($code_id, $project_id = ''){ return json_decode($this->apiCall(self::GET, $url)); } - public function postCode($project_id, $filename, $zipFilename,$name){ + public function postCode($project_id, $filename, $zipFilename, $name){ $this->setProjectId($project_id); $this->setPostHeaders(); $this->headers['Content-Length'] = filesize($zipFilename); @@ -199,10 +199,9 @@ public function postCode($project_id, $filename, $zipFilename,$name){ "class_name" => $name, "options" => array(), "access_key" => $name); - //$sendingData = array(); - //$sendingData[] = json_encode($sendingData); + $sendingData = json_encode($sendingData); - //print_r($sendingData);exit; + // For reference to multi-part encoding in php, see: // http://vedovini.net/2009/08/posting-multipart-form-data-using-php/ $eol = "\r\n"; @@ -210,7 +209,6 @@ public function postCode($project_id, $filename, $zipFilename,$name){ $mime_boundary = md5(time()); $data .= '--' . $mime_boundary . $eol; $data .= 'Content-Disposition: form-data; name="data"' . $eol . $eol; - //$data .= "Some Data" . $eol; $data .= $sendingData . $eol; $data .= '--' . $mime_boundary . $eol; $data .= 'Content-Disposition: form-data; name="file"; filename=$zipFilename' . $eol; @@ -226,7 +224,6 @@ public function postCode($project_id, $filename, $zipFilename,$name){ 'content' => $data )); $ctx = stream_context_create($params); - //$response = @file_get_contents($destination, FILE_TEXT, $ctx); $destination = "{$this->url}projects/{$this->project_id}/codes?oauth={$this->token}"; $this->debug('destination', $destination); @@ -362,6 +359,9 @@ public function getTaskDetails($project_id, $task_id){ public function cancelTask($project_id, $task_id){ $this->setProjectId($project_id); + if (empty($task_id)){ + throw new InvalidArgumentException("Please set task_id"); + } $url = "projects/{$this->project_id}/tasks/$task_id/cancel"; $request = array(); @@ -373,6 +373,9 @@ public function cancelTask($project_id, $task_id){ public function setTaskProgress($project_id, $task_id, $percent, $msg = ''){ $this->setProjectId($project_id); + if (empty($task_id)){ + throw new InvalidArgumentException("Please set task_id"); + } $url = "projects/{$this->project_id}/tasks/$task_id/progress"; $request = array( 'percent' => $percent, @@ -460,7 +463,6 @@ private function runtimeFileType($name) { private function apiCall($type, $url, $params = array()){ $url = "{$this->url}$url"; - $this->debug('apiCall url', $url); $s = curl_init(); if (! isset($params['oauth'])) { @@ -468,10 +470,13 @@ private function apiCall($type, $url, $params = array()){ } switch ($type) { case self::DELETE: - curl_setopt($s, CURLOPT_URL, $url . '?' . http_build_query($params)); + $fullUrl = $url . '?' . http_build_query($params); + $this->debug('apiCall fullUrl', $fullUrl); + curl_setopt($s, CURLOPT_URL, $fullUrl); curl_setopt($s, CURLOPT_CUSTOMREQUEST, self::DELETE); break; case self::POST: + $this->debug('apiCall url', $url); curl_setopt($s, CURLOPT_URL, $url); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, json_encode($params)); @@ -479,7 +484,6 @@ private function apiCall($type, $url, $params = array()){ case self::GET: $fullUrl = $url . '?' . http_build_query($params); $this->debug('apiCall fullUrl', $fullUrl); - curl_setopt($s, CURLOPT_URL, $fullUrl); break; } @@ -532,10 +536,7 @@ private function getConfigData($config_file_or_options){ } private function getFileContent($filename){ - $this->debug("filename", $filename); - $fn = getcwd() . DIRECTORY_SEPARATOR . $filename; - $this->debug("filename full", $fn); - return file_get_contents($fn); + return file_get_contents($filename); } private function setCommonHeaders(){ diff --git a/testBasic.php b/testBasic.php index e9d8887..5b4152a 100644 --- a/testBasic.php +++ b/testBasic.php @@ -10,6 +10,7 @@ $zipName = "code/$name.zip"; $files_to_zip = array('testTask.php'); $zipFile = SimpleWorker::createZip(dirname(__FILE__)."/worker_examples/hello_world", $files_to_zip, $zipName, true); +if (!$zipFile) die("Zip file $zipName was not created!"); $res = $sw->postCode($project_id, 'testTask.php', $zipName, $name); $payload = array( @@ -29,7 +30,7 @@ sleep(10); $details = $sw->getTaskDetails($project_id, $task_id); print_r($details); - +# Check log only if task finished. if ($details->status != 'queued'){ $log = $sw->getLog($project_id, $task_id); print_r($log); diff --git a/testFull.php b/testFull.php index b5a5442..732134b 100644 --- a/testFull.php +++ b/testFull.php @@ -7,7 +7,7 @@ function tolog($name, $variable, $display = false){ if ($display){echo "{$name}: ".var_export($variable,true)."\n";} } -$name = "testBasic.php-helloPHP-".microtime(true); +$name = "testFull.php-".microtime(true); $project_id = ''; # using default project_id from config @@ -31,9 +31,9 @@ function tolog($name, $variable, $display = false){ /* echo "\n--Deleting Project-------------------------------------\n"; -# TODO: {"msg":"Method DELETE not allowed","status_code":405} +# TODO: {"msg":"Method not allowed","status_code":405} $res = $sw->deleteProject($project_id); -tolog('delete_project', $res); +tolog('delete_project', $res, true); */ # =========================== Codes ============================= @@ -72,15 +72,11 @@ function tolog($name, $variable, $display = false){ echo "\n--Get Task Log----------------------------------------\n"; sleep(15); -$log = $sw->getLog($project_id, $task_id); -tolog('task_log', $log, true); - -/* -echo "\n--Deleting Task---------------------------------------\n"; -# TODO: -$res = $sw->deleteTask($project_id, $task_id); -tolog('delete_task', $res); -*/ +# Check log only if task finished. +if ($details->status != 'queued'){ + $log = $sw->getLog($project_id, $task_id); + tolog('task_log', $log, true); +} echo "\n--Set Task Progress-----------------------------------\n"; $res = $sw->setTaskProgress($project_id, $task_id, 50, 'Job half-done'); @@ -88,12 +84,15 @@ function tolog($name, $variable, $display = false){ /* echo "\n--Cancel Task-----------------------------------\n"; -# TODO: returns {"goto":"http://www.iron.io","version":"2.0.12"} +# TODO: returns {"msg":"Not found","status_code":404} # or {"msg":"Method POST not allowed","status_code":405} $res = $sw->cancelTask($project_id, $task_id); tolog('cancel_task', $res, true); */ +echo "\n--Deleting Task---------------------------------------\n"; +$res = $sw->deleteTask($project_id, $task_id); +tolog('delete_task', $res, true); # ========================== Schedules ========================== @@ -107,16 +106,13 @@ function tolog($name, $variable, $display = false){ echo "\n--Posting Advanced Shedule--------------------------------------\n"; $start_at = SimpleWorker::dateRfc3339(time()); -echo ">>>$start_at<<<\n"; $schedule_id = $sw->postScheduleAdvanced($project_id, $name, $start_at, 50, null, 4, 0); tolog('post_schedule_advanced', $schedule_id, true); -/* + echo "\n--Deleting Shedule-------------------------------------\n"; -# TODO: $res = $sw->deleteSchedule($project_id, $schedule_id); tolog('delete_schedule', $res, true); -*/