Permalink
Browse files

PDO example for client php class. PHPdoc comments.

  • Loading branch information...
1 parent 4813b55 commit 390db9d810e3958e23c0068c0c582fa608c5dcb0 @thousandsofthem thousandsofthem committed Dec 11, 2011
Showing with 89 additions and 14 deletions.
  1. +27 −14 SimpleWorker.class.php
  2. +7 −0 config_sw.ini
  3. +35 −0 testPDO.php
  4. +20 −0 worker_examples/PDO/Pdo.php
View
41 SimpleWorker.class.php
@@ -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){
@@ -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) {
@@ -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);
@@ -130,7 +140,6 @@ public static function dateRfc3339($timestamp = 0) {
$date .= 'Z';
}
return $date;
-
}
public function setProjectId($project_id) {
@@ -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();
@@ -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(
@@ -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;
}
@@ -438,7 +452,6 @@ private function compiledHeaders(){
foreach ($this->headers as $k => $v){
$headers[] = "$k: $v";
}
- #$this->debug("headers", $headers);
return $headers;
}
View
7 config_sw.ini
@@ -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 =
View
35 testPDO.php
@@ -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);
+}
+
View
20 worker_examples/PDO/Pdo.php
@@ -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.