Skip to content

Commit

Permalink
Making audit layer aware. Adding add_job to queue command line
Browse files Browse the repository at this point in the history
  • Loading branch information
phochste committed Jan 31, 2017
1 parent 003533d commit e9d963f
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 14 deletions.
5 changes: 4 additions & 1 deletion config/catmandu.local.yml-example
Expand Up @@ -2,7 +2,10 @@ default_lang: en

host: "http://localhost:5001"

# Specity the ipranges which can download (local) files
## Uncomment when audit messages need to be displayed
# audit: 1

## Specity the ipranges which can download (local) files
ip_range:
- 157.193.0.0/16
- 127.0.0.1
Expand Down
1 change: 1 addition & 0 deletions cpanfile
Expand Up @@ -28,6 +28,7 @@ requires 'Catmandu::DBI', '>=0.0510';
requires 'Catmandu::BibTeX';
requires 'Catmandu::XML';
requires 'Catmandu::ArXiv';
requires 'Catmandu::LDAP';
requires 'Catmandu::Importer::getJSON';
requires 'Catmandu::Identifier', '>=0.05';
requires 'Catmandu::RIS', '>=0.04';
Expand Down
8 changes: 5 additions & 3 deletions etc/librecat-audit.initd
Expand Up @@ -16,6 +16,8 @@ SERVER_USER="biblio"
SERVER_GROUP="biblio"
WORKERS=1
LIBRECAT_DIR="/opt/librecat"
# If a config.yml is in a layers directory, use this line:
# LIBRECAT_LAYERS="/opt/librecat-ugent"

lock_file=/var/lock/subsys/audit.librecat

Expand All @@ -24,7 +26,7 @@ do_start()
if [ ! -f "$lock_file" ] ; then
cd $LIBRECAT_DIR
echo -n $"Starting ${SCRIPT}: "
sudo -u ${SERVER_USER} -g ${SERVER_GROUP} $CARTON exec "bin/librecat worker audit start --workers ${WORKERS} --supervise" > /dev/null 2>&1
sudo -u ${SERVER_USER} -g ${SERVER_GROUP} $CARTON exec "LIBRECAT_LAYERS=${LIBRECAT_LAYERS} bin/librecat worker audit start --workers ${WORKERS} --supervise" > /dev/null 2>&1
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
touch $lock_file
Expand All @@ -43,7 +45,7 @@ do_stop()
{
cd $LIBRECAT_DIR
echo -n $"Stopping $SCRIPT: "
sudo -u ${SERVER_USER} -g ${SERVER_GROUP} $CARTON exec "bin/librecat worker audit stop --workers ${WORKERS} --supervise" > /dev/null 2>&1
sudo -u ${SERVER_USER} -g ${SERVER_GROUP} $CARTON exec "LIBRECAT_LAYERS=${LIBRECAT_LAYERS} bin/librecat worker audit stop --workers ${WORKERS} --supervise" > /dev/null 2>&1
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
touch $lock_file
Expand All @@ -58,7 +60,7 @@ do_stop()
do_status()
{
cd $LIBRECAT_DIR
sudo -u ${SERVER_USER} -g ${SERVER_GROUP} $CARTON exec "bin/librecat queue"
sudo -u ${SERVER_USER} -g ${SERVER_GROUP} $CARTON exec "LIBRECAT_LAYERS=${LIBRECAT_LAYERS} bin/librecat queue status"
}

case "$1" in
Expand Down
58 changes: 57 additions & 1 deletion lib/LibreCat/Cmd/queue.pm
Expand Up @@ -2,6 +2,8 @@ package LibreCat::Cmd::queue;

use Catmandu::Sane;
use Catmandu;
use LibreCat::App::Helper;
use Carp;
use Net::Telnet::Gearman;

use parent 'LibreCat::Cmd';
Expand All @@ -10,14 +12,60 @@ sub description {
return <<EOF;
Usage:
librecat queue
librecat queue status
librecat queue add_job WORKER FILE
EOF
}

sub command {
my ($self, $opts, $args) = @_;

my $commands = qr/status|add_job/;

unless (@$args) {
$args = ['status'];
}

my $cmd = shift @$args;

unless ($cmd =~ /^$commands$/) {
$self->usage_error("should be one of $commands");
}

if ($cmd eq 'status') {
$self->_status;
}
elsif ($cmd eq 'add_job') {
$self->_add_job(@$args);
}
}

sub _add_job {
my ($self,$worker,$file) = @_;

croak "usage: $0 add_job <worker> <file>" unless defined($worker) && -r $file;

my $importer = Catmandu->importer('YAML', file => $file);
my $exporter = Catmandu->exporter('YAML');
my $queue = LibreCat::App::Helper::Helpers->new->queue;

$importer->each(sub {
my $job = $_[0];

my $response = $queue->add_job("audit",$job);

print "Adding job\n";

$job->{'_response'} = $response;

$exporter->add($job);
});

$exporter->commit;
}

sub _status {
my $gm = Net::Telnet::Gearman->new(Host => '127.0.0.1', Port => 4730,);
my $version = $gm->version;
$version =~ s/^OK //;
Expand Down Expand Up @@ -68,4 +116,12 @@ __END__
LibreCat::Cmd::queue - show job queue status
=head1 SYNOPSIS
# Show the status of all worker
librecat queue status
# Submit a YAML file job to the WORKER
librecat queue add_job WORKER FILE
=cut
17 changes: 10 additions & 7 deletions lib/LibreCat/Hook/audit_message.pm
Expand Up @@ -43,14 +43,17 @@ sub fix {
$action = 'batch';
}

my $job = {
id => $id ,
bag => 'publication' ,
process => "hook($name)" ,
action => "$action" ,
message => "activated by $login ($user_id)" ,
};

h->log->debug("adding job: " . to_yaml($job));
try {
h->queue->add_job('audit',{
id => $id ,
bag => 'publication' ,
process => "hook($name)" ,
action => "$action" ,
message => "activated by $login ($user_id)" ,
});
h->queue->add_job('audit', $job);
} catch {
h->log->trace("caught a : $_");
};
Expand Down
4 changes: 2 additions & 2 deletions views/backend/audit.tt
Expand Up @@ -5,7 +5,7 @@
<h1>[% h.loc("audit.title") %]</h1>
</div>

<table border="1">
<table border="1" width="800">
<tr>
<th>[% h.loc("audit.id") %]</th>
<th>[% h.loc("audit.bag") %]</th>
Expand All @@ -16,7 +16,7 @@
</tr>
[% FOREACH au in audit %]
<tr>
<td>[% au.id %]</td>
<td nowrap="nowrap">[% au.id %]</td>
<td>[% au.bag %]</td>
<td nowrap="nowrap">[% au.process %]</td>
<td>[% au.action %]</td>
Expand Down

0 comments on commit e9d963f

Please sign in to comment.