Massive update tidying codebase for release
Added POD to client and worker Updated HTMl and Text email templates
This commit is contained in:
@@ -10,13 +10,9 @@ use IPC::Open2;
|
||||
use Log::Handler;
|
||||
use Pod::Usage;
|
||||
use String::Random qw/random_string/;
|
||||
use Storable qw/thaw/;
|
||||
use Storable qw/freeze thaw/;
|
||||
use Switch;
|
||||
|
||||
# Handle interrupts, and term signals
|
||||
$SIG{'INT'} = 'INT_handler';
|
||||
$SIG{'TERM'} = 'TERM_handler';
|
||||
|
||||
# Globals
|
||||
our %options = (
|
||||
verbose => 0,
|
||||
@@ -31,12 +27,12 @@ our %options = (
|
||||
|
||||
Getopt::Long::Configure( qw(bundling no_getopt_compat) );
|
||||
GetOptions(
|
||||
'verbose|v+' => \$options{verbose},
|
||||
'help|h' => \$options{help},
|
||||
'verbose|v' => \$options{verbose},
|
||||
'debug|d' => \$options{debug},
|
||||
'quiet|q' => \$options{quiet},
|
||||
'silent|s' => \$options{silent},
|
||||
'log|l=s' => \$options{log_file},
|
||||
'help|h' => \$options{help},
|
||||
'pretend|n' => \$options{pretend},
|
||||
'job-servers|j=s@' => \$options{job_servers},
|
||||
'handbrake' => \$options{handbrake},
|
||||
@@ -69,19 +65,22 @@ if ( $options{log_file}) {
|
||||
|
||||
# Setup the worker, and listen for jobs
|
||||
my $worker = Gearman::Worker->new(job_servers => $options{job_servers});
|
||||
$worker->register_function(rip => \&do_rip);
|
||||
$worker->register_function(handbrake_rip => \&handbrake_rip);
|
||||
$worker->work while 1;
|
||||
|
||||
sub do_rip {
|
||||
sub handbrake_rip {
|
||||
my $job = shift;
|
||||
my %rip_options = %{ thaw($job->arg) };
|
||||
|
||||
my %response;
|
||||
|
||||
$log->notice("Beginning new rip to $rip_options{output_filename}");
|
||||
|
||||
# Generate a unique filename based on the output filename to prevent clashes from previous runs
|
||||
my $uuid = random_string('cccccc');
|
||||
$log->debug("Using $uuid as unique identifier for this job");
|
||||
$rip_options{unique_output_filename} = $rip_options{output_filename};
|
||||
$rip_options{unique_output_filename} =~ s/\.([^\.]+)$/\.$uuid\.$1/;
|
||||
$rip_options{real_output_filename} = $rip_options{output_filename};
|
||||
$rip_options{real_output_filename} =~ s/\.([^\.]+)$/\.$uuid\.$1/;
|
||||
$response{real_output_filename} = $rip_options{real_output_filename};
|
||||
|
||||
# Generate the command line for handbrake
|
||||
my @handbrake_cmd = (
|
||||
@@ -91,7 +90,7 @@ sub do_rip {
|
||||
# Construct the handbrake command line
|
||||
$options{handbrake},
|
||||
get_options(\%rip_options, 'input_filename', '-i'),
|
||||
get_options(\%rip_options, 'unique_output_filename', '-o'),
|
||||
get_options(\%rip_options, 'real_output_filename', '-o'),
|
||||
get_options(\%rip_options, 'title'),
|
||||
get_options(\%rip_options, 'format', '-f'),
|
||||
get_options(\%rip_options, 'video_codec', '-e'),
|
||||
@@ -105,31 +104,36 @@ sub do_rip {
|
||||
get_options(\%rip_options, 'subtitle_tracks', '-s'),
|
||||
);
|
||||
|
||||
$log->debug("Beginning ripping process with command:\n" . join(' ', @handbrake_cmd));
|
||||
# Return a copy of the command used to rip the title
|
||||
$response{handbrake_cmd} = @handbrake_cmd;
|
||||
|
||||
# Execute the ripping process
|
||||
$log->debug("Beginning ripping process with command:\n" . join(' ', @handbrake_cmd));
|
||||
my ($child_in, $child_out);
|
||||
my $child_pid = open2($child_out, $child_in, @handbrake_cmd);
|
||||
# Don't need to write from the child
|
||||
# No need to write to the child process
|
||||
close($child_in);
|
||||
|
||||
# Log the output from handbrake, and return it back to the client
|
||||
$response{log} = ();
|
||||
my $line;
|
||||
while ($line = <$child_out>) {
|
||||
push @{ $response{log} }, $line;
|
||||
$log->debug($line);
|
||||
}
|
||||
close($child_out);
|
||||
|
||||
# If the rip process failed, report an error status here
|
||||
waitpid($child_pid, 0);
|
||||
my $child_exit_status = $? >> 8;
|
||||
|
||||
if ($child_exit_status) {
|
||||
$log->warning("Ripping process returned error status: $child_exit_status");
|
||||
return undef;
|
||||
$response{status} = $? >> 8;
|
||||
$response{success} = $response{status} == 0;
|
||||
if ($response{success}) {
|
||||
$log->notice("Finished rip to $rip_options{real_output_filename}");
|
||||
} else {
|
||||
$log->warning("Ripping process returned error status: $response{status}");
|
||||
}
|
||||
|
||||
$log->notice("Finished rip to $rip_options{unique_output_filename}");
|
||||
return $rip_options{unique_output_filename};
|
||||
return freeze(\%response);
|
||||
}
|
||||
|
||||
sub get_options {
|
||||
@@ -155,17 +159,6 @@ sub get_options {
|
||||
}
|
||||
}
|
||||
|
||||
sub INT_handler {
|
||||
$log->error("Caught interrupt, exiting.");
|
||||
exit 0;
|
||||
}
|
||||
|
||||
sub TERM_handler {
|
||||
$log->error("Caught SIGTERM, exiting.");
|
||||
exit 0;
|
||||
}
|
||||
|
||||
|
||||
__END__;
|
||||
=head1 NAME
|
||||
|
||||
@@ -173,16 +166,61 @@ __END__;
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
handbrake-cluster-worker.pl -h
|
||||
handbrake-cluster-worker.pl [-v [-d]|-q|-s]
|
||||
[-l logfile]
|
||||
[-n]
|
||||
handbrake-cluster-worker.pl -h|--help
|
||||
handbrake-cluster-worker.pl [[-v|--verbose] | [-d|--debug] |
|
||||
[-q|--quiet] | [-s|--silent]]
|
||||
[-l|--log <log file>]
|
||||
[-n|--pretend]
|
||||
[-j|--job-servers <server list>]
|
||||
[--handbrake <handbrake executable>]
|
||||
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
Processes ripping tasks as requested by a gearman job server. Logging and the
|
||||
job servers to use are configurable by command line arguments.
|
||||
Processes ripping tasks as requested by a gearman job server. Logging and the
|
||||
job servers to use are configurable by command line arguments.
|
||||
|
||||
=head1 OPTIONS
|
||||
|
||||
=over 4
|
||||
|
||||
=item B<-h|--help>
|
||||
|
||||
Displays this help information and quits.
|
||||
|
||||
=item B<-v|--verbose>
|
||||
|
||||
Displays verbose logging information.
|
||||
|
||||
=item B<-d|--debug>
|
||||
|
||||
Displays full debugging information, including all output from HandBrake.
|
||||
|
||||
=item B<-q|--quiet>
|
||||
|
||||
Displays only errors.
|
||||
|
||||
=item B<-s|--silent>
|
||||
|
||||
Displays no output whatsoever, useful for scripting. Output will still be
|
||||
logged to disk if a file is specified.
|
||||
|
||||
=item B<-l|--log E<lt>log fileE<gt>>
|
||||
|
||||
Specifies the name of a file to write logging information to.
|
||||
|
||||
=item B<-n|--pretend>
|
||||
|
||||
Only shows what action would be taken, no rips are actually performed.
|
||||
|
||||
=item B<-j|--job-servers E<lt>server listE<gt>>
|
||||
|
||||
Specifies a comma separated list of alternate servers to use for job
|
||||
distribution.
|
||||
|
||||
=item B<--handbrake E<lt>handbrake executableE<gt>>
|
||||
|
||||
Specifies an alternate HandBrake executable to use. Default is
|
||||
C</usr/bin/HandBrakeCLI>.
|
||||
|
||||
=cut
|
||||
|
||||
Reference in New Issue
Block a user