Skip to content

Commit

Permalink
Merge pull request #34 from Martin1982/running-broadcast-environment
Browse files Browse the repository at this point in the history
#32 getRunningBroadcasts doesn't use environment metadata
  • Loading branch information
Martin1982 authored Jul 8, 2016
2 parents 01ced48 + f8b0c4c commit 3773007
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 157 deletions.
80 changes: 44 additions & 36 deletions Broadcaster/AbstractSchedulerCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@
*/
abstract class AbstractSchedulerCommands implements SchedulerCommandsInterface
{
/**
* @var string
*/
protected $environment;
const METADATA_BROADCAST = 'broadcast_id';
const METADATA_CHANNEL = 'channel_id';
const METADATA_ENVIRONMENT = 'env';

/**
* @var array
* @var string
*/
protected $metadata = array();
protected $kernelEnvironment;

/**
* SchedulerCommands constructor.
*
* @param string $environment
* @param string $kernelEnvironment
*/
public function __construct($environment)
public function __construct($kernelEnvironment)
{
$this->environment = $environment;
$this->kernelEnvironment = $kernelEnvironment;
}

/**
Expand All @@ -33,7 +32,7 @@ public function __construct($environment)
public function startProcess($input, $output, $metadata)
{
$meta = '';
$metadata['env'] = $this->getEnvironment();
$metadata['env'] = $this->getKernelEnvironment();

foreach ($metadata as $key => $value) {
$meta .= ' -metadata '.$key.'='.$value;
Expand Down Expand Up @@ -68,43 +67,39 @@ public function getProcessId($processString)
return (int) $pid[0];
}

return;
return 0;
}

/**
* {@inheritdoc}
*/
public function getBroadcastId($processString)
{
$metadataKey = 'broadcast_id';

if (!count($this->metadata)) {
$this->readMetadata($processString);
}

if (array_key_exists($metadataKey, $this->metadata)) {
return $this->metadata[$metadataKey];
}

return;
return $this->getMetadataValue($processString, self::METADATA_BROADCAST);
}

/**
* {@inheritdoc}
*/
public function getChannelId($processString)
{
$metadataKey = 'channel_id';

if (!count($this->metadata)) {
$this->readMetadata($processString);
}
return $this->getMetadataValue($processString, self::METADATA_CHANNEL);
}

if (array_key_exists($metadataKey, $this->metadata)) {
return $this->metadata[$metadataKey];
}
/**
* {@inheritdoc}
*/
public function getEnvironment($processString)
{
return $this->getMetadataValue($processString, self::METADATA_ENVIRONMENT);
}

return;
/**
* {@inheritdoc}
*/
public function getKernelEnvironment()
{
return $this->kernelEnvironment;
}

/**
Expand All @@ -123,24 +118,37 @@ protected function execStreamCommand($input, $output, $meta)
* Read metadata from a process string.
*
* @param $processString
*
* @return array
*/
protected function readMetadata($processString)
{
$metadataRegex = '/-metadata ([a-z_]+)=([\d]+)/';
$processMetadata = array();
$metadataRegex = '/-metadata ([a-z_]+)=([[:alnum:]]+)/';
preg_match_all($metadataRegex, $processString, $metadata);

if (count($metadata) === 3 && is_array($metadata[1]) && is_array($metadata[2])) {
foreach ($metadata[1] as $metadataIndex => $metadataKey) {
$this->metadata[$metadataKey] = $metadata[2][$metadataIndex];
$processMetadata[$metadataKey] = $metadata[2][$metadataIndex];
}
}

return $processMetadata;
}

/**
* @return string
* @param string $processString
* @param string $metadataKey
* @return mixed|void
*/
private function getEnvironment()
private function getMetadataValue($processString, $metadataKey)
{
return $this->environment;
$metadata = $this->readMetadata($processString);

if (array_key_exists($metadataKey, $metadata)) {
return $metadata[$metadataKey];
}

return;
}
}
30 changes: 24 additions & 6 deletions Broadcaster/RunningBroadcast.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,25 @@ class RunningBroadcast
*/
private $channelId;

/**
* @var string
*/
private $environment;

/**
* RunningBroadcast constructor.
*
* @param int $broadcastId
* @param int $processId
* @param int $channelId
* @param int $broadcastId
* @param int $processId
* @param int $channelId
* @param string $environment
*/
public function __construct($broadcastId, $processId, $channelId)
public function __construct($broadcastId, $processId, $channelId, $environment)
{
$this->broadcastId = (int) $broadcastId;
$this->processId = (int) $processId;
$this->channelId = (int) $channelId;
$this->environment = $environment;
}

/**
Expand All @@ -63,6 +70,14 @@ public function getChannelId()
return $this->channelId;
}

/**
* @return string
*/
public function getEnvironment()
{
return $this->environment;
}

/**
* @param $broadcast
* @param $channel
Expand All @@ -74,11 +89,14 @@ public function isBroadcasting(LiveBroadcast $broadcast, BaseChannel $channel)
}

/**
* @param string $kernelEnvironment
*
* @return bool
*/
public function isValid()
public function isValid($kernelEnvironment)
{
return ($this->getProcessId() !== 0) &&
return ($kernelEnvironment === $this->getEnvironment()) &&
($this->getProcessId() !== 0) &&
($this->getBroadcastId() !== 0) &&
($this->getChannelId() !== 0);
}
Expand Down
19 changes: 10 additions & 9 deletions Broadcaster/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,17 @@ public function getRunningBroadcasts()
{
$this->runningBroadcasts = array();
$this->logger->debug('Get running broadcasts');
$output = $this->schedulerCommands->getRunningProcesses();
$processStrings = $this->schedulerCommands->getRunningProcesses();

foreach ($output as $runningBroadcast) {
foreach ($processStrings as $processString) {
$runningItem = new RunningBroadcast(
$this->schedulerCommands->getBroadcastId($runningBroadcast),
$this->schedulerCommands->getProcessId($runningBroadcast),
$this->schedulerCommands->getChannelId($runningBroadcast)
$this->schedulerCommands->getBroadcastId($processString),
$this->schedulerCommands->getProcessId($processString),
$this->schedulerCommands->getChannelId($processString),
$this->schedulerCommands->getEnvironment($processString)
);

if ($runningItem->isValid()) {
if ($runningItem->isValid($this->schedulerCommands->getKernelEnvironment())) {
$this->runningBroadcasts[] = $runningItem;
}
}
Expand Down Expand Up @@ -230,9 +231,9 @@ protected function getPlannedBroadcasts()
{
$broadcastRepository = $this->entityManager->getRepository('LiveBroadcastBundle:LiveBroadcast');
$expr = Criteria::expr();
$criterea = Criteria::create();
$criteria = Criteria::create();

$criterea->where($expr->andX(
$criteria->where($expr->andX(
$expr->lte('startTimestamp', new \DateTime()),
$expr->gte('endTimestamp', new \DateTime())
));
Expand All @@ -241,7 +242,7 @@ protected function getPlannedBroadcasts()

/* @var LiveBroadcast[] $nowLive */
$this->plannedBroadcasts = $broadcastRepository->createQueryBuilder('lb')
->addCriteria($criterea)
->addCriteria($criteria)
->getQuery()
->getResult();

Expand Down
3 changes: 0 additions & 3 deletions Broadcaster/SchedulerCommandsDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@ public static function createSchedulerCommands($environment)
switch ($osCode) {
case 'WIN':
return new WindowsCommands($environment);
break;
case 'DAR':
return new MacCommands($environment);
break;
default:
return new LinuxCommands($environment);
break;
}
}
}
12 changes: 12 additions & 0 deletions Broadcaster/SchedulerCommandsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,16 @@ public function getBroadcastId($processString);
* @return int|null
*/
public function getChannelId($processString);

/**
* @param string $processString
*
* @return string|null
*/
public function getEnvironment($processString);

/**
* @return string
*/
public function getKernelEnvironment();
}
81 changes: 0 additions & 81 deletions Tests/Broadcaster/Linux/SchedulerCommandsLinuxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,6 @@ class SchedulerCommandsLinuxTest extends \PHPUnit_Framework_TestCase
{
use PHPMock;

/**
* Test the start process command.
*/
public function testStartProcess()
{
$command = new SchedulerCommands('unittest');

$exec = $this->getFunctionMock('Martin1982\LiveBroadcastBundle\Broadcaster', 'exec');
$exec->expects($this->once())->willReturnCallback(
function ($command) {
// @codingStandardsIgnoreLine
self::assertEquals('ffmpeg input output -metadata broadcast_id=4 -metadata unit=test -metadata env=unittest >/dev/null 2>&1 &', $command);
}
);

$command->startProcess('input', 'output', array('broadcast_id' => 4, 'unit' => 'test'));
}

/**
* Test the stop process command.
*/
Expand Down Expand Up @@ -67,67 +49,4 @@ function ($command, &$output) {
// @codingStandardsIgnoreLine
self::assertEquals('1234 ffmpeg -re -i /path/to/video.mp4 -vcodec copy -acodec copy -f flv rtmp://live-ams.twitch.tv/app/ -metadata env=unittest -metadata broadcast_id=1337', $running);
}

/**
* Test retrieval of the broadcast id.
*/
public function testGetBroadcastId()
{
$command = new SchedulerCommands('unittest');
// @codingStandardsIgnoreLine
$id = $command->getBroadcastId('1234 ffmpeg -re -i /path/to/video.mp4 -vcodec copy -acodec copy -f flv rtmp://live-ams.twitch.tv/app/ -metadata env=unittest -metadata broadcast_id=1337');
self::assertEquals(1337, $id);

$command = new SchedulerCommands('unittest');
$id = $command->getBroadcastId('');
self::assertEquals(null, $id);

$command = new SchedulerCommands('unittest');
// @codingStandardsIgnoreLine
$id = $command->getBroadcastId('1234 ffmpeg -re -i /path/to/video.mp4 -vcodec copy -acodec copy -f flv rtmp://live-ams.twitch.tv/app/ -metadata env=unittest -metadata');
self::assertEquals(null, $id);

$command = new SchedulerCommands('unittest');
// @codingStandardsIgnoreLine
$id = $command->getBroadcastId('1234 ffmpeg -re -i /path/to/video.mp4 -vcodec copy -acodec copy -f flv rtmp://live-ams.twitch.tv/app/ -metadata env=unittest -metadata broadcast_id=');
self::assertEquals(null, $id);
}

/**
* Test retrieval of the process id.
*/
public function testGetProcessId()
{
$command = new SchedulerCommands('unittest');
$id = $command->getProcessId('');
self::assertEquals(null, $id);

// @codingStandardsIgnoreLine
$id = $command->getProcessId('1234 ffmpeg -re -i /path/to/video.mp4 -vcodec copy -acodec copy -f flv rtmp://live-ams.twitch.tv/app/ -metadata env=unittest -metadata broadcast_id=1337');
self::assertEquals(1234, $id);

// @codingStandardsIgnoreLine
$id = $command->getProcessId(' 5678 ffmpeg -re -i /path/to/video.mp4 -vcodec copy -acodec copy -f flv rtmp://live-ams.twitch.tv/app/ -metadata env=unittest -metadata broadcast_id=1337');
self::assertEquals(5678, $id);

$id = $command->getBroadcastId('test 5678');
self::assertEquals(null, $id);
}

/**
* Test retrieval of the channel id.
*/
public function testGetChannelId()
{
$command = new SchedulerCommands('unittest');
$id = $command->getChannelId('');
self::assertEquals(null, $id);

$id = $command->getChannelId('channelid=12');
self::assertEquals(null, $id);

// @codingStandardsIgnoreLine
$id = $command->getChannelId('1234 ffmpeg -re -i /path/to/video.mp4 -vcodec copy -acodec copy -f flv rtmp://live-ams.twitch.tv/app/ -metadata env=unittest -metadata broadcast_id=1337 -metadata channel_id=5');
self::assertEquals(5, $id);
}
}
Loading

0 comments on commit 3773007

Please sign in to comment.