Permalink
Browse files

implemented automatic start of sc_serv/sc_trans

  • Loading branch information...
1 parent 6280216 commit 41c8fd117a7dddfb7b9896c11a953ac36930c001 @peaceman committed Apr 10, 2012
@@ -20,6 +20,7 @@ public function __construct(\Daemon\Config $configuration)
{
$this->_config = $configuration;
$this->_initStdio();
+ $this->_init();
}
protected function _initStdio()
@@ -44,8 +45,6 @@ protected function _initStdio()
$STDIN = fopen('/dev/null', 'r');
$STDOUT = fopen($stdoutFile, 'ab');
$STDERR = fopen($stdoutFile, 'ab');
-
- $this->_init();
}
/**
@@ -15,6 +15,19 @@ class ForkMaster extends AbstractProcess
protected function _init()
{
+ $pid = pcntl_fork();
+ if ($pid !== 0) {
+ return;
+ }
+
+ $pidFile = $this->_config->get('pid_file');
+ if (file_exists($pidFile) && '' != exec('ps -p `cat ' . $pidFile . '` --no-heading')) {
+ trigger_error('Process running with PID ' . file_get_contents($pidFile), E_USER_NOTICE);
+ exit(0);
+ }
+
+ file_put_contents($pidFile, getmypid());
+
setproctitle('Daemon: ForkMaster');
$this->_forkChilds();
$this->_monitorChildProcesses();
@@ -0,0 +1,87 @@
+<?php
+/**
+ * User: peaceman
+ * Date: 4/10/12
+ * Time: 10:44 PM
+ */
+namespace SAP\Daemon\Task\SCv1;
+use Daemon\Task,
+ Daemon\Message;
+
+class InitialStart extends Task\AbstractTask
+{
+ protected function _run()
+ {
+ $this->_startServersWithAvailableConfiguration();
+ $this->_startTranscodersWithAvailableConfiguration();
+ }
+
+ protected function _startServersWithAvailableConfiguration()
+ {
+ $configurationFiles = $this->_getServerConfigurationFiles();
+ foreach ($configurationFiles as $configurationFile) {
+ $serverIdentifier = $this->_getServerIdentifierFromServerConfigurationFilename($configurationFile);
+ $startServerMessage = new Message\Task\Add(array(
+ 'task' => new \SAP\Daemon\Task\SCv1\Server\Start(array(
+ 'server_identifier' => $serverIdentifier,
+ 'server_config_file' => $configurationFile,
+ )),
+ ));
+
+ $this->_messagesToQueueManager[] = $startServerMessage;
+ }
+ }
+
+ protected function _getServerConfigurationFiles()
+ {
+ $configurationPath = $this->_getConfigurationPath();
+ if (!is_dir($configurationPath)) {
+ throw new \RuntimeException(sprintf('%s is no directory', $configurationPath));
+ }
+
+ return glob($configurationPath . '/sc_serv-*.ini');
+ }
+
+ protected function _getConfigurationPath()
+ {
+ return realpath(APPLICATION_PATH . '/configs/streams');
+ }
+
+ protected function _getServerIdentifierFromServerConfigurationFilename($configurationFilename)
+ {
+ $filename = pathinfo($configurationFilename, PATHINFO_FILENAME);
+ return str_replace('.ini', '', $filename);
+ }
+
+ protected function _startTranscodersWithAvailableConfiguration()
+ {
+ $configurationFiles = $this->_getTranscoderConfigurationFiles();
+ foreach ($configurationFiles as $configurationFile) {
+ $transcoderIdentifier = $this->_getTranscoderIdentifierFromServerConfigurationFilename($configurationFile);
+ $startTranscoderMessage = new Message\Task\Add(array(
+ 'task' => new \SAP\Daemon\Task\SCv1\Transcoder\Start(array(
+ 'transcoder_identifier' => $transcoderIdentifier,
+ 'transcoder_config_file' => $configurationFile,
+ )),
+ ));
+
+ $this->_messagesToQueueManager[] = $startTranscoderMessage;
+ }
+ }
+
+ protected function _getTranscoderConfigurationFiles()
+ {
+ $configurationPath = $this->_getConfigurationPath();
+ if (!is_dir($configurationPath)) {
+ throw new \RuntimeException(sprintf('%s is no directory', $configurationPath));
+ }
+
+ return glob($configurationPath . '/sc_trans-*.ini');
+ }
+
+ protected function _getTranscoderIdentifierFromServerConfigurationFilename($configurationFilename)
+ {
+ $filename = pathinfo($configurationFilename, PATHINFO_FILENAME);
+ return str_replace('.ini', '', $filename);
+ }
+}
@@ -17,10 +17,20 @@ protected function _init()
throw new \InvalidArgumentException('no or invalid server_identifier given');
}
- if (!isset($this->_data['server_config']) || !is_array($this->_data['server_config'])) {
+ if (!isset($this->_data['server_config_file'])
+ && (!isset($this->_data['server_config'])
+ || !is_array($this->_data['server_config'])))
+ {
$this->_maxTries = 0;
throw new \InvalidArgumentException('no or invalid server_config given');
}
+
+ if (isset($this->_data['server_config_file'])
+ && !file_exists($this->_data['server_config_file']))
+ {
+ $this->_maxTries = 0;
+ throw new \InvalidArgumentException(sprintf('server configuration file %s doesnt exist', $this->_data['server_config_file']));
+ }
}
protected function _run()
@@ -34,7 +44,10 @@ protected function _run()
return;
}
- $this->_updateServerConfigurationFile();
+ if (!isset($this->_data['server_config_file'])) {
+ $this->_updateServerConfigurationFile();
+ }
+
$this->_startServerWithConfigurationFile();
}
@@ -16,10 +16,20 @@ protected function _init()
throw new \InvalidArgumentException('no or invalid transcoder_identifier given');
}
- if (!isset($this->_data['transcoder_config']) || !is_array($this->_data['transcoder_config'])) {
+ if (!isset($this->_data['transcoder_config_file'])
+ && (!isset($this->_data['transcoder_config'])
+ || !is_array($this->_data['transcoder_config'])))
+ {
$this->_maxTries = 0;
throw new \InvalidArgumentException('no or invalid transcoder_config given');
}
+
+ if (isset($this->_data['transcoder_config_file'])
+ && !file_exists($this->_data['transcoder_config_file']))
+ {
+ $this->_maxTries = 0;
+ throw new \InvalidArgumentException(sprintf('transcoder configuration file %s doesnt exist', $this->_data['transcoder_config_file']));
+ }
}
protected function _run()
@@ -33,7 +43,10 @@ protected function _run()
return;
}
- $this->_updateTranscoderConfigurationFile();
+ if (!isset($this->_data['transcoder_config_file'])) {
+ $this->_updateTranscoderConfigurationFile();
+ }
+
$this->_startTranscoderWithConfigurationFile();
}
@@ -117,6 +130,7 @@ protected function _startTranscoderWithConfigurationFile()
'result' => $result,
'output' => $output,
));
+ file_put_contents('/tmp/abc', $shellCommand, FILE_APPEND);
}
/**
View
@@ -35,12 +35,17 @@ function el($v) { e($v . PHP_EOL); }
$config = require 'config.php';
$config = new \Daemon\Config($config);
-$pidFile = $config->get('pid_file');
-if (file_exists($pidFile) && '' != exec('ps -p `cat ' . $pidFile . '` --no-heading')) {
- trigger_error('Process running with PID ' . file_get_contents($pidFile), E_USER_NOTICE);
- exit(0);
-}
+new \Daemon\Process\ForkMaster($config);
-file_put_contents($pidFile, getmypid());
+$context = new ZMQContext();
+$socket = $context->getSocket(ZMQ::SOCKET_DEALER);
+$socket->setSockOpt(\ZMQ::SOCKOPT_IDENTITY, uniqid());
+$socket->connect($config->get('sockets.queueManager'));
-new \Daemon\Process\ForkMaster($config);
+$message = new \Daemon\Message\Task\Add(array(
+ 'task' => new \SAP\Daemon\Task\SCv1\InitialStart(),
+));
+
+$zmsg = new \ZMQ\Zmsg($socket);
+$zmsg->body_set(serialize($message));
+$zmsg->send();

0 comments on commit 41c8fd1

Please sign in to comment.