Permalink
Browse files

Minor improvements.

  • Loading branch information...
1 parent 8b496b9 commit 4813b55786bdf6e72ca3e42d2f8e3e4e74c6bff3 @thousandsofthem thousandsofthem committed Dec 9, 2011
Showing with 30 additions and 32 deletions.
  1. +15 −14 SimpleWorker.class.php
  2. +2 −1 testBasic.php
  3. +13 −17 testFull.php
View
29 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,18 +199,16 @@ 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";
$data = '';
$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,26 +463,27 @@ 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'])) {
$params['oauth'] = $this->token;
}
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));
break;
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(){
View
3 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);
View
30 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,28 +72,27 @@ 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: <empty responce>
-$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');
tolog('set_task_progress', $res, true);
/*
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: <empty responce>
$res = $sw->deleteSchedule($project_id, $schedule_id);
tolog('delete_schedule', $res, true);
-*/

0 comments on commit 4813b55

Please sign in to comment.