Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

PDO example for client php class. PHPdoc comments.

  • Loading branch information...
commit 390db9d810e3958e23c0068c0c582fa608c5dcb0 1 parent 4813b55
Alexander Shapiotko thousandsofthem authored
41 SimpleWorker.class.php
@@ -39,14 +39,15 @@ class SimpleWorker{
39 39
40 40 /**
41 41 * @param string|array $config_file_or_options
42   - * array of options or name of config file
43   - * required fields in options array or in config:
  42 + * Array of options or name of config file.
  43 + * Fields in options array or in config:
  44 + * Required:
44 45 * - token
45 46 * - protocol
46 47 * - host
47 48 * - port
48 49 * - api_version
49   - * optional:
  50 + * Optional:
50 51 * - default_project_id
51 52 */
52 53 function __construct($config_file_or_options){
@@ -66,12 +67,13 @@ function __construct($config_file_or_options){
66 67 }
67 68
68 69 /**
  70 + * Creates a zip archieve from array of file names
69 71 * @static
70   - * @param string $base_dir full path to directory which contain files
71   - * @param array $files file names, should refer to $base_dir,
72   - * examples: 'worker.php','lib/file.php'
73   - * @param string $destination zip file name
74   - * @param bool $overwrite
  72 + * @param string $base_dir Full path to directory which contain files
  73 + * @param array $files File names, path (both passesed and stored) is relative to $base_dir.
  74 + * Examples: 'worker.php','lib/file.php'
  75 + * @param string $destination Zip file name.
  76 + * @param bool $overwrite Overwite existing file or not.
75 77 * @return bool
76 78 */
77 79 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
105 107 }
106 108 }
107 109
  110 + /**
  111 + * Creates a zip archieve with all files and folders inside specific directory.
  112 + * @static
  113 + * @param string $directory
  114 + * @param string $destination
  115 + * @param bool $overwrite
  116 + * @return bool
  117 + */
108 118 public static function zipDirectory($directory, $destination, $overwrite = false){
109 119 if (!file_exists($directory) || !is_dir($directory)) return false;
110 120 $directory = rtrim($directory, DIRECTORY_SEPARATOR);
@@ -130,7 +140,6 @@ public static function dateRfc3339($timestamp = 0) {
130 140 $date .= 'Z';
131 141 }
132 142 return $date;
133   -
134 143 }
135 144
136 145 public function setProjectId($project_id) {
@@ -181,6 +190,14 @@ public function getCodeDetails($code_id, $project_id = ''){
181 190 return json_decode($this->apiCall(self::GET, $url));
182 191 }
183 192
  193 + /**
  194 + * Uploads your code package
  195 + * @param string $project_id
  196 + * @param string $filename this file will be launched as worker
  197 + * @param string $zipFilename zip file containing code to execute
  198 + * @param string $name referenceable (unique) name for your worker
  199 + * @return mixed
  200 + */
184 201 public function postCode($project_id, $filename, $zipFilename, $name){
185 202 $this->setProjectId($project_id);
186 203 $this->setPostHeaders();
@@ -214,8 +231,7 @@ public function postCode($project_id, $filename, $zipFilename, $name){
214 231 $data .= 'Content-Disposition: form-data; name="file"; filename=$zipFilename' . $eol;
215 232 $data .= 'Content-Type: text/plain' . $eol;
216 233 $data .= 'Content-Transfer-Encoding: binary' . $eol . $eol;
217   - $fileContent = $this->getFileContent($zipFilename);
218   - $data .= $fileContent . $eol;
  234 + $data .= $this->getFileContent($zipFilename) . $eol;
219 235 $data .= "--" . $mime_boundary . "--" . $eol . $eol; // finish with two eol's!!
220 236
221 237 $params = array('http' => array(
@@ -421,8 +437,6 @@ private function postSchedule($project_id, $name, $options, $payload = array()){
421 437
422 438 $this->setCommonHeaders();
423 439 $res = $this->apiCall(self::POST, $url, $request);
424   -
425   - #$this->debug('postSchedule res', $res);
426 440 $shedules = json_decode($res);
427 441 return $shedules->schedules[0]->id;
428 442 }
@@ -438,7 +452,6 @@ private function compiledHeaders(){
438 452 foreach ($this->headers as $k => $v){
439 453 $headers[] = "$k: $v";
440 454 }
441   - #$this->debug("headers", $headers);
442 455 return $headers;
443 456 }
444 457
7 config_sw.ini
@@ -5,3 +5,10 @@ api_version = 2
5 5 host = 174.129.54.171
6 6 port = 8080
7 7 default_project_id = 4ecbde6fcddb133515000001
  8 +
  9 +[pdo]
  10 +driver = mysql
  11 +host = mysql.host.name
  12 +db = testing
  13 +user = root
  14 +password =
35 testPDO.php
... ... @@ -0,0 +1,35 @@
  1 +<?php
  2 +include("SimpleWorker.class.php");
  3 +
  4 +$name = "testPDO.php-".microtime(true);
  5 +
  6 +$config = parse_ini_file('config_sw.ini', true);
  7 +
  8 +# Passing array of options instead of config file.
  9 +$sw = new SimpleWorker($config['simple_worker']);
  10 +$sw->debug_enabled = true;
  11 +
  12 +$project_id = ""; # using default project_id from config
  13 +$zipName = "code/$name.zip";
  14 +
  15 +$zipFile = SimpleWorker::zipDirectory(dirname(__FILE__)."/worker_examples/PDO", $zipName, true);
  16 +
  17 +$res = $sw->postCode($project_id, 'testPDO.php', $zipName, $name);
  18 +print_r($res);
  19 +
  20 +$payload = array(
  21 + 'connection' => $config['pdo'],
  22 + 'yet_another' => array('value', 'value #2')
  23 +);
  24 +
  25 +$task_id = $sw->postTask($project_id, $name, $payload);
  26 +echo "task_id = $task_id \n";
  27 +sleep(10);
  28 +$details = $sw->getTaskDetails($project_id, $task_id);
  29 +print_r($details);
  30 +
  31 +if ($details->status != 'queued'){
  32 + $log = $sw->getLog($project_id, $task_id);
  33 + print_r($log);
  34 +}
  35 +
20 worker_examples/PDO/Pdo.php
... ... @@ -0,0 +1,20 @@
  1 +<?php
  2 +
  3 +function getPayload($argv){
  4 + foreach($argv as $k => $v){
  5 + if ($v == '-payload' && !empty($argv[$k+1]) && file_exists($argv[$k+1])){
  6 + return json_decode(file_get_contents($argv[$k+1]));
  7 + }
  8 + }
  9 + return array();
  10 +}
  11 +
  12 +$payload = getPayload($argv);
  13 +
  14 +$connect_str = "{$payload->connection->driver}:host={$payload->connection->driver};dbname={$payload->connection->db}";
  15 +$db = new PDO($connect_str, $payload->connection->user, $payload->connection->password);
  16 +
  17 +
  18 +# Some hard work with db here.
  19 +$rows = $db->exec("SELECT NOW() AS `now`;");
  20 +print_r($rows);

0 comments on commit 390db9d

Please sign in to comment.
Something went wrong with that request. Please try again.