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.
43 use Koha
::Exceptions
::Exception
;
49 C4
::Context
->_new_userenv(1);
50 if ( ( $flags[0] || '' ) eq '-cron' ) {
53 C4
::Context
->_new_userenv(1);
54 C4
::Context
->set_userenv(
55 undef, undef, undef, 'CRON', 'CRON',
56 undef, undef, undef, undef, undef
60 C4
::Context
->interface('cron');
65 C4
::Context
->set_userenv(
66 undef, undef, undef, 'CLI', 'CLI',
67 undef, undef, undef, undef, undef
71 C4
::Context
->interface('commandline');
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.
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"
102 my $self = { script
=> $script };
103 $self->{lock_name
} = $params->{lock_name
}
104 if exists $params->{lock_name
} and $params->{lock_name
};
112 # die if cannot get the lock
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.
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.
155 sub _initialize_locking
{
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";
169 Martin Renvoize <martin.renvoize@ptfs-europe.com>