Commit:    e8c45281bb21303e213e91150b2ec6c2823c97fc
Author:    Anatol Belski <[email protected]>         Mon, 11 May 2015 18:14:07 +0200
Parents:   e0602076ca68a5e19a1d842800ff6a36457f34c2
Branches:  master pecl_legacy

Link:       
http://git.php.net/?p=web/rmtools.git;a=commitdiff;h=e8c45281bb21303e213e91150b2ec6c2823c97fc

Log:
basic PickleJob implementation

Changed paths:
  M  client/include/PickleJob.php


Diff:
diff --git a/client/include/PickleJob.php b/client/include/PickleJob.php
index b18ff78..506fcac 100644
--- a/client/include/PickleJob.php
+++ b/client/include/PickleJob.php
@@ -2,9 +2,93 @@
 
 namespace rmtools;
 
+/* XXX need to add locking for job file ops, might be essential if there are 
too much exts/builds */
 
 class PickleJob
 {
+       protected $job_dir;
 
+       public function __construct($job_dir)
+       {
+               if (!is_dir($job_dir)) {
+                       throw new \Exception("Job dir '$job_dir' doesn't 
exist");
+               }
+               $this->job_dir = $job_dir;
+       }
+
+       public function add($sha, $name, $uri)
+       {
+               $fn = "{$this->job_dir}/$sha.job";
+               $data = array(
+                       "name" => $name,
+                       "hash" => $sha,
+                       "uri"  => $uri,
+                       "status" => "new",
+               );
+
+               $this->save($fn, $data);
+       }
+
+       protected function save($fn, $data)
+       {
+               $json = json_encode($data, JSON_PRETTY_PRINT);
+
+               if (strlen($json) != file_put_contents($fn, $json)) {
+                       throw new \Exception("Error while writing data to 
'$fn'");
+               }
+       }
+
+       protected function validStatus($st)
+       {
+               return "new" == $st ||
+                       "fail" == $st ||
+                       "pass" == $st;
+       }
+
+       public function setStatus($sha, $status)
+       {
+               $fn = "{$this->job_dir}/$sha.job";
+
+               if (!$this->validStatus($status)) {
+                       throw new \Exception("Invalid job status '$status'");
+               }
+
+               if (!file_exists($fn)) {
+                       throw new \Exception("Job '$fn' doesn't exist");
+               }
+
+               $data = json_decode(file_get_contents($fn), true);
+
+               $data["status"] = $status;
+
+               $this->save($fn, $data);
+       }
+
+       public function getNextNew()
+       {
+               $jobs = glob("{$this->job_dir}/*.job");
+
+               foreach ($jobs as $fn) {
+                       $data = json_decode(file_get_contents($fn), true);
+                       if ("new" == $data["status"]) {
+                               return $data;
+                       }
+               }
+
+               return NULL;
+       }
+
+       public function cleanup()
+       {
+               $jobs = glob("{$this->job_dir}/*.job");
+               
+               foreach ($jobs as $fn) {
+                       $data = json_decode(file_get_contents($fn), true);
+                       if ("fail " == $data["status"] || "fail " == 
$data["status"]) {
+                               /* XXX save this to PickleDB */
+                               unlink($fn);
+                       }
+               }
+       }
 }


--
PHP Webmaster List Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to