Massive refactor to use SihnonFramework and PEAR's Net_Gearman
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
<?php
|
||||
|
||||
class RippingCluster_BackgroundTask {
|
||||
|
||||
protected function __construct() {
|
||||
|
||||
}
|
||||
|
||||
public static function run($command) {
|
||||
$pipes = array();
|
||||
$pid = proc_open($command . ' &', array(), $pipes, getcwd());
|
||||
proc_close($pid);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
@@ -1,62 +0,0 @@
|
||||
<?php
|
||||
|
||||
class RippingCluster_Cache {
|
||||
|
||||
protected $config;
|
||||
protected $cache_dir;
|
||||
|
||||
public function __construct(RippingCluster_Config $config) {
|
||||
$this->config = $config;
|
||||
$this->cache_dir = $config->get('cache.base_dir');
|
||||
|
||||
if (is_dir($this->cache_dir)) {
|
||||
if ( ! is_writeable($this->cache_dir)) {
|
||||
throw new RippingCluster_Exception_InvalidCacheDir();
|
||||
}
|
||||
} else {
|
||||
if ( ! RippingCluster_Main::mkdir_recursive($this->cache_dir)) {
|
||||
throw new RippingCluster_Exception_InvalidCacheDir();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function cacheFilename($source_filename) {
|
||||
return $this->cache_dir . sha1($source_filename);
|
||||
}
|
||||
|
||||
public function exists($source_filename, $ttl = 3600) {
|
||||
$cache_filename = $this->cacheFilename($source_filename);
|
||||
|
||||
// Check to see if the file is cached
|
||||
if (!file_exists($cache_filename)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check to see if the cache has expired
|
||||
if (filemtime($cache_filename) + $ttl < time()) {
|
||||
// Delete the cached item
|
||||
unlink($cache_filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function store($source_filename, $content) {
|
||||
$cache_filename = $this->cacheFilename($source_filename);
|
||||
return file_put_contents($cache_filename, $content);
|
||||
}
|
||||
|
||||
public function fetch($source_filename, $ttl = 3600) {
|
||||
$cache_filename = $this->cacheFilename($source_filename);
|
||||
|
||||
if (!$this->exists($source_filename)) {
|
||||
throw new RippingCluster_Exception_CacheObjectNotFound($source_filename);
|
||||
}
|
||||
|
||||
return file_get_contents($cache_filename);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
@@ -1,13 +0,0 @@
|
||||
<?php
|
||||
|
||||
class RippingCluster_ClientLogEntry extends RippingCluster_LogEntry {
|
||||
|
||||
public static function initialise() {
|
||||
parent::$table_name = 'client_log';
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
RippingCluster_ClientLogEntry::initialise();
|
||||
|
||||
?>
|
||||
@@ -1,145 +0,0 @@
|
||||
<?php
|
||||
|
||||
class RippingCluster_Config {
|
||||
|
||||
/**
|
||||
* Boolean value type
|
||||
* @var bool
|
||||
*/
|
||||
const TYPE_BOOL = 'bool';
|
||||
|
||||
/**
|
||||
* Integer value type
|
||||
* @var int
|
||||
*/
|
||||
const TYPE_INT = 'int';
|
||||
|
||||
/**
|
||||
* Float value type
|
||||
* @var float
|
||||
*/
|
||||
const TYPE_FLOAT = 'float';
|
||||
|
||||
/**
|
||||
* String value type
|
||||
* @var string
|
||||
*/
|
||||
const TYPE_STRING = 'string';
|
||||
|
||||
/**
|
||||
* String List value type; list of newline separated strings
|
||||
* @var array(string)
|
||||
*/
|
||||
const TYPE_STRING_LIST = 'array(string)';
|
||||
|
||||
/**
|
||||
* Contents of the dbconfig file
|
||||
* @var string
|
||||
*/
|
||||
private $dbconfig;
|
||||
|
||||
/**
|
||||
* Database object created for the lifetime of this script
|
||||
* @var RippingCluster_Database
|
||||
*/
|
||||
private $database;
|
||||
|
||||
/**
|
||||
* Associative array of connection parameters for the database configuration
|
||||
* @var array(string=>string)
|
||||
*/
|
||||
private $databaseConfig = array();
|
||||
|
||||
/**
|
||||
* Associative array of settings loaded from the database
|
||||
* @var array(string=>array(string=>string))
|
||||
*/
|
||||
private $settings = array();
|
||||
|
||||
/**
|
||||
* Constructs a new instance of the Config class
|
||||
*
|
||||
* @param string $dbconfig Database configuration file contents
|
||||
* @return RippingCluster_Config
|
||||
*/
|
||||
public function __construct($dbconfig) {
|
||||
$this->dbconfig = $dbconfig;
|
||||
|
||||
$this->parseDatabaseConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the contents of the database configuration file so that individual settings can be retrieved.
|
||||
*
|
||||
*/
|
||||
public function parseDatabaseConfig() {
|
||||
$this->databaseConfig = parse_ini_file($this->dbconfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of the named item from the database configuration file
|
||||
*
|
||||
* @param string $key Name of the setting to retrieve
|
||||
*/
|
||||
public function getDatabase($key) {
|
||||
if (!isset($this->databaseConfig[$key])) {
|
||||
throw new RippingCluster_Exception_DatabaseConfigMissing($key);
|
||||
}
|
||||
|
||||
return $this->databaseConfig[$key];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the database instance used by this object
|
||||
*
|
||||
* @param RippingCluster_Database $database Database instance
|
||||
*/
|
||||
public function setDatabase(RippingCluster_Database $database) {
|
||||
$this->database = $database;
|
||||
$this->preload();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the entire list of settings from the database
|
||||
*
|
||||
*/
|
||||
private function preload() {
|
||||
if (!$this->database) {
|
||||
throw new RippingCluster_Exception_NoDatabaseConnection();
|
||||
}
|
||||
|
||||
$this->settings = $this->database->selectAssoc('SELECT name,type,value FROM settings', 'name', array('name', 'value', 'type'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Identifies whether the named setting exists
|
||||
*
|
||||
* @param string $key Name of the setting
|
||||
* @return bool
|
||||
*/
|
||||
public function exists($key) {
|
||||
return isset($this->settings[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the value of the named setting
|
||||
*
|
||||
* @param string $key Name of the setting
|
||||
*/
|
||||
public function get($key) {
|
||||
if (!isset($this->settings[$key])) {
|
||||
throw new RippingCluster_Exception_UnknownSetting($key);
|
||||
}
|
||||
|
||||
switch ($this->settings[$key]['type']) {
|
||||
case self::TYPE_STRING_LIST:
|
||||
return array_map('trim', explode("\n", $this->settings[$key]['value']));
|
||||
|
||||
default:
|
||||
return $this->settings[$key]['value'];
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
@@ -1,141 +0,0 @@
|
||||
<?php
|
||||
|
||||
class RippingCluster_Database {
|
||||
|
||||
private $config;
|
||||
private $dbh;
|
||||
|
||||
private $hostname;
|
||||
private $username;
|
||||
private $password;
|
||||
private $dbname;
|
||||
|
||||
private $prepared_statements = array();
|
||||
|
||||
public function __construct(RippingCluster_Config $config) {
|
||||
$this->config = $config;
|
||||
|
||||
$this->hostname = $this->config->getDatabase('hostname');
|
||||
$this->username = $this->config->getDatabase('username');
|
||||
$this->password = $this->config->getDatabase('password');
|
||||
$this->dbname = $this->config->getDatabase('dbname');
|
||||
|
||||
try {
|
||||
$this->dbh = new PDO("mysql:host={$this->hostname};dbname={$this->dbname}", $this->username, $this->password);
|
||||
} catch (PDOException $e) {
|
||||
throw new RippingCluster_Exception_DatabaseConnectFailed($e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
$this->dbh = null;
|
||||
}
|
||||
|
||||
public function selectAssoc($sql, $key_col, $value_cols) {
|
||||
$results = array();
|
||||
|
||||
foreach ($this->dbh->query($sql) as $row) {
|
||||
if (is_array($value_cols)) {
|
||||
$values = array();
|
||||
foreach ($value_cols as $value_col) {
|
||||
$values[$value_col] = $row[$value_col];
|
||||
}
|
||||
|
||||
$results[$row[$key_col]] = $values;
|
||||
} else {
|
||||
$results[$row[$key_col]] = $row[$value_col];
|
||||
}
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
public function selectList($sql, $bind_params = null) {
|
||||
if ($bind_params) {
|
||||
$stmt = $this->dbh->prepare($sql);
|
||||
|
||||
foreach ($bind_params as $param) {
|
||||
$stmt->bindValue(':'.$param['name'], $param['value'], $param['type']);
|
||||
}
|
||||
|
||||
$result = $stmt->execute();
|
||||
if (!$result) {
|
||||
list($dummy, $code, $message) = $stmt->errorInfo();
|
||||
throw new RippingCluster_Exception_DatabaseQueryFailed($message, $code);
|
||||
}
|
||||
|
||||
return $stmt->fetchAll();
|
||||
|
||||
} else {
|
||||
$results = array();
|
||||
|
||||
$result = $this->dbh->query($sql);
|
||||
foreach ($result as $row) {
|
||||
$results[] = $row;
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
|
||||
public function selectOne($sql, $bind_params = null) {
|
||||
$rows = $this->selectList($sql, $bind_params);
|
||||
if (count($rows) != 1) {
|
||||
throw new RippingCluster_Exception_ResultCountMismatch(count($rows));
|
||||
}
|
||||
|
||||
return $rows[0];
|
||||
}
|
||||
|
||||
public function insert($sql, $bind_params = null) {
|
||||
$stmt = $this->dbh->prepare($sql);
|
||||
|
||||
if ($bind_params) {
|
||||
foreach ($bind_params as $param) {
|
||||
if (isset($param['type'])) {
|
||||
$stmt->bindValue(':'.$param['name'], $param['value'], $param['type']);
|
||||
} else {
|
||||
$stmt->bindValue(':'.$param['name'], $param['value']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$result = $stmt->execute();
|
||||
if (!$result) {
|
||||
list($code, $dummy, $message) = $stmt->errorInfo();
|
||||
throw new RippingCluster_Exception_DatabaseQueryFailed($message, $code);
|
||||
}
|
||||
}
|
||||
|
||||
public function update($sql, $bind_params = null) {
|
||||
$stmt = $this->dbh->prepare($sql);
|
||||
|
||||
if ($bind_params) {
|
||||
foreach ($bind_params as $param) {
|
||||
if (isset($param['type'])) {
|
||||
$stmt->bindValue(':'.$param['name'], $param['value'], $param['type']);
|
||||
} else {
|
||||
$stmt->bindValue(':'.$param['name'], $param['value']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$result = $stmt->execute();
|
||||
if (!$result) {
|
||||
list($code, $dummy, $message) = $stmt->errorInfo();
|
||||
throw new RippingCluster_Exception_DatabaseQueryFailed($message, $code);
|
||||
}
|
||||
}
|
||||
|
||||
public function errorInfo() {
|
||||
return $this->dbh->errorInfo();
|
||||
}
|
||||
|
||||
public function lastInsertId() {
|
||||
return $this->dbh->lastInsertId();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -1,113 +0,0 @@
|
||||
<?php
|
||||
|
||||
class RippingCluster_ForegroundTask {
|
||||
|
||||
const PIPE_STDIN = 0;
|
||||
const PIPE_STDOUT = 1;
|
||||
const PIPE_STDERR = 2;
|
||||
|
||||
private function __construct() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Code largely taken from user submitted comment on http://php.sihnon.net/manual/en/function.proc-open.php
|
||||
* @param unknown_type $command
|
||||
* @param unknown_type $cwd
|
||||
* @param unknown_type $env
|
||||
* @param unknown_type $stdin
|
||||
* @param unknown_type $callback_stdout
|
||||
* @param unknown_type $callback_stderr
|
||||
*/
|
||||
public static function execute($command, $cwd = null, $env = null, $stdin = null, $callback_stdout = null, $callback_stderr = null, $identifier = null) {
|
||||
$txOff = 0;
|
||||
$txLen = strlen($stdin);
|
||||
$stdout = '';
|
||||
$stdoutDone = FALSE;
|
||||
$stderr = '';
|
||||
$stderrDone = FALSE;
|
||||
|
||||
$descriptors = array(
|
||||
self::PIPE_STDIN => array('pipe', 'r'),
|
||||
self::PIPE_STDOUT => array('pipe', 'w'),
|
||||
self::PIPE_STDERR => array('pipe', 'w'),
|
||||
);
|
||||
|
||||
$pipes = array();
|
||||
$process = proc_open($command, $descriptors, $pipes);
|
||||
|
||||
stream_set_blocking($pipes[self::PIPE_STDIN], 0); // Make stdin/stdout/stderr non-blocking
|
||||
stream_set_blocking($pipes[self::PIPE_STDOUT], 0);
|
||||
stream_set_blocking($pipes[self::PIPE_STDERR], 0);
|
||||
|
||||
if ($txLen == 0) {
|
||||
fclose($pipes[0]);
|
||||
}
|
||||
|
||||
while (true) {
|
||||
$rx = array(); // The program's stdout/stderr
|
||||
if (!$stdoutDone) {
|
||||
$rx[] = $pipes[self::PIPE_STDOUT];
|
||||
}
|
||||
if (!$stderrDone) {
|
||||
$rx[] = $pipes[self::PIPE_STDERR];
|
||||
}
|
||||
|
||||
$tx = array(); // The program's stdin
|
||||
if ($txOff < $txLen) {
|
||||
$tx[] = $pipes[self::PIPE_STDIN];
|
||||
}
|
||||
|
||||
$ex = array();
|
||||
|
||||
stream_select($rx, $tx, $ex, null, null); // Block til r/w possible
|
||||
if (!empty($tx)) {
|
||||
$txRet = fwrite($pipes[self::PIPE_STDIN], substr($stdin, $txOff, 8192));
|
||||
if ($txRet !== false) {
|
||||
$txOff += $txRet;
|
||||
}
|
||||
if ($txOff >= $txLen) {
|
||||
fclose($pipes[self::PIPE_STDIN]);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($rx as $r) {
|
||||
if ($r == $pipes[self::PIPE_STDOUT]) {
|
||||
$chunk = fread($pipes[self::PIPE_STDOUT], 8192);
|
||||
if (feof($pipes[self::PIPE_STDOUT])) {
|
||||
fclose($pipes[self::PIPE_STDOUT]); $stdoutDone = true;
|
||||
}
|
||||
|
||||
if ($callback_stdout) {
|
||||
call_user_func($callback_stdout, $identifier, $chunk);
|
||||
} else {
|
||||
$stdout .= $chunk;
|
||||
}
|
||||
|
||||
} else if ($r == $pipes[self::PIPE_STDERR]) {
|
||||
$chunk = fread($pipes[self::PIPE_STDERR], 8192);
|
||||
if (feof($pipes[self::PIPE_STDERR])) {
|
||||
fclose($pipes[self::PIPE_STDERR]); $stderrDone = true;
|
||||
}
|
||||
|
||||
if ($callback_stderr) {
|
||||
call_user_func($callback_stderr, $identifier, $chunk);
|
||||
} else {
|
||||
$stderr .= $chunk;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_resource($process))
|
||||
break;
|
||||
|
||||
if ($txOff >= $txLen && $stdoutDone && $stderrDone)
|
||||
break;
|
||||
}
|
||||
|
||||
return array(proc_close($process), $stdout, $stderr);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
<?php
|
||||
|
||||
interface RippingCluster_IPlugin {
|
||||
|
||||
public static function init();
|
||||
|
||||
public static function name();
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -1,9 +0,0 @@
|
||||
<?php
|
||||
|
||||
interface RippingCluster_IPluginFactory {
|
||||
|
||||
public static function init();
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -1,61 +0,0 @@
|
||||
<?php
|
||||
|
||||
class RippingCluster_Log {
|
||||
|
||||
private static $hostname = '';
|
||||
|
||||
private $database;
|
||||
private $config;
|
||||
private $table;
|
||||
|
||||
public function __construct(RippingCluster_Database $database, RippingCluster_Config $config, $table) {
|
||||
$this->database = $database;
|
||||
$this->config = $config;
|
||||
$this->table = $table;
|
||||
|
||||
}
|
||||
|
||||
public function log($severity, $message, $job_id = 0) {
|
||||
$this->database->insert("INSERT INTO {$this->table} (job_id,level,ctime,pid,hostname,progname,line,message) VALUES(:job_id, :level, :ctime, :pid, :hostname, :progname, :line, :message)",
|
||||
array(
|
||||
array('name' => 'job_id', 'value' => $job_id, 'type' => PDO::PARAM_INT),
|
||||
array('name' => 'level', 'value' => $severity, 'type' => PDO::PARAM_STR),
|
||||
array('name' => 'ctime', 'value' => time(), 'type' => PDO::PARAM_INT),
|
||||
array('name' => 'pid', 'value' => 0, 'type' => PDO::PARAM_INT),
|
||||
array('name' => 'hostname', 'value' => self::$hostname, 'type' => PDO::PARAM_STR),
|
||||
array('name' => 'progname', 'value' => 'webui', 'type' => PDO::PARAM_STR),
|
||||
array('name' => 'line', 'value' => 0, 'type' => PDO::PARAM_INT),
|
||||
array('name' => 'message', 'value' => $message, 'type' => PDO::PARAM_STR)
|
||||
)
|
||||
);
|
||||
|
||||
if (HBC_File == 'worker') {
|
||||
echo date("r") . ' ' . $message . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
public function debug($message, $job_id = 0) {
|
||||
return $this->log('DEBUG', $message, $job_id);
|
||||
}
|
||||
|
||||
public function info($message, $job_id = 0) {
|
||||
return $this->log('INFO', $message, $job_id);
|
||||
}
|
||||
|
||||
public function warning($message, $job_id = 0) {
|
||||
return $this->log('WARNING', $message, $job_id);
|
||||
}
|
||||
|
||||
public function error($message, $job_id = 0) {
|
||||
return $this->log('ERROR', $message, $job_id);
|
||||
}
|
||||
|
||||
public static function initialise() {
|
||||
self::$hostname = trim(`hostname`);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
RippingCluster_Log::initialise();
|
||||
|
||||
?>
|
||||
@@ -1,122 +0,0 @@
|
||||
<?php
|
||||
|
||||
abstract class RippingCluster_LogEntry {
|
||||
|
||||
protected static $table_name = "";
|
||||
|
||||
protected $id;
|
||||
protected $job_id;
|
||||
protected $level;
|
||||
protected $ctime;
|
||||
protected $pid;
|
||||
protected $hostname;
|
||||
protected $progname;
|
||||
protected $line;
|
||||
protected $message;
|
||||
|
||||
protected function __construct($id, $job_id, $level, $ctime, $pid, $hostname, $progname, $line, $message) {
|
||||
$this->id = $id;
|
||||
$this->job_id = $job_id;
|
||||
$this->level = $level;
|
||||
$this->ctime = $ctime;
|
||||
$this->pid = $pid;
|
||||
$this->hostname = $hostname;
|
||||
$this->progname = $progname;
|
||||
$this->line = $line;
|
||||
$this->message = $message;
|
||||
}
|
||||
|
||||
public static function fromDatabaseRow($row) {
|
||||
return new RippingCluster_ClientLogEntry(
|
||||
$row['id'],
|
||||
$row['job_id'],
|
||||
$row['level'],
|
||||
$row['ctime'],
|
||||
$row['pid'],
|
||||
$row['hostname'],
|
||||
$row['progname'],
|
||||
$row['line'],
|
||||
$row['message']
|
||||
);
|
||||
}
|
||||
|
||||
public static function fromId($id) {
|
||||
$database = RippingCluster_Main::instance()->database();
|
||||
return RippingCluster_ClientLogEntry::fromDatabaseRow(
|
||||
$database->selectOne('SELECT * FROM '.self::$table_name.' WHERE id=:id', array(
|
||||
array('name' => 'id', 'value' => $id, 'type' => PDO::PARAM_INT)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function recent($limit = 100) {
|
||||
$entries = array();
|
||||
|
||||
$database = RippingCluster_Main::instance()->database();
|
||||
foreach ($database->selectList('SELECT * FROM '.self::$table_name.' ORDER BY ctime DESC LIMIT :limit', array(
|
||||
array('name' => 'limit', 'value' => $limit, 'type' => PDO::PARAM_INT)
|
||||
)) as $row) {
|
||||
$entries[] = self::fromDatabaseRow($row);
|
||||
}
|
||||
|
||||
return $entries;
|
||||
}
|
||||
|
||||
public static function recentForJob($job_id, $limit = 100) {
|
||||
$entries = array();
|
||||
|
||||
$database = RippingCluster_Main::instance()->database();
|
||||
foreach ($database->selectList('SELECT * FROM '.self::$table_name.' WHERE job_id=:job_id ORDER BY ctime DESC LIMIT :limit', array(
|
||||
array('name' => 'job_id', 'value' => $job_id, 'type' => PDO::PARAM_INT),
|
||||
array('name' => 'limit', 'value' => $limit, 'type' => PDO::PARAM_INT)
|
||||
)) as $row) {
|
||||
$entries[] = self::fromDatabaseRow($row);
|
||||
}
|
||||
|
||||
return $entries;
|
||||
}
|
||||
|
||||
public static function allForNoJob() {
|
||||
return self::allForJob(0);
|
||||
}
|
||||
|
||||
public function id() {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function jobId() {
|
||||
return $this->job_id;
|
||||
}
|
||||
|
||||
public function level() {
|
||||
return $this->level;
|
||||
}
|
||||
|
||||
public function ctime() {
|
||||
return $this->ctime;
|
||||
}
|
||||
|
||||
public function pid() {
|
||||
return $this->pid;
|
||||
}
|
||||
|
||||
public function hostname() {
|
||||
return $this->hostname;
|
||||
}
|
||||
|
||||
public function progname() {
|
||||
return $this->progname;
|
||||
}
|
||||
|
||||
public function line() {
|
||||
return $this->line;
|
||||
}
|
||||
|
||||
public function message() {
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
@@ -1,230 +0,0 @@
|
||||
<?php
|
||||
|
||||
require 'smarty/Smarty.class.php';
|
||||
|
||||
class RippingCluster_Main {
|
||||
|
||||
private static $instance;
|
||||
|
||||
private $smarty;
|
||||
private $config;
|
||||
private $database;
|
||||
private $log;
|
||||
private $request;
|
||||
private $cache;
|
||||
|
||||
private $base_uri;
|
||||
|
||||
private function __construct() {
|
||||
$request_string = isset($_GET['l']) ? $_GET['l'] : '';
|
||||
|
||||
$log_table = null;
|
||||
switch(HBC_File) {
|
||||
case 'index':
|
||||
case 'run-jobs': {
|
||||
$log_table = 'client_log';
|
||||
} break;
|
||||
|
||||
case 'worker': {
|
||||
$log_table = 'worker_log';
|
||||
}
|
||||
}
|
||||
|
||||
$this->config = new RippingCluster_Config(RippingCluster_DBConfig);
|
||||
$this->database = new RippingCluster_Database($this->config);
|
||||
$this->config->setDatabase($this->database);
|
||||
|
||||
$this->log = new RippingCluster_Log($this->database, $this->config, $log_table);
|
||||
$this->request = new RippingCluster_RequestParser($request_string);
|
||||
$this->cache = new RippingCluster_Cache($this->config);
|
||||
|
||||
$this->smarty = new Smarty();
|
||||
$this->smarty->template_dir = './templates';
|
||||
$this->smarty->compile_dir = './tmp/templates';
|
||||
$this->smarty->cache_dir = './tmp/cache';
|
||||
$this->smarty->config_fir = './config';
|
||||
|
||||
$this->smarty->register_modifier('formatDuration', array('RippingCluster_Main', 'formatDuration'));
|
||||
|
||||
$this->smarty->assign('version', '0.1');
|
||||
|
||||
$this->base_uri = dirname($_SERVER['SCRIPT_NAME']) . '/';
|
||||
$this->smarty->assign('base_uri', $this->base_uri);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return RippingCluster_Main
|
||||
*/
|
||||
public static function instance() {
|
||||
if (!self::$instance) {
|
||||
self::$instance = new RippingCluster_Main();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function smarty() {
|
||||
return $this->smarty;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return RippingCluster_Config
|
||||
*/
|
||||
public function config() {
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return RippingCluster_Database
|
||||
*/
|
||||
public function database() {
|
||||
return $this->database;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return RippingCluster_Log
|
||||
*/
|
||||
public function log() {
|
||||
return $this->log;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return RippingCluster_RequestParser
|
||||
*/
|
||||
public function request() {
|
||||
return $this->request;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return RippingCluster_Cache
|
||||
*/
|
||||
public function cache() {
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
public function baseUri() {
|
||||
return $this->base_uri;
|
||||
}
|
||||
|
||||
public function absoluteUrl($relative_url) {
|
||||
$secure = isset($_SERVER['secure']);
|
||||
$port = $_SERVER['SERVER_PORT'];
|
||||
return 'http' . ($secure ? 's' : '') . '://'
|
||||
. $_SERVER['HTTP_HOST'] . (($port == 80 || ($secure && $port == 443)) ? '' : ':' . $port)
|
||||
. '/' . $this->base_uri . $relative_url;
|
||||
}
|
||||
|
||||
public static function initialise() {
|
||||
spl_autoload_register(array('RippingCluster_Main','autoload'));
|
||||
}
|
||||
|
||||
public static function autoload($classname) {
|
||||
// Ensure the classname contains only valid class name characters
|
||||
if (!preg_match('/^[A-Z][a-zA-Z0-9_]*$/', $classname)) {
|
||||
throw new Exception('Illegal characters in classname'); // TODO Subclass this exception
|
||||
}
|
||||
|
||||
// Ensure the class to load begins with our prefix
|
||||
if (!preg_match('/^RippingCluster_/', $classname)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Special case: All exceptions are stored in the same file
|
||||
if (preg_match('/^RippingCluster_Exception/', $classname)) {
|
||||
require_once(RippingCluster_Lib . 'RippingCluster/Exceptions.class.php');
|
||||
return;
|
||||
}
|
||||
|
||||
// Replace any underscores with directory separators
|
||||
$filename = RippingCluster_Lib . preg_replace('/_/', '/', $classname);
|
||||
|
||||
// Tack on the class file suffix
|
||||
$filename .= '.class.php';
|
||||
|
||||
// If this file exists, load it
|
||||
if (file_exists($filename)) {
|
||||
require_once $filename;
|
||||
}
|
||||
}
|
||||
|
||||
public static function mkdir_recursive($directory, $permissions=0777) {
|
||||
$parts = explode('/', $directory);
|
||||
$path = '';
|
||||
for ($i=1,$l=count($parts); $i<=$l; $i++) {
|
||||
$iPath = $parts;
|
||||
$path = join('/', array_slice($iPath, 0, $i));
|
||||
if (empty($path)) continue;
|
||||
if (!file_exists($path)) {
|
||||
if (!mkdir($path)) return false;
|
||||
if (!chmod($path, $permissions)) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function rmdir_recursive($dir) {
|
||||
if (is_dir($dir)) {
|
||||
$objects = scandir($dir);
|
||||
foreach ($objects as $object) {
|
||||
if ($object != "." && $object != "..") {
|
||||
if (filetype($dir."/".$object) == "dir") self::rmdir_recursive($dir."/".$object); else unlink($dir."/".$object);
|
||||
}
|
||||
}
|
||||
reset($objects);
|
||||
rmdir($dir);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function issetelse($var, $default = null) {
|
||||
if (isset($var)) {
|
||||
return $var;
|
||||
}
|
||||
|
||||
if (is_string($default) && preg_match('/^RippingCluster_Exception/', $default) && class_exists($default) && is_subclass_of($default, RippingCluster_Exception)) {
|
||||
throw new $default();
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
public static function formatDuration($time) {
|
||||
if (is_null($time)) {
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
$labels = array('seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years');
|
||||
$limits = array(1, 60, 3600, 86400, 604800, 2592000, 31556926, PHP_INT_MAX);
|
||||
|
||||
$working_time = $time;
|
||||
|
||||
$result = "";
|
||||
$ptr = count($labels) - 1;
|
||||
|
||||
while ($ptr >= 0 && $working_time < $limits[$ptr]) {
|
||||
--$ptr;
|
||||
}
|
||||
|
||||
while ($ptr >= 0) {
|
||||
$unit_time = floor($working_time / $limits[$ptr]);
|
||||
$working_time -= $unit_time * $limits[$ptr];
|
||||
$result = $result . ' ' . $unit_time . ' ' . $labels[$ptr];
|
||||
--$ptr;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
RippingCluster_Main::initialise();
|
||||
|
||||
?>
|
||||
@@ -1,74 +0,0 @@
|
||||
<?php
|
||||
|
||||
class RippingCluster_Page {
|
||||
|
||||
private $smarty;
|
||||
private $request;
|
||||
|
||||
private $page;
|
||||
|
||||
public function __construct(Smarty $smarty, RippingCluster_RequestParser $request) {
|
||||
$this->smarty = $smarty;
|
||||
$this->request = $request;
|
||||
$this->page = $request->page();
|
||||
}
|
||||
|
||||
public function page() {
|
||||
return $this->page;
|
||||
}
|
||||
|
||||
public function template_filename() {
|
||||
return $this->page . '.tpl';
|
||||
}
|
||||
|
||||
public function evaluate($template_variables = array()) {
|
||||
$code_filename = $this->page . '.php';
|
||||
$template_filename = $this->template_filename();
|
||||
|
||||
try {
|
||||
$this->render($template_filename, $code_filename, $template_variables);
|
||||
} catch (RippingCluster_Exception_AbortEntirePage $e) {
|
||||
return false;
|
||||
} catch (RippingCluster_Exception_FileNotFound $e) {
|
||||
$this->render('errors/404.tpl', 'errors/404.php');
|
||||
} catch (RippingCluster_Exception $e) {
|
||||
$this->render('errors/unhandled-exception.tpl', 'errors/unhandled-exception.php', array(
|
||||
'exception' => $e,
|
||||
));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function render($template_filename, $code_filename = null, $template_variables = array()) {
|
||||
if ( ! $this->smarty->template_exists($template_filename)) {
|
||||
throw new RippingCluster_Exception_FileNotFound($template_filename);
|
||||
}
|
||||
|
||||
// Copy all the template variables into the namespace for this function,
|
||||
// so that they are readily available to the template
|
||||
foreach ($template_variables as $__k => $__v) {
|
||||
$$__k = $__v;
|
||||
}
|
||||
|
||||
// Include the template code file, which will do all the work for this page
|
||||
$real_code_filename = 'pages/' . $code_filename;
|
||||
if ($code_filename && file_exists($real_code_filename)) {
|
||||
include $real_code_filename;
|
||||
}
|
||||
|
||||
// Now execute the template itself, which will render the results of the code file
|
||||
$this->smarty->assign('page_content', $this->smarty->fetch($template_filename));
|
||||
}
|
||||
|
||||
public static function redirect($relative_url) {
|
||||
$absolute_url = RippingCluster_Main::instance()->absoluteUrl($relative_url);
|
||||
|
||||
header("Location: $absolute_url");
|
||||
|
||||
throw new RippingCluster_Exception_AbortEntirePage();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
@@ -1,30 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Base class for all plugins, providing default implementations for
|
||||
* standard plugin methods.
|
||||
*
|
||||
* @class RippingCluster_PluginBase
|
||||
*/
|
||||
class RippingCluster_PluginBase {
|
||||
|
||||
/**
|
||||
* Provides a basic initialisation function that does nothing.
|
||||
*
|
||||
*/
|
||||
public static function init() {
|
||||
// Nothing to do
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of this plugin
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function name() {
|
||||
return static::PLUGIN_NAME;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -1,67 +0,0 @@
|
||||
<?php
|
||||
|
||||
abstract class RippingCluster_PluginFactory implements RippingCluster_IPluginFactory {
|
||||
|
||||
static private $validPlugins = array();
|
||||
|
||||
protected static function ensureScanned() {
|
||||
if (! isset(self::$validPlugins[get_called_class()])) {
|
||||
static::scan();
|
||||
}
|
||||
}
|
||||
|
||||
protected static function isValidPlugin($plugin) {
|
||||
return isset(self::$validPlugins[get_called_class()][$plugin]);
|
||||
}
|
||||
|
||||
public static function getValidPlugins() {
|
||||
static::ensureScanned();
|
||||
return array_keys(self::$validPlugins[get_called_class()]);
|
||||
}
|
||||
|
||||
protected static function findPlugins($directory) {
|
||||
$plugins = array();
|
||||
|
||||
$iterator = new RippingCluster_Utility_ClassFilesIterator(new RippingCluster_Utility_VisibleFilesIterator(new DirectoryIterator(RippingCluster_Lib . $directory)));
|
||||
|
||||
foreach ($iterator as /** @var SplFileInfo */ $file) {
|
||||
$plugin = preg_replace('/.class.php$/', '', $file->getFilename());
|
||||
$plugins[] = $plugin;
|
||||
}
|
||||
|
||||
return $plugins;
|
||||
}
|
||||
|
||||
protected static function loadPlugins($plugins, $prefix, $interface) {
|
||||
self::$validPlugins[get_called_class()] = array();
|
||||
|
||||
foreach ($plugins as $plugin) {
|
||||
$fullClassname = $prefix . $plugin;
|
||||
if ( ! class_exists($fullClassname, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( ! in_array($interface, class_implements($fullClassname))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Initialise the plugin
|
||||
call_user_func(array($fullClassname, 'init'));
|
||||
|
||||
self::$validPlugins[get_called_class()][$plugin] = $fullClassname;
|
||||
}
|
||||
}
|
||||
|
||||
public static function classname($plugin) {
|
||||
static::ensureScanned();
|
||||
|
||||
if ( ! self::isValidPlugin($plugin)) {
|
||||
throw new RippingCluster_Exception_InvalidPluginName($plugin);
|
||||
}
|
||||
|
||||
return self::$validPlugins[get_called_class()][$plugin];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -1,97 +0,0 @@
|
||||
<?php
|
||||
|
||||
class RippingCluster_RequestParser {
|
||||
|
||||
private $request_string;
|
||||
private $page = array();
|
||||
private $vars = array();
|
||||
|
||||
public function __construct($request_string) {
|
||||
$this->request_string = $request_string;
|
||||
|
||||
$this->parse();
|
||||
}
|
||||
|
||||
public function parse() {
|
||||
if (!$this->request_string) {
|
||||
$this->page = array('home');
|
||||
return;
|
||||
}
|
||||
|
||||
$components = explode('/', $this->request_string);
|
||||
if (!$components) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Read through the components list looking for elements matching known directories and files
|
||||
// to determine which page this request is for
|
||||
$base_dir = 'templates';
|
||||
while (true) {
|
||||
if ($components && ! $components[0]) {
|
||||
// Skip over any empty components before we find a page
|
||||
array_shift($components);
|
||||
}
|
||||
|
||||
if ($components && is_dir($base_dir . '/' . $components[0])) {
|
||||
$base_dir .= '/' . $components[0];
|
||||
array_push($this->page, array_shift($components));
|
||||
} elseif ($components && is_file($base_dir . '/' . $components[0] . '.tpl')) {
|
||||
// We have found a valid page, so break the loop here,
|
||||
// leaving the remaining components as key/value pairs
|
||||
array_push($this->page, array_shift($components));
|
||||
break;
|
||||
} else {
|
||||
// See if we've already seen a component and assumed it referred to a dir when a file of the same name exists
|
||||
if ($this->page && is_file($base_dir . '.tpl')) {
|
||||
break;
|
||||
} elseif ( ! $components && is_file($base_dir . '/index.tpl')) {
|
||||
// The last component in the path was a valid directory, and a directory index exists
|
||||
array_push($this->page, 'index');
|
||||
break;
|
||||
} else {
|
||||
// No valid page was found, so display an error page
|
||||
$this->page = array('404');
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The subsequent tokens are parameters for this page in key value pairs
|
||||
while ($components) {
|
||||
// If the url ended with a trailing slash, the last element will be null
|
||||
$last_element = $components[count($components) - 1];
|
||||
if ($last_element == "") {
|
||||
array_pop($components);
|
||||
}
|
||||
|
||||
$this->vars[array_shift($components)] = $components ? array_shift($components) : true;
|
||||
}
|
||||
}
|
||||
|
||||
public function page() {
|
||||
return join('/', $this->page);
|
||||
}
|
||||
|
||||
public function exists($key) {
|
||||
return isset($this->vars[$key]);
|
||||
}
|
||||
|
||||
public function get($key, $default = null) {
|
||||
if (isset($this->vars[$key])) {
|
||||
return $this->vars[$key];
|
||||
}
|
||||
|
||||
if (is_string($default) && preg_match('/^RippingCluster_Exception/', $default) && class_exists($default) && is_subclass_of($default, RippingCluster_Exception)) {
|
||||
throw new $default();
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
public function request_string() {
|
||||
return $this->request_string;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
?>
|
||||
@@ -1,45 +0,0 @@
|
||||
<?php
|
||||
|
||||
class RippingCluster_Worker {
|
||||
|
||||
protected $gearman;
|
||||
|
||||
public function __construct() {
|
||||
$this->init();
|
||||
|
||||
}
|
||||
|
||||
private function init() {
|
||||
if ($this->gearman) {
|
||||
return;
|
||||
}
|
||||
|
||||
$config = RippingCluster_Main::instance()->config();
|
||||
|
||||
$this->gearman = new GearmanWorker();
|
||||
$this->gearman->addServers($config->get('rips.job_servers'));
|
||||
|
||||
// Load all the plugin classes
|
||||
RippingCluster_Worker_PluginFactory::scan();
|
||||
foreach (RippingCluster_Worker_PluginFactory::getValidPlugins() as $plugin) {
|
||||
$workerFunctions = RippingCluster_Worker_PluginFactory::getPluginWorkerFunctions($plugin);
|
||||
|
||||
foreach ($workerFunctions as $function => $callback) {
|
||||
$this->gearman->addFunction($function, $callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function start() {
|
||||
while($this->gearman->work()) {
|
||||
if ($this->gearman->returnCode() != GEARMAN_SUCCESS) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -1,21 +0,0 @@
|
||||
<?php
|
||||
|
||||
interface RippingCluster_Worker_IPlugin extends RippingCluster_IPlugin {
|
||||
|
||||
/**
|
||||
* Returns the list of functions (and names) implemented by this plugin for registration with Gearman
|
||||
*
|
||||
* @return array(string => callback)
|
||||
*/
|
||||
public static function workerFunctions();
|
||||
|
||||
/**
|
||||
* Creates an instance of the Worker plugin, and uses it to execute a single job
|
||||
*
|
||||
* @param GearmanJob $job Gearman Job object, describing the work to be done
|
||||
*/
|
||||
public static function rip(GearmanJob $job);
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -1,13 +0,0 @@
|
||||
<?php
|
||||
|
||||
class RippingCluster_WorkerLogEntry extends RippingCluster_LogEntry {
|
||||
|
||||
public static function initialise() {
|
||||
parent::$table_name = 'worker_log';
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
RippingCluster_WorkerLogEntry::initialise();
|
||||
|
||||
?>
|
||||
2
private/.gitignore
vendored
Normal file
2
private/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
config.php
|
||||
dbconfig.conf
|
||||
@@ -1,75 +1,51 @@
|
||||
<?php
|
||||
|
||||
class RippingCluster_Worker_Plugin_HandBrake extends RippingCluster_PluginBase implements RippingCluster_Worker_IPlugin {
|
||||
class Net_Gearman_Job_HandBrake extends Net_Gearman_Job_Common implements RippingCluster_Worker_IPlugin {
|
||||
|
||||
const PLUGIN_NAME = 'HandBrake';
|
||||
|
||||
const DEINTERLACE_ALWAYS = 1;
|
||||
const DEINTERLACE_SELECTIVELY = 2;
|
||||
|
||||
private $output;
|
||||
|
||||
private $gearman_job;
|
||||
|
||||
private $job;
|
||||
|
||||
private $rip_options;
|
||||
|
||||
private function __construct(GearmanJob $gearman_job) {
|
||||
public function __construct($conn, $handle) {
|
||||
parent::__construct($conn, $handle);
|
||||
|
||||
$this->output = '';
|
||||
|
||||
$this->gearman_job = $gearman_job;
|
||||
|
||||
$this->rip_options = unserialize($this->gearman_job->workload());
|
||||
|
||||
if ( ! $this->rip_options['id']) {
|
||||
throw new RippingCluster_Exception_LogicException("Job ID must not be zero/null");
|
||||
}
|
||||
$this->job = RippingCluster_Job::fromId($this->rip_options['id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of functions (and names) implemented by this plugin for registration with Gearman
|
||||
*
|
||||
* @return array(string => callback)
|
||||
*/
|
||||
public static function workerFunctions() {
|
||||
return array(
|
||||
'handbrake_rip' => array(__CLASS__, 'rip'),
|
||||
);
|
||||
public static function init() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance of the Worker plugin, and uses it to execute a single job
|
||||
*
|
||||
* @param GearmanJob $job Gearman Job object, describing the work to be done
|
||||
*/
|
||||
public static function rip(GearmanJob $job) {
|
||||
$rip = new self($job);
|
||||
$rip->execute();
|
||||
}
|
||||
public static function name() {
|
||||
|
||||
private function execute() {
|
||||
}
|
||||
|
||||
public function run($args) {;
|
||||
$main = RippingCluster_Main::instance();
|
||||
$config = $main->config();
|
||||
$log = $main->log();
|
||||
|
||||
$this->job = RippingCluster_Job::fromId($args['rip_options']['id']);
|
||||
|
||||
$handbrake_cmd_raw = array(
|
||||
'-n', $config->get('rips.nice'),
|
||||
$config->get('rips.handbrake_binary'),
|
||||
$this->evaluateOption('input_filename', '-i'),
|
||||
$this->evaluateOption('output_filename', '-o'),
|
||||
$this->evaluateOption('title'),
|
||||
$this->evaluateOption('format', '-f'),
|
||||
$this->evaluateOption('video_codec', '-e'),
|
||||
$this->evaluateOption('quantizer', '-q'),
|
||||
$this->evaluateOption('video_width', '-w'),
|
||||
$this->evaluateOption('video_height', '-l'),
|
||||
$this->evaluateOption('deinterlace'),
|
||||
$this->evaluateOption('audio_tracks', '-a'),
|
||||
$this->evaluateOption('audio_codec', '-E'),
|
||||
$this->evaluateOption('audio_names', '-A'),
|
||||
$this->evaluateOption('subtitle_tracks', '-s'),
|
||||
self::evaluateOption($args['rip_options'], 'input_filename', '-i'),
|
||||
self::evaluateOption($args['rip_options'], 'output_filename', '-o'),
|
||||
self::evaluateOption($args['rip_options'], 'title'),
|
||||
self::evaluateOption($args['rip_options'], 'format', '-f'),
|
||||
self::evaluateOption($args['rip_options'], 'video_codec', '-e'),
|
||||
self::evaluateOption($args['rip_options'], 'quantizer', '-q'),
|
||||
self::evaluateOption($args['rip_options'], 'video_width', '-w'),
|
||||
self::evaluateOption($args['rip_options'], 'video_height', '-l'),
|
||||
self::evaluateOption($args['rip_options'], 'deinterlace'),
|
||||
self::evaluateOption($args['rip_options'], 'audio_tracks', '-a'),
|
||||
self::evaluateOption($args['rip_options'], 'audio_codec', '-E'),
|
||||
self::evaluateOption($args['rip_options'], 'audio_names', '-A'),
|
||||
self::evaluateOption($args['rip_options'], 'subtitle_tracks', '-s'),
|
||||
);
|
||||
|
||||
$handbrake_cmd = array($config->get('rips.nice_binary'));
|
||||
@@ -85,24 +61,25 @@ class RippingCluster_Worker_Plugin_HandBrake extends RippingCluster_PluginBase i
|
||||
|
||||
list($return_val, $stdout, $stderr) = RippingCluster_ForegroundTask::execute($handbrake_cmd, null, null, null, array($this, 'callbackOutput'), array($this, 'callbackOutput'), $this);
|
||||
if ($return_val) {
|
||||
$this->gearman_job->sendFail($return_val);
|
||||
$this->fail($return_val);
|
||||
} else {
|
||||
$this->job->updateStatus(RippingCluster_JobStatus::COMPLETE);
|
||||
$this->complete();
|
||||
}
|
||||
}
|
||||
|
||||
private function evaluateOption($name, $option = null) {
|
||||
private static function evaluateOption($options, $name, $option = null) {
|
||||
switch($name) {
|
||||
case 'title': {
|
||||
if (!$this->rip_options[$name] || (int)$this->rip_options[$name] < 0) {
|
||||
if (!$options[$name] || (int)$options[$name] < 0) {
|
||||
return array('-L');
|
||||
} else {
|
||||
return array('-t', $this->rip_options[$name]);
|
||||
return array('-t', $options[$name]);
|
||||
}
|
||||
} break;
|
||||
|
||||
case 'deinterlace': {
|
||||
switch ($this->rip_options[$name]) {
|
||||
switch ($options[$name]) {
|
||||
case self::DEINTERLACE_ALWAYS:
|
||||
return array('-d');
|
||||
case self::DEINTERLACE_SELECTIVELY:
|
||||
@@ -113,7 +90,7 @@ class RippingCluster_Worker_Plugin_HandBrake extends RippingCluster_PluginBase i
|
||||
}
|
||||
|
||||
default:
|
||||
return array(isset($option) ? $option : $name, $this->rip_options[$name]);
|
||||
return array(isset($option) ? $option : $name, $options[$name]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,6 +105,7 @@ class RippingCluster_Worker_Plugin_HandBrake extends RippingCluster_PluginBase i
|
||||
if (preg_match('/Encoding: task \d+ of \d+, (\d+\.\d+) %/', $line, $matches)) {
|
||||
$status = $rip->job->currentStatus();
|
||||
$status->updateRipProgress($matches[1]);
|
||||
$this->status($matches[1], 100);
|
||||
} else {
|
||||
$log = RippingCluster_Main::instance()->log();
|
||||
$log->debug($line, $rip->job->id());
|
||||
38
source/lib/RippingCluster/ClientLogEntry.class.php
Normal file
38
source/lib/RippingCluster/ClientLogEntry.class.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
class RippingCluster_ClientLogEntry extends RippingCluster_LogEntry {
|
||||
|
||||
protected $jobId;
|
||||
|
||||
protected function __construct($id, $level, $ctime, $pid, $hostname, $progname, $line, $message, $jobId) {
|
||||
parent::__construct($id, $level, $ctime, $pid, $hostname, $progname, $line, $message);
|
||||
|
||||
$this->jobId = $jobId;
|
||||
}
|
||||
|
||||
public static function fromDatabaseRow($row) {
|
||||
return new self(
|
||||
$row['id'],
|
||||
$row['level'],
|
||||
$row['ctime'],
|
||||
$row['pid'],
|
||||
$row['hostname'],
|
||||
$row['progname'],
|
||||
$row['line'],
|
||||
$row['message'],
|
||||
$row['job_id']
|
||||
);
|
||||
}
|
||||
|
||||
public static function initialise() {
|
||||
parent::$table_name = 'client_log';
|
||||
}
|
||||
|
||||
public function jobId() {
|
||||
return $this->jobId;
|
||||
}
|
||||
};
|
||||
|
||||
RippingCluster_ClientLogEntry::initialise();
|
||||
|
||||
?>
|
||||
@@ -219,11 +219,9 @@ class RippingCluster_Job {
|
||||
$this->id = null;
|
||||
}
|
||||
|
||||
public function queue($gearman) {
|
||||
public function queue() {
|
||||
$main = RippingCluster_Main::instance();
|
||||
$config = $main->config();
|
||||
$log = $main->log();
|
||||
$log->info('Starting job', $this->id);
|
||||
|
||||
// Construct the rip options
|
||||
$rip_options = array(
|
||||
@@ -244,16 +242,7 @@ class RippingCluster_Job {
|
||||
'subtitle_tracks' => $this->subtitle_tracks,
|
||||
);
|
||||
|
||||
// Enqueue this rip
|
||||
if ( ! $this->id) {
|
||||
throw new RippingCluster_Exception_LogicException("Rip cannot be queued without being saved!");
|
||||
}
|
||||
$task = $gearman->addTask('handbrake_rip', serialize($rip_options), $config->get('rips.context'), $this->id);
|
||||
if ($task) {
|
||||
$this->updateStatus(RippingCluster_JobStatus::QUEUED);
|
||||
} else {
|
||||
$this->updateStatus(RippingCluster_JobStatus::FAILED);
|
||||
}
|
||||
return array('HandBrake', array('rip_options' => $rip_options));
|
||||
}
|
||||
|
||||
protected function loadStatuses() {
|
||||
50
source/lib/RippingCluster/Main.class.php
Normal file
50
source/lib/RippingCluster/Main.class.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
require 'smarty/Smarty.class.php';
|
||||
|
||||
class RippingCluster_Main extends SihnonFramework_Main {
|
||||
|
||||
protected static $instance;
|
||||
|
||||
protected $smarty;
|
||||
protected $request;
|
||||
|
||||
protected function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
$request_string = isset($_GET['l']) ? $_GET['l'] : '';
|
||||
|
||||
$this->request = new RippingCluster_RequestParser($request_string);
|
||||
|
||||
if (HBC_File == 'index') {
|
||||
$this->smarty = new Smarty();
|
||||
$this->smarty->template_dir = './source/templates';
|
||||
$this->smarty->compile_dir = './tmp/templates';
|
||||
$this->smarty->cache_dir = './tmp/cache';
|
||||
$this->smarty->config_dir = './config';
|
||||
|
||||
$this->smarty->registerPlugin('modifier', 'formatDuration', array('RippingCluster_Main', 'formatDuration'));
|
||||
|
||||
$this->smarty->assign('version', '0.1');
|
||||
$this->smarty->assign('messages', array());
|
||||
|
||||
$this->smarty->assign('base_uri', $this->base_uri);
|
||||
}
|
||||
}
|
||||
|
||||
public function smarty() {
|
||||
return $this->smarty;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return RippingCluster_RequestParser
|
||||
*/
|
||||
public function request() {
|
||||
return $this->request;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -25,7 +25,7 @@ class RippingCluster_Rips_SourceAudioTrack {
|
||||
}
|
||||
|
||||
public function name() {
|
||||
return $name;
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setName($name) {
|
||||
@@ -2,20 +2,17 @@
|
||||
|
||||
class RippingCluster_Source_PluginFactory extends RippingCluster_PluginFactory {
|
||||
|
||||
const PLUGIN_DIR = 'RippingCluster/Source/Plugin/';
|
||||
const PLUGIN_PREFIX = 'RippingCluster_Source_Plugin_';
|
||||
const PLUGIN_INTERFACE = 'RippingCluster_Source_IPlugin';
|
||||
protected static $plugin_prefix = 'RippingCluster_Source_Plugin_';
|
||||
protected static $plugin_interface = 'RippingCluster_Source_IPlugin';
|
||||
protected static $plugin_dir = array(
|
||||
RippingCluster_Lib => 'RippingCluster/Source/Plugin/',
|
||||
);
|
||||
|
||||
|
||||
public static function init() {
|
||||
|
||||
}
|
||||
|
||||
public static function scan() {
|
||||
$candidatePlugins = parent::findPlugins(self::PLUGIN_DIR);
|
||||
|
||||
self::loadPlugins($candidatePlugins, self::PLUGIN_PREFIX, self::PLUGIN_INTERFACE);
|
||||
}
|
||||
|
||||
public static function enumerate($plugin) {
|
||||
self::ensureScanned();
|
||||
|
||||
@@ -57,7 +54,7 @@ class RippingCluster_Source_PluginFactory extends RippingCluster_PluginFactory {
|
||||
return call_user_func(array(self::classname($plugin), 'loadEncoded'), $encoded_filename, $scan, $use_cache);
|
||||
}
|
||||
|
||||
public static function isValidSource($plugin, $source_filename) {
|
||||
/*public static function isValidSource($plugin, $source_filename) {
|
||||
self::ensureScanned();
|
||||
|
||||
if ( ! self::isValidPlugin($plugin)) {
|
||||
@@ -65,7 +62,7 @@ class RippingCluster_Source_PluginFactory extends RippingCluster_PluginFactory {
|
||||
}
|
||||
|
||||
return call_user_func(array(self::classname($plugin), 'isValidSource'), source_filename);
|
||||
}
|
||||
}*/
|
||||
|
||||
/**
|
||||
* Permanently deletes the given source from disk
|
||||
47
source/lib/RippingCluster/Worker.class.php
Normal file
47
source/lib/RippingCluster/Worker.class.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
class RippingCluster_Worker {
|
||||
|
||||
protected $gearman;
|
||||
|
||||
public function __construct() {
|
||||
$this->init();
|
||||
|
||||
}
|
||||
|
||||
private function init() {
|
||||
if ($this->gearman) {
|
||||
return;
|
||||
}
|
||||
|
||||
$config = RippingCluster_Main::instance()->config();
|
||||
|
||||
$this->gearman = new Net_Gearman_Worker('river.sihnon.net:4730');//$config->get('rips.job_servers'));
|
||||
|
||||
// Load all the plugin classes
|
||||
RippingCluster_Worker_PluginFactory::scan();
|
||||
$plugins = RippingCluster_Worker_PluginFactory::getValidPlugins();
|
||||
foreach ($plugins as $plugin) {
|
||||
$this->gearman->addAbility($plugin);
|
||||
|
||||
//$workerFunctions = RippingCluster_Worker_PluginFactory::getPluginWorkerFunctions($plugin);
|
||||
//foreach ($workerFunctions as $function => $callback) {
|
||||
// echo "Added ability $function\n";
|
||||
// $this->gearman->addAbility($function);
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
public function start() {
|
||||
try {
|
||||
$this->gearman->beginWork();
|
||||
} catch (Net_Gearman_Exception $e) {
|
||||
// Do stuff
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
16
source/lib/RippingCluster/Worker/IPlugin.class.php
Normal file
16
source/lib/RippingCluster/Worker/IPlugin.class.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
interface RippingCluster_Worker_IPlugin extends RippingCluster_IPlugin {
|
||||
|
||||
/**
|
||||
* Returns the list of functions (and names) implemented by this plugin for registration with Gearman
|
||||
*
|
||||
* @return array(string => callback)
|
||||
*/
|
||||
//public static function workerFunctions();
|
||||
|
||||
//public static function run($args);
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -51,6 +51,11 @@ class RippingCluster_Worker_Bluray extends RippingCluster_PluginBase implements
|
||||
$this->job = RippingCluster_Job::fromId($this->rip_options['id']);
|
||||
}
|
||||
|
||||
public static function init() {
|
||||
// Nothing to do
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the list of functions (and names) implemented by this plugin for registration with Gearman
|
||||
*
|
||||
@@ -62,14 +67,9 @@ class RippingCluster_Worker_Bluray extends RippingCluster_PluginBase implements
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance of the Worker plugin, and uses it to execute a single job
|
||||
*
|
||||
* @param GearmanJob $job Gearman Job object, describing the work to be done
|
||||
*/
|
||||
public static function rip(GearmanJob $job) {
|
||||
$rip = new self($job);
|
||||
$rip->execute();
|
||||
public static function run($args) {
|
||||
//$rip = new self($job);
|
||||
//$rip->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,14 +62,9 @@ class RippingCluster_Worker_FfmpegTranscode extends RippingCluster_PluginBase im
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance of the Worker plugin, and uses it to execute a single job
|
||||
*
|
||||
* @param GearmanJob $job Gearman Job object, describing the work to be done
|
||||
*/
|
||||
public static function rip(GearmanJob $job) {
|
||||
$rip = new self($job);
|
||||
$rip->execute();
|
||||
public static function run($args) {
|
||||
//$rip = new self($job);
|
||||
//$rip->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
class RippingCluster_Worker_PluginFactory extends RippingCluster_PluginFactory {
|
||||
|
||||
const PLUGIN_DIR = 'RippingCluster/Worker/Plugin/';
|
||||
const PLUGIN_PREFIX = 'RippingCluster_Worker_Plugin_';
|
||||
const PLUGIN_DIR = 'Net/Gearman/Job/';
|
||||
const PLUGIN_PREFIX = 'Net_Gearman_Job_';
|
||||
const PLUGIN_INTERFACE = 'RippingCluster_Worker_IPlugin';
|
||||
|
||||
public static function init() {
|
||||
39
source/lib/RippingCluster/WorkerLogEntry.class.php
Normal file
39
source/lib/RippingCluster/WorkerLogEntry.class.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
class RippingCluster_WorkerLogEntry extends RippingCluster_LogEntry {
|
||||
|
||||
protected $jobId;
|
||||
|
||||
protected function __construct($id, $level, $ctime, $pid, $hostname, $progname, $line, $message, $jobId) {
|
||||
parent::__construct($id, $level, $ctime, $pid, $hostname, $progname, $line, $message);
|
||||
|
||||
$this->jobId = $jobId;
|
||||
}
|
||||
|
||||
public static function fromDatabaseRow($row) {
|
||||
return new self(
|
||||
$row['id'],
|
||||
$row['level'],
|
||||
$row['ctime'],
|
||||
$row['pid'],
|
||||
$row['hostname'],
|
||||
$row['progname'],
|
||||
$row['line'],
|
||||
$row['message'],
|
||||
$row['job_id']
|
||||
);
|
||||
}
|
||||
|
||||
public static function initialise() {
|
||||
parent::$table_name = 'worker_log';
|
||||
}
|
||||
|
||||
public function jobId() {
|
||||
return $this->jobId;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
RippingCluster_WorkerLogEntry::initialise();
|
||||
|
||||
?>
|
||||
@@ -1,6 +1,10 @@
|
||||
<?php
|
||||
|
||||
require_once '../config.php';
|
||||
require_once RippingCluster_Lib . 'RippingCluster/Main.class.php';
|
||||
require_once '../private/config.php';
|
||||
require_once(SihnonFramework_Lib . 'SihnonFramework/Main.class.php');
|
||||
//require_once RippingCluster_Lib . 'RippingCluster/Main.class.php';
|
||||
|
||||
SihnonFramework_Main::registerAutoloadClasses('Sihnon', SihnonFramework_Lib,
|
||||
'RippingCluster', SihnonFramework_Main::makeAbsolutePath('../source/lib/'));
|
||||
|
||||
?>
|
||||
|
||||
@@ -2,36 +2,38 @@
|
||||
|
||||
define('HBC_File', 'run-jobs');
|
||||
|
||||
require_once '../config.php';
|
||||
require_once RippingCluster_Lib . 'RippingCluster/Main.class.php';
|
||||
require_once '../private/config.php';
|
||||
require_once(SihnonFramework_Lib . 'SihnonFramework/Main.class.php');
|
||||
require_once 'Net/Gearman/Client.php';
|
||||
|
||||
SihnonFramework_Main::registerAutoloadClasses('Sihnon', SihnonFramework_Lib,
|
||||
'RippingCluster', SihnonFramework_Main::makeAbsolutePath('../lib/'));
|
||||
|
||||
try {
|
||||
$main = RippingCluster_Main::instance();
|
||||
$config = $main->config();
|
||||
$log = $main->log();
|
||||
|
||||
$gearman = new GearmanClient();
|
||||
$gearman->addServers($config->get('rips.job_servers'));
|
||||
$gearman->setCreatedCallback("gearman_created_callback");
|
||||
$gearman->setDataCallback("gearman_data_callback");
|
||||
$gearman->setCompleteCallback("gearman_complete_callback");
|
||||
$gearman->setFailCallback("gearman_fail_callback");
|
||||
$client = new Net_Gearman_Client('river.sihnon.net:4730');//$config->get('rips.job_servers'));
|
||||
$set = new Net_Gearman_Set();
|
||||
|
||||
// Retrieve a list of Created jobs
|
||||
$jobs = RippingCluster_Job::allWithStatus(RippingCluster_JobStatus::CREATED);
|
||||
|
||||
foreach ($jobs as $job) {
|
||||
// Enqueue the job using gearman
|
||||
$job->queue($gearman);
|
||||
list($method, $rip_options) = $job->queue();
|
||||
$task = new Net_Gearman_Task($method, $rip_options);
|
||||
$task->attachCallback('gearman_complete', Net_Gearman_Task::TASK_COMPLETE);
|
||||
$task->attachCallback('gearman_fail', Net_Gearman_Task::TASK_FAIL);
|
||||
$set->addTask($task);
|
||||
|
||||
$job->updateStatus(RippingCluster_JobStatus::QUEUED);
|
||||
}
|
||||
|
||||
// Start the job queue
|
||||
$result = $gearman->runTasks();
|
||||
if (!$result) {
|
||||
$log->error($gearman->error());
|
||||
die($gearman->error());
|
||||
}
|
||||
|
||||
$result = $client->runSet($set);
|
||||
|
||||
$log->info("Job queue completed");
|
||||
|
||||
} catch (RippingCluster_Exception $e) {
|
||||
@@ -39,35 +41,23 @@ try {
|
||||
}
|
||||
|
||||
|
||||
function gearman_created_callback($gearman_task) {
|
||||
$main = RippingCluster_Main::instance();
|
||||
$log = $main->log();
|
||||
|
||||
$log->info("Job successfully queued with Gearman", $gearman_task->unique());
|
||||
}
|
||||
|
||||
function gearman_data_callback($gearman_task) {
|
||||
function gearman_complete($method, $handle, $result) {
|
||||
$main = RippingCluster_Main::instance();
|
||||
$log = $main->log();
|
||||
|
||||
$log->debug("Received data callback from Gearman Task");
|
||||
/*$log->info("Job Complete", $job->id());*/
|
||||
$log->info("Job complete");
|
||||
}
|
||||
|
||||
function gearman_complete_callback($gearman_task) {
|
||||
function gearman_fail($task) {
|
||||
$main = RippingCluster_Main::instance();
|
||||
$log = $main->log();
|
||||
|
||||
$log->info("Job Complete", $job->id());
|
||||
}
|
||||
|
||||
function gearman_fail_callback($gearman_task) {
|
||||
$main = RippingCluster_Main::instance();
|
||||
$log = $main->log();
|
||||
|
||||
$job = RippingCluster_Job::fromId($gearman_task->unique());
|
||||
/*$job = RippingCluster_Job::fromId($gearman_task->unique());
|
||||
$job->updateStatus(RippingCluster_JobStatus::FAILED);
|
||||
|
||||
$log->info("Job Failed", $job->id());
|
||||
$log->info("Job Failed", $job->id());*/
|
||||
$log->info("Job failed");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -19,13 +19,13 @@
|
||||
</div>
|
||||
|
||||
<div id="navigation">
|
||||
{include file=navigation.tpl}
|
||||
{include file="navigation.tpl"}
|
||||
</div>
|
||||
|
||||
<div id="page-container">
|
||||
|
||||
<div id="sidebar">
|
||||
{include file=sidebar.tpl}
|
||||
{include file="sidebar.tpl"}
|
||||
</div>
|
||||
|
||||
<div id="page">
|
||||
@@ -17,7 +17,7 @@
|
||||
{assign var='source_plugin' value=$source->plugin()}
|
||||
{assign var='source_filename' value=$source->filename()}
|
||||
{assign var='source_filename_encoded' value=$source->filenameEncoded()}
|
||||
{assign var='source_cached' value="$source->isCached()}
|
||||
{assign var='source_cached' value=$source->isCached()}
|
||||
<li>
|
||||
[ <a href="{$base_uri}rips/source-details/plugin/{$source_plugin}/id/{$source_filename_encoded}" title="Browse source details">Browse</a> |
|
||||
<a href="{$base_uri}rips/setup-rip/plugin/{$source_plugin}/id/{$source_filename_encoded}" title="Rip this source">Rip</a> |
|
||||
@@ -2,8 +2,14 @@
|
||||
|
||||
define('HBC_File', 'worker');
|
||||
|
||||
require_once '../config.php';
|
||||
require_once RippingCluster_Lib . 'RippingCluster/Main.class.php';
|
||||
require_once '../private/config.php';
|
||||
require_once(SihnonFramework_Lib . 'SihnonFramework/Main.class.php');
|
||||
require_once 'Net/Gearman/Worker.php';
|
||||
|
||||
SihnonFramework_Main::registerAutoloadClasses('Sihnon', SihnonFramework_Lib,
|
||||
'RippingCluster', SihnonFramework_Main::makeAbsolutePath('../lib/'));
|
||||
SihnonFramework_Main::registerAutoloadClasses('Net', SihnonFramework_Main::makeAbsolutePath('../lib/'));
|
||||
|
||||
|
||||
try {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user