Skip to content

Commit

Permalink
PDO example for client php class. PHPdoc comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
thousandsofthem committed Dec 11, 2011
1 parent 4813b55 commit 390db9d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 14 deletions.
41 changes: 27 additions & 14 deletions SimpleWorker.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ class SimpleWorker{

/**
* @param string|array $config_file_or_options
* array of options or name of config file
* required fields in options array or in config:
* Array of options or name of config file.
* Fields in options array or in config:
* Required:
* - token
* - protocol
* - host
* - port
* - api_version
* optional:
* Optional:
* - default_project_id
*/
function __construct($config_file_or_options){
Expand All @@ -66,12 +67,13 @@ function __construct($config_file_or_options){
}

/**
* Creates a zip archieve from array of file names
* @static
* @param string $base_dir full path to directory which contain files
* @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
* @param string $base_dir Full path to directory which contain files
* @param array $files File names, path (both passesed and stored) is relative to $base_dir.
* Examples: 'worker.php','lib/file.php'
* @param string $destination Zip file name.
* @param bool $overwrite Overwite existing file or not.
* @return bool
*/
public static function createZip($base_dir, $files = array(), $destination, $overwrite = false) {
Expand Down Expand Up @@ -105,6 +107,14 @@ public static function createZip($base_dir, $files = array(), $destination, $ove
}
}

/**
* Creates a zip archieve with all files and folders inside specific directory.
* @static
* @param string $directory
* @param string $destination
* @param bool $overwrite
* @return bool
*/
public static function zipDirectory($directory, $destination, $overwrite = false){
if (!file_exists($directory) || !is_dir($directory)) return false;
$directory = rtrim($directory, DIRECTORY_SEPARATOR);
Expand All @@ -130,7 +140,6 @@ public static function dateRfc3339($timestamp = 0) {
$date .= 'Z';
}
return $date;

}

public function setProjectId($project_id) {
Expand Down Expand Up @@ -181,6 +190,14 @@ public function getCodeDetails($code_id, $project_id = ''){
return json_decode($this->apiCall(self::GET, $url));
}

/**
* Uploads your code package
* @param string $project_id
* @param string $filename this file will be launched as worker
* @param string $zipFilename zip file containing code to execute
* @param string $name referenceable (unique) name for your worker
* @return mixed
*/
public function postCode($project_id, $filename, $zipFilename, $name){
$this->setProjectId($project_id);
$this->setPostHeaders();
Expand Down Expand Up @@ -214,8 +231,7 @@ public function postCode($project_id, $filename, $zipFilename, $name){
$data .= 'Content-Disposition: form-data; name="file"; filename=$zipFilename' . $eol;
$data .= 'Content-Type: text/plain' . $eol;
$data .= 'Content-Transfer-Encoding: binary' . $eol . $eol;
$fileContent = $this->getFileContent($zipFilename);
$data .= $fileContent . $eol;
$data .= $this->getFileContent($zipFilename) . $eol;
$data .= "--" . $mime_boundary . "--" . $eol . $eol; // finish with two eol's!!

$params = array('http' => array(
Expand Down Expand Up @@ -421,8 +437,6 @@ private function postSchedule($project_id, $name, $options, $payload = array()){

$this->setCommonHeaders();
$res = $this->apiCall(self::POST, $url, $request);

#$this->debug('postSchedule res', $res);
$shedules = json_decode($res);
return $shedules->schedules[0]->id;
}
Expand All @@ -438,7 +452,6 @@ private function compiledHeaders(){
foreach ($this->headers as $k => $v){
$headers[] = "$k: $v";
}
#$this->debug("headers", $headers);
return $headers;
}

Expand Down
7 changes: 7 additions & 0 deletions config_sw.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ api_version = 2
host = 174.129.54.171
port = 8080
default_project_id = 4ecbde6fcddb133515000001

[pdo]
driver = mysql
host = mysql.host.name
db = testing
user = root
password =
35 changes: 35 additions & 0 deletions testPDO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
include("SimpleWorker.class.php");

$name = "testPDO.php-".microtime(true);

$config = parse_ini_file('config_sw.ini', true);

# Passing array of options instead of config file.
$sw = new SimpleWorker($config['simple_worker']);
$sw->debug_enabled = true;

$project_id = ""; # using default project_id from config
$zipName = "code/$name.zip";

$zipFile = SimpleWorker::zipDirectory(dirname(__FILE__)."/worker_examples/PDO", $zipName, true);

$res = $sw->postCode($project_id, 'testPDO.php', $zipName, $name);
print_r($res);

$payload = array(
'connection' => $config['pdo'],
'yet_another' => array('value', 'value #2')
);

$task_id = $sw->postTask($project_id, $name, $payload);
echo "task_id = $task_id \n";
sleep(10);
$details = $sw->getTaskDetails($project_id, $task_id);
print_r($details);

if ($details->status != 'queued'){
$log = $sw->getLog($project_id, $task_id);
print_r($log);
}

20 changes: 20 additions & 0 deletions worker_examples/PDO/Pdo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

function getPayload($argv){
foreach($argv as $k => $v){
if ($v == '-payload' && !empty($argv[$k+1]) && file_exists($argv[$k+1])){
return json_decode(file_get_contents($argv[$k+1]));
}
}
return array();
}

$payload = getPayload($argv);

$connect_str = "{$payload->connection->driver}:host={$payload->connection->driver};dbname={$payload->connection->db}";
$db = new PDO($connect_str, $payload->connection->user, $payload->connection->password);


# Some hard work with db here.
$rows = $db->exec("SELECT NOW() AS `now`;");
print_r($rows);

0 comments on commit 390db9d

Please sign in to comment.