Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Minor improvements.
  • Loading branch information
thousandsofthem committed Dec 9, 2011
1 parent 8b496b9 commit 4813b55
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
29 changes: 15 additions & 14 deletions SimpleWorker.class.php
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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();

Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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(){
Expand Down
3 changes: 2 additions & 1 deletion testBasic.php
Expand Up @@ -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(
Expand All @@ -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);
Expand Down
30 changes: 13 additions & 17 deletions testFull.php
Expand Up @@ -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

Expand All @@ -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 =============================
Expand Down Expand Up @@ -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 ==========================

Expand All @@ -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);
*/



Expand Down

0 comments on commit 4813b55

Please sign in to comment.