Bug 22630: DBRev 19.12.00.081
[koha.git] / Koha / Script.pm
blobaaa2e8b5607a1bae8ebdd8b750a376e35b5eaffe
1 package Koha::Script;
3 # Copyright PTFS Europe 2019
4 # Copyright 2019 Koha Development Team
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use Modern::Perl;
23 =head1 NAME
25 Koha::Script - Koha scripts base class
27 =head1 SYNOPSIS
29 use Koha::Script
30 use Koha::Script -cron;
32 =head1 DESCRIPTION
34 This class should be used in all scripts. It sets the interface and userenv appropriately.
36 =cut
38 use File::Basename;
39 use Fcntl qw(:flock);
41 use C4::Context;
42 use Koha::Exceptions;
43 use Koha::Exceptions::Exception;
45 sub import {
46 my $class = shift;
47 my @flags = @_;
49 C4::Context->_new_userenv(1);
50 if ( ( $flags[0] || '' ) eq '-cron' ) {
52 # Set userenv
53 C4::Context->_new_userenv(1);
54 C4::Context->set_userenv(
55 undef, undef, undef, 'CRON', 'CRON',
56 undef, undef, undef, undef, undef
59 # Set interface
60 C4::Context->interface('cron');
63 else {
64 # Set userenv
65 C4::Context->set_userenv(
66 undef, undef, undef, 'CLI', 'CLI',
67 undef, undef, undef, undef, undef
70 # Set interface
71 C4::Context->interface('commandline');
75 =head1 API
77 =head2 Class methods
79 =head3 new
81 my $script = Koha::Script->new(
83 script => $0, # mandatory
84 [ lock_name => 'my_script' ]
88 Create a new Koha::Script object. The I<script> parameter is mandatory,
89 and will usually be passed I<$0> in the caller script. The I<lock_name>
90 parameter is optional, and is used to generate the lock file if passed.
92 =cut
94 sub new {
95 my ($class, $params) = @_;
96 my $script = $params->{script};
98 Koha::Exceptions::MissingParameter->throw(
99 "The 'script' parameter is mandatory. You should usually pass \$0"
100 ) unless $script;
102 my $self = { script => $script };
103 $self->{lock_name} = $params->{lock_name}
104 if exists $params->{lock_name} and $params->{lock_name};
106 bless $self, $class;
107 return $self;
110 =head3 lock_exec
112 # die if cannot get the lock
113 try {
114 $script->lock_exec;
116 catch {
117 die "$_";
120 # wait for the lock to be released
121 $script->lock_exec({ wait => 1 });
123 This method sets an execution lock to prevent concurrent execution of the caller
124 script. If passed the I<wait> parameter with a true value, it will make the caller
125 wait until it can be granted the lock (flock's LOCK_NB behaviour). It will
126 otherwise throw an exception immediately.
128 =cut
130 sub lock_exec {
131 my ($self, $params) = @_;
133 $self->_initialize_locking
134 unless $self->{lock_file};
136 my $lock_params = ($params->{wait}) ? LOCK_EX : LOCK_EX | LOCK_NB;
138 open my $lock_handle, '>', $self->{lock_file}
139 or Koha::Exceptions::Exception->throw("Unable to open the lock file ".$self->{lock_file}.": $!");
140 $self->{lock_handle} = $lock_handle;
141 flock( $lock_handle, $lock_params )
142 or Koha::Exceptions::Exception->throw("Unable to acquire the lock ".$self->{lock_file}.": $!");
145 =head2 Internal methods
147 =head3 _initialize_locking
149 $self->_initialize_locking
151 This method initializes the locking configuration.
153 =cut
155 sub _initialize_locking {
156 my ($self) = @_;
158 my $lock_dir = C4::Context->config('lockdir')
159 // C4::Context->temporary_directory();
161 my $lock_name = $self->{lock_name} // fileparse( $self->{script} );
162 $self->{lock_file} = "$lock_dir/$lock_name";
164 return $self;
167 =head1 AUTHOR
169 Martin Renvoize <martin.renvoize@ptfs-europe.com>
171 =cut