Update worker code to the point that it can run.

Added plugin architecture for Worker functions, to support multiple
ripping engines.
Update worker script to use the new code.
Update Logger to output messages to console when running under the
worker script.
This commit is contained in:
2010-08-22 23:05:33 +01:00
parent 853c380e65
commit 40fa00c034
7 changed files with 260 additions and 67 deletions

View File

@@ -2,84 +2,47 @@
class HandBrakeCluster_Worker {
const DEINTERLACE_ALWAYS = 1;
const DEINTERLACE_SELECTIVELY = 2;
protected $gearman;
public function __construct() {
$gearman = new GearmanWorker();
$gearman->addServers($config->get('rips.job_servers'));
$gearman->addFunction('handbrake_rip', 'hbc_gearman_handbrake_rip');
$this->init();
}
public function start() {
while($gearman->work()) {
if ($gearman->returnCode() != GEARMAN_SUCCESS) {
private function init() {
if ($this->gearman) {
return;
}
$config = HandBrakeCluster_Main::instance()->config();
$this->gearman = new GearmanWorker();
$this->gearman->addServers($config->get('rips.job_servers'));
// Load all the plugin classes
echo "Loading Plugins\n";
HandBrakeCluster_Worker_PluginFactory::scan();
foreach (HandBrakeCluster_Worker_PluginFactory::getValidPlugins() as $plugin) {
echo "Grabbing worker functions provided by {$plugin}\n";
$workerFunctions = HandBrakeCluster_Worker_PluginFactory::getPluginWorkerFunctions($plugin);
foreach ($workerFunctions as $function => $callback) {
echo "Adding {$plugin}::{$callback[1]} as {$function}\n";
$this->gearman->addFunction($function, $callback);
}
}
}
public function start() {
while($this->gearman->work()) {
if ($this->gearman->returnCode() != GEARMAN_SUCCESS) {
break;
}
}
return true;
}
public function handbrakeRip($client_job_id, $rip_options) {
$handbrake_cmd_raw = array(
'-n', $config->get('rips.nice'),
$config->get('rips.handbrake_binary'),
self::evaluateOption($rip_options, 'input_filename', '-i'),
self::evaluateOption($rip_options, 'real_output_filename', '-o'),
self::evaluateOption($rip_options, 'title'),
self::evaluateOption($rip_options, 'format', '-f'),
self::evaluateOption($rip_options, 'video_codec', '-e'),
self::evaluateOption($rip_options, 'quantizer', '-q'),
self::evaluateOption($rip_options, 'video_width', '-w'),
self::evaluateOption($rip_options, 'video_height', '-l'),
self::evaluateOption($rip_options, 'deinterlace'),
self::evaluateOption($rip_options, 'audio_tracks', '-a'),
self::evaluateOption($rip_options, 'audio_codec', '-E'),
self::evaluateOption($rip_options, 'audio_names', '-A'),
self::evaluateOption($rip_options, 'subtitle_tracks', '-s'),
);
$handbrake_cmd = array($config->get('rips.nice_binary'));
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($handbrake_cmd_raw)) as $value) {
$handbrake_cmd[] = shell_escape_arg($value);
}
$handbrake_cmd = array_join(' ', $handbrake_cmd);
return HandBrakeCluster_ForegroundTask::execute($handbrake_cmd, null, null, null, array($this, 'callback_stdout'), array($this, 'callback_stderr'), $this);
}
protected static function evaluateOption(array &$rip_options, $name, $option = null) {
switch($name) {
case 'title': {
if (!$rip_options[$name] || int($rip_options[$name]) < 0) {
return array('-L');
} else {
return array('-t', $rip_options[$name]);
}
} break;
case 'deinterlace': {
switch ($rip_options[$name]) {
case self::DEINTERLACE_ALWAYS:
return array('-d');
case self::DEINTERLACE_SELECTIVELY:
return array('-5');
default:
return array();
}
}
default:
return array(isset($option) ? $option : $name, $rip_options[$name]);
}
}
}
function hbc_gearman_handbrake_rip(GearmanJob $job) {
return $worker->handbrakeRip($job->unique(), unserialize($job->workload));
}
?>