From a1705efe1bc5df7833d2580375ef1c5d3635d769 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Wed, 10 Apr 2019 15:46:34 +0200 Subject: [PATCH] Add purge script, add install documentation --- INSTALL.md | 7 --- .../Com/BibLibre/TransitionBibliographique.pm | 31 +++++++++++++ .../TransitionBibliographique/cron/purge.pl | 45 +++++++++++++++++++ README.md | 27 ++++++++++- 4 files changed, 101 insertions(+), 9 deletions(-) delete mode 100644 INSTALL.md create mode 100755 Koha/Plugin/Com/BibLibre/TransitionBibliographique/cron/purge.pl diff --git a/INSTALL.md b/INSTALL.md deleted file mode 100644 index 311fabb..0000000 --- a/INSTALL.md +++ /dev/null @@ -1,7 +0,0 @@ -# Installation du plugin Koha "transition bibliographique" - -## Pré-requis - -Librairies: -* libcatmandu-perl -* Catmandu::Importer::MARC diff --git a/Koha/Plugin/Com/BibLibre/TransitionBibliographique.pm b/Koha/Plugin/Com/BibLibre/TransitionBibliographique.pm index b2d6c7e..8a1f8e8 100644 --- a/Koha/Plugin/Com/BibLibre/TransitionBibliographique.pm +++ b/Koha/Plugin/Com/BibLibre/TransitionBibliographique.pm @@ -660,4 +660,35 @@ sub get_marc_record { return $marc_record; }; +sub purge { + my ($self, $older_than) = @_; + + my $dbh = C4::Context->dbh; + my $jobs_table = $self->get_qualified_table_name('jobs'); + my $jobs = $dbh->selectall_arrayref(qq{ + SELECT * FROM $jobs_table + WHERE state = 'finished' + AND finished_at < DATE_SUB(CURRENT_TIMESTAMP, INTERVAL ? DAY) + ORDER BY enqueued_at ASC + }, { Slice => {} }, $older_than); + + my $delete_sth = $dbh->prepare(qq{ + DELETE FROM $jobs_table + WHERE id = ? + }); + + foreach my $job (@$jobs) { + say STDERR "Removing job " . $job->{id}; + + $job->{args} = decode_json($job->{args}); + my $filepath = $job->{args}->{filepath}; + + say STDERR "Removing file $filepath"; + unlink $filepath or say STDERR "Could not unlink file $filepath: $!"; + + say STDERR "Removing database entry"; + $delete_sth->execute($job->{id}) or say STDERR "Could not remove database entry: " . $delete_sth->errstr; + } +} + 1; diff --git a/Koha/Plugin/Com/BibLibre/TransitionBibliographique/cron/purge.pl b/Koha/Plugin/Com/BibLibre/TransitionBibliographique/cron/purge.pl new file mode 100755 index 0000000..0a449d6 --- /dev/null +++ b/Koha/Plugin/Com/BibLibre/TransitionBibliographique/cron/purge.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl + +use Modern::Perl; +use Getopt::Long; + +use Koha::Plugins; + +use Koha::Plugin::Com::BibLibre::TransitionBibliographique; + +my $help; +my $older_than = 30; + +GetOptions( + 'help' => \$help, + 'older-than=i' => \$older_than, +) or die usage(); + +if ($help) { + print usage(); + exit; +} + +my $plugin = Koha::Plugin::Com::BibLibre::TransitionBibliographique->new({ + enable_plugins => 1, +}); +$plugin->purge($older_than); + +sub usage { + my $usage = <<'EOF'; +purge.pl [options] +purge.pl --help + +Remove finished jobs and related files + +Options + --older-than=DAYS + Remove jobs that are finished since more than DAYS days ago. + Defaults to 30 + + --help + Display this help message +EOF + + return $usage; +} diff --git a/README.md b/README.md index 965b966..8f36f1d 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,8 @@ transition bibliographique - Koha 18.11 minimum - Modules Perl: - - Catmandu - - Catmandu::Exporter::MARC + - [Catmandu](https://metacpan.org/pod/Catmandu) + - [Catmandu::MARC](https://metacpan.org/pod/Catmandu::MARC) - YAML # Installation @@ -21,3 +21,26 @@ transition bibliographique 4. Si besoin, modifier `config.yaml` 5. Mettre `Koha/Plugin/Com/BibLibre/TransitionBibliographique/cron/export.pl` en cronjob et lancer manuellement le script une première fois. +6. Mettre + `Koha/Plugin/Com/BibLibre/TransitionBibliographique/cron/job-runner.pl` + en cronjob. +7. (Optionnel) Mettre + `Koha/Plugin/Com/BibLibre/TransitionBibliographique/cron/purge.pl` en + cronjob + +# Cronjobs + +Tous les cronjobs doivent être lancés quotidiennement, de préférence la nuit +pour ne pas gêner l'utilisation normale de Koha. + +Exemple: + +``` +PERL5LIB=/path/to/koha +KOHA_CONF=/path/to/koha-conf.xml +PATH_TO_PLUGIN=/path/to/plugin + +0 22 * * * $PATH_TO_PLUGIN/Koha/Plugin/Com/BibLibre/TransitionBibliographique/cron/export.pl +50 22 * * * $PATH_TO_PLUGIN/Koha/Plugin/Com/BibLibre/TransitionBibliographique/cron/purge.pl --older-than=30 +0 23 * * * $PATH_TO_PLUGIN/Koha/Plugin/Com/BibLibre/TransitionBibliographique/cron/job-runner.pl +```