Latest version with support for webui and database logging

This commit is contained in:
2010-06-08 23:13:36 +01:00
parent 9358c100ae
commit bfcb32f9cb
3 changed files with 160 additions and 122 deletions

View File

@@ -1,12 +1,14 @@
#!/usr/bin/perl
package HandbrakeCluster::Client;
use warnings;
use strict;
use Data::Dumper;
use Gearman::Client;
use Getopt::Long;
use Log::Handler;
use Log::Log4perl;
use MIME::Lite::TT::HTML;
use Pod::Usage;
use Storable qw/freeze thaw/;
@@ -14,22 +16,19 @@ use YAML::Any;
# Globals
my $default_options = {
verbose => 0,
debug => 0,
quiet => 0,
log_file => '/tmp/handbrake-cluster-client.log',
silent => 0,
log_config => 'logging.conf',
help => 0,
pretend => 0,
config_file => '',
job_servers => ['build0.sihnon.net', 'build1.sihnon.net', 'build2.sihnon.net'],
gearman_prefix => '',
limit => [],
email_target => '',
email_sender => '',
email_subject => 'Rip completed',
email_timezone => 'Europe/London',
email_html => 'rip-completed.html',
email_text => 'rip-completed.txt',
email_target => '',
email_sender => '',
email_subject => 'Rip completed',
email_timezone => 'Europe/London',
email_html => 'rip-completed.html',
email_text => 'rip-completed.txt',
};
my $options = { map { $_ => undef } keys %$default_options };
@@ -55,14 +54,11 @@ my $rip_options = { map { $_ => undef } keys %$default_rip_options };
Getopt::Long::Configure( qw(bundling no_getopt_compat) );
GetOptions(
'verbose|v+' => \$options->{verbose},
'debug|d' => \$options->{debug},
'quiet|q' => \$options->{quiet},
'silent|s' => \$options->{silent},
'log|l=s' => \$options->{log_file},
'log-config|l=s' => \$options->{log_config},
'help|h' => \$options->{help},
'pretend|n' => \$options->{pretend},
'job-servers|j=s@' => \$options->{job_servers},
'gearman-prefix=s' => \$options->{gearman_prefix},
'config|c=s' => \$options->{config_file},
'limit|L=s' => sub { my ($name, $value) = @_; $options->{limit} = [split(/,/, $value)]; },
'nice|N=i' => \$rip_options->{nice},
@@ -95,33 +91,14 @@ $jobs{__commandline} = $rip_options if $options->{title};
die "No rips specified" unless %jobs;
# Setup logging
my $log = Log::Handler->new();
# default to logging warning+, unless quiet in which case log error+, or debug in which case log everything
my $maxLogLevel = ($options->{debug} ? 'debug' : ($options->{quiet} ? 3 : $options->{verbose} + 4));
# Ignore the quiet option when logging to disk
my $maxFileLogLevel = ($options->{debug} ? 'debug' : 'info');
Log::Log4perl->init($options->{log_config});
my $client_log = Log::Log4perl->get_logger('HandbrakeCluster::Client');
$client_log->debug("Logging started");
if ( ! $options->{silent}) {
$log->add(
screen => {
log_to => 'STDOUT',
minlevel => 'emergency',
maxlevel => $maxLogLevel,
},
);
}
if ( $options->{log_file}) {
$log->add(
file => {
filename => $options->{log_file},
minlevel => 'emergency',
maxlevel => $maxFileLogLevel,
},
);
}
# Setup the distribution client
my $client = Gearman::Client->new;
$client->prefix($options->{gearman_prefix}) if $options->{gearman_prefix};
$client->job_servers($options->{job_servers});
# Add new ripping task for each job to run
@@ -131,7 +108,13 @@ foreach my $job_id (keys %jobs) {
my $job = $jobs{$job_id};
# Check that the job hasn't been restricted by the limit option
next if (!$options->{limit} || !$options->{job_permitted}->{$job_id});
if (@{ $options->{limit} } && !$options->{job_permitted}->{$job_id}) {
$client_log->info("Skipped job $job_id due to limit restrictions");
next;
}
# Create a new database record for this job, and grab the new id
$job->{db_job_id} = 43;
$progress{$job_id} = 0;
@@ -148,14 +131,14 @@ foreach my $job_id (keys %jobs) {
on_complete => \&on_complete_handler,
on_retry => sub {
my $attempt = shift or die;
$log->warning("Retrying '$job_id' rip");
$client_log->warning("Retrying '$job_id' rip");
},
on_fail => sub {
$log->warning("Rip '$job_id' failed");
$client_log->warning("Rip '$job_id' failed");
},
}
);
$log->info("Enqueued '$job_id' rip");
$client_log->info("Enqueued '$job_id' rip");
}
$taskset->wait;
@@ -188,7 +171,7 @@ sub on_complete_handler {
$email->send;
}
$log->notice("Completed rip to $response->{real_output_filename}");
$client_log->info("Completed rip to $response->{real_output_filename}");
}
sub display_progress {