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>.
25 Koha::Script - Koha scripts base class
30 use Koha::Script -cron;
34 This class should be used in all scripts. It sets the interface and userenv appropriately.
38 use File::Basename qw( fileparse );
39 use Fcntl qw( LOCK_EX LOCK_NB );
49 if ( ( $flags[0] || '' ) eq '-cron' ) {
52 C4::Context->set_userenv(
53 undef, undef, undef, 'CRON', 'CRON',
54 undef, undef, undef, undef, undef
58 C4::Context->interface('cron');
63 C4::Context->set_userenv(
64 undef, undef, undef, 'CLI', 'CLI',
65 undef, undef, undef, undef, undef
69 C4::Context->interface('commandline');
79 my $script = Koha::Script->new(
81 script => $0, # mandatory
82 [ lock_name => 'my_script' ]
86 Create a new Koha::Script object. The I<script> parameter is mandatory,
87 and will usually be passed I<$0> in the caller script. The I<lock_name>
88 parameter is optional, and is used to generate the lock file if passed.
93 my ($class, $params) = @_;
94 my $script = $params->{script};
96 Koha::Exceptions::MissingParameter->throw(
97 "The 'script' parameter is mandatory. You should usually pass \$0"
100 my $self = { script => $script };
101 $self->{lock_name} = $params->{lock_name}
102 if exists $params->{lock_name} and $params->{lock_name};
110 # die if cannot get the lock
118 # wait for the lock to be released
119 $script->lock_exec({ wait => 1 });
121 This method sets an execution lock to prevent concurrent execution of the caller
122 script. If passed the I<wait> parameter with a true value, it will make the caller
123 wait until it can be granted the lock (flock's LOCK_NB behaviour). It will
124 otherwise throw an exception immediately.
129 my ($self, $params) = @_;
131 $self->_initialize_locking
132 unless $self->{lock_file};
134 my $lock_params = ($params->{wait}) ? LOCK_EX : LOCK_EX | LOCK_NB;
136 open my $lock_handle, '>', $self->{lock_file}
137 or Koha::Exception->throw("Unable to open the lock file ".$self->{lock_file}.": $!");
138 $self->{lock_handle} = $lock_handle;
139 flock( $lock_handle, $lock_params )
140 or Koha::Exception->throw("Unable to acquire the lock ".$self->{lock_file}.": $!");
143 =head2 Internal methods
145 =head3 _initialize_locking
147 $self->_initialize_locking
149 This method initializes the locking configuration.
153 sub _initialize_locking {
156 my $lock_dir = C4::Context->config('lockdir')
157 // C4::Context->temporary_directory();
159 my $lock_name = $self->{lock_name} // fileparse( $self->{script} );
160 $self->{lock_file} = "$lock_dir/$lock_name";
167 Martin Renvoize <martin.renvoize@ptfs-europe.com>