Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

12 refactor devices #19

Merged
merged 22 commits into from
Dec 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.swp
*.log
File renamed without changes.
38 changes: 38 additions & 0 deletions lib/App/EventStreamr/DVswitch/Mixer.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package App::EventStreamr::DVswitch::Mixer;
use Method::Signatures;
use Moo;
use namespace::clean;

# ABSTRACT: A DVswitch Process

# VERSION: Generated by DZP::OurPkg:Version

=head1 SYNOPSIS

This Provides a pre-configured DVswitch process.

=head1 DESCRIPTION

This largely extends L<App::EventStreamr::Process>, provides
default cmds that can be overidden in the configuration.

=cut

extends 'App::EventStreamr::Process';

has 'cmd' => ( is => 'ro', lazy => 1, builder => 1 );
has 'id' => ( is => 'ro', default => sub { 'dvswitch' } );
has 'type' => ( is => 'ro', default => sub { 'mixer' } );

method _build_cmd() {
my $command = $self->{config}{commands}{dvswitch} ? $self->{config}{commands}{dvswitch} : 'dvswitch -h 0.0.0.0 -p $port';

my %cmd_vars = (
port => $self->{config}{mixer}{port},
);

$command =~ s/\$(\w+)/$cmd_vars{$1}/g;
return $command;
}

1;
42 changes: 42 additions & 0 deletions lib/App/EventStreamr/DVswitch/Record.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package App::EventStreamr::DVswitch::Record;
use Method::Signatures;
use Moo;
use namespace::clean;

# ABSTRACT: A DVswitch Standby Process

# VERSION: Generated by DZP::OurPkg:Version

=head1 SYNOPSIS

This Provides a pre-configured DVswitch process.

=head1 DESCRIPTION

This largely extends L<App::EventStreamr::Process>, provides
default cmds that can be overidden in the configuration.

=cut

extends 'App::EventStreamr::Process';

has 'cmd' => ( is => 'ro', lazy => 1, builder => 1 );
has 'id' => ( is => 'ro', default => sub { 'dvsink' } );
has 'type' => ( is => 'ro', default => sub { 'record' } );

method _build_cmd() {
my $command = $self->{config}{commands}{file} ? $self->{config}{commands}{file} : 'dvsink-files -h $host -p $port $path/%Y-%m-%d_%H-%M-%S.dv';

my %cmd_vars = (
host => $self->{config}{mixer}{host},
port => $self->{config}{mixer}{port},
path => $self->{status}{$self->{id}}{record_path},
);

$command =~ s/\$(\w+)/$cmd_vars{$1}/g;
return $command;
}

with('App::EventStreamr::Roles::Record');

1;
64 changes: 64 additions & 0 deletions lib/App/EventStreamr/DVswitch/Running.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package App::EventStreamr::DVswitch::Running;

use v5.010;
use strict;
use warnings;
use Method::Signatures 20140224; # libmethod-signatures-perl
use Proc::ProcessTable; # libproc-processtable-perl
use IO::Socket::INET;

use Moo::Role; # libmoo-perl

# ABSTRACT: A recording role

# VERSION: Generated by DZP::OurPkg:Version

=head1 SYNOPSIS

This is a role wraps around the 'run_stop' of a process.

=head1 DESCRIPTION

This is a Role that can be consumed to ensure the path exists
before allowing the record process to take place.

It requires a 'run_stop' method, so really should only be consumed
by processes that extend L<App::EventStreamr::Process>.

=cut

requires 'run_stop','status','config','id','type';

method _dvswitch_running() {
# TODO: This is an example of what could used as a dependency for
# Processes. Unsure what impact Opening/closing a port per process
# check will have on dvswitch. Probably should do something similar
# to the original logic and set a flag.
my $state;
my $sock = new IO::Socket::INET (
PeerAddr => $self->{config}{mixer}{host},
PeerPort => $self->{config}{mixer}{port},
Proto => 'tcp'
);

if ($sock) {
$state = 1;
$sock->close;
} else {
$state = 0;
}
return $state;
}

around 'run_stop' => sub {
my $orig = shift;
my $self = shift;

if ( $self->_dvswitch_running() ) {
$orig->($self);
} else {
$self->status->threshold($self->{id},'dvswitch_not_running');
}
};

1;
40 changes: 40 additions & 0 deletions lib/App/EventStreamr/DVswitch/Standby.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package App::EventStreamr::DVswitch::Standby;
use Method::Signatures;
use Moo;
use namespace::clean;

# ABSTRACT: A DVswitch Standby Process

# VERSION: Generated by DZP::OurPkg:Version

=head1 SYNOPSIS

This Provides a pre-configured DVswitch process.

=head1 DESCRIPTION

This largely extends L<App::EventStreamr::Process>, provides
default cmds that can be overidden in the configuration.

=cut

extends 'App::EventStreamr::Process';

has 'cmd' => ( is => 'ro', lazy => 1, builder => 1 );
has 'id' => ( is => 'ro', default => sub { 'dvfile' } );
has 'type' => ( is => 'ro', default => sub { 'ingest' } );

method _build_cmd() {
my $command = $self->{config}{commands}{file} ? $self->{config}{commands}{file} : 'dvsource-file -h $host -p $port -l $file';

my %cmd_vars = (
file => $self->{config}{mixer}{loop},
host => $self->{config}{mixer}{host},
port => $self->{config}{mixer}{port},
);

$command =~ s/\$(\w+)/$cmd_vars{$1}/g;
return $command;
}

1;
51 changes: 51 additions & 0 deletions lib/App/EventStreamr/DVswitch/V4L.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package App::EventStreamr::DVswitch::V4L;
use Method::Signatures;
use Moo;
use namespace::clean;

# ABSTRACT: A DVswitch V4l Process

# VERSION: Generated by DZP::OurPkg:Version

=head1 SYNOPSIS

This Provides a V4L ingest process.

=head1 DESCRIPTION

This largely extends L<App::EventStreamr::Process>, provides
default cmds that can be overidden in the configuration.

=cut

extends 'App::EventStreamr::Process';

has 'cmd' => ( is => 'ro', lazy => 1, builder => 1 );
has 'cmd_regex' => ( is => 'ro', lazy => 1, builder => 1 );
has 'id' => ( is => 'ro', required => 1 );
has 'device' => ( is => 'ro', required => 1 );
has 'type' => ( is => 'ro', default => sub { 'ingest' } );

method _build_cmd() {
my $command;
if ( -e '/usr/bin/avconv' ) {
$command = $self->{config}{commands}{file} ? $self->{config}{commands}{file} : 'avconv -f video4linux2 -s vga -r 25 -i $device -target pal-dv - | dvsource-file /dev/stdin -h $host -p $port';
} else {
$command = $self->{config}{commands}{file} ? $self->{config}{commands}{file} : 'ffmpeg -f video4linux2 -s vga -r 25 -i $device -target pal-dv - | dvsource-file /dev/stdin -h $host -p $port';
}

my %cmd_vars = (
device => $self->device,
host => $self->{config}{mixer}{host},
port => $self->{config}{mixer}{port},
);

$command =~ s/\$(\w+)/$cmd_vars{$1}/g;
return $command;
}

method _build_cmd_regex() {
return qr:^[ffmpeg|avconv].+/dev/$self->{id}.*:;
}

1;
34 changes: 34 additions & 0 deletions lib/App/EventStreamr/Internal/API.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package App::EventStreamr::Internal::API;
use Method::Signatures;
use FindBin '$Bin';
use Moo;
use namespace::clean;

# ABSTRACT: An EventStreamr API Process

# VERSION: Generated by DZP::OurPkg:Version

=head1 SYNOPSIS

This manages the internal EventStreamr API

=head1 DESCRIPTION

This largely extends L<App::EventStreamr::Process>, provides
default cmds that can be overidden in the configuration.

=cut

extends 'App::EventStreamr::Process';

has 'cmd' => ( is => 'ro', default => sub { "plackup -s Twiggy -p 3000 $Bin/eventstreamr-api.pl --daemon --environment production" } );
has 'cmd_regex' => ( is => 'ro', default => sub { "\\/bin\\/plackup.*" } );
has 'id' => ( is => 'ro', default => sub { 'api' } );
has 'type' => ( is => 'ro', default => sub { 'internal' } );

# We're special, we should always be running
method _run() {
return 1;
}

1;
121 changes: 121 additions & 0 deletions lib/App/EventStreamr/Process.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package App::EventStreamr::Process;
use Method::Signatures;
use Moo;
use Scalar::Util::Reftype;
use Carp 'croak';
use namespace::clean;

# ABSTRACT: A process object

# VERSION: Generated by DZP::OurPkg:Version

=head1 SYNOPSIS

This package provides core start/stop logic for all processes
and devices.

=head1 DESCRIPTION

This package provides the core run/stop logic for EventStreamr. A
process will extend this and provide any extra logic specifc to its
needs.

It consumes the 'ProcessControl' role with requires a 'cmd' attribute
and will utilise an optional 'cmd_regex' if one exists.

=cut

my $ConfigRef = sub {
croak "auth isn't a 'App::EventStreamr::Config' object!" unless reftype( $_[0] )->class eq "App::EventStreamr::Config";
};

my $StatusRef = sub {
croak "auth isn't a 'App::EventStreamr::Status' object!" unless reftype( $_[0] )->class eq "App::EventStreamr::Status";
};

has 'config' => ( is => 'rw', required => 1, isa => $ConfigRef );
has 'status' => ( is => 'rw', required => 1, isa => $StatusRef );
has 'cmd' => ( is => 'ro', required => 1 );
has 'id' => ( is => 'ro', required => 1 );
has 'type' => ( is => 'ro', default => sub { 'internal' } );
has 'sleep_time' => ( is => 'ro', default => sub { 1 } );
has 'cmd_regex' => ( is => 'ro' );

method _run() {
# Unless we spefically set our state we're going to want to run
# By default our control config will be empty and this shortcut
# attriubute makes things look a little cleaner.
my $run = defined $self->{config}{control}{$self->{id}}{run} ? $self->{config}{control}{$self->{id}}{run} : 1;

$self->{config}{control}{$self->{id}}{run} = $run;

# We only want to run if both the process is set to run
# and the system is set to run.
if ($run && $self->{config}{run}) {
return 1;
} else {
return 0;
}
}

method _restart() {
if (defined $self->{config}{control}{$self->{id}}{run} && $self->{config}{control}{$self->{id}}{run} == 2) {
return 1;
} elsif ($self->{config}{run} == 2) {
return 1;
} else {
return 0;
}
}

=method run_stop
$device->run_stop()

Will start the process if it's intended to be running or stop it
if isn't.

=cut

method run_stop() {
if ($self->_restart) {
$self->status->restarting($self->{id});

if (! $self->running) {
$self->{config}{control}{$self->{id}}{run} = 1;
} else {
$self->status->stopping($self->{id});
$self->stop();

# Give the process time to settle.
sleep $self->sleep_time;
}

} elsif ( $self->_run && ! $self->pid) {
# Slows down the restarts to ensure we don't flood logs
# and control systems.
return if $self->status->threshold($self->{id});

$self->status->starting($self->{id},$self->{type});

$self->start();

# Give the process time to settle.
sleep $self->sleep_time;
} elsif ( (! $self->_run && $self->pid ) ) {
$self->status->stopping($self->{id});

$self->stop();

# Give the process time to settle.
sleep $self->sleep_time;
}


# TODO: Post config if changed
$self->status->set_state($self->running,$self->{id});
return;
}

with('App::EventStreamr::Roles::ProcessControl');

1;
Loading