Bug 35197: (QA follow-up): Update tablename
[koha.git] / Koha / Script.pm
1 package Koha::Script;
2
3 # Copyright PTFS Europe 2019
4 # Copyright 2019 Koha Development Team
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
17 #
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>.
20
21 use Modern::Perl;
22
23 =head1 NAME
24
25 Koha::Script - Koha scripts base class
26
27 =head1 SYNOPSIS
28
29     use Koha::Script
30     use Koha::Script -cron;
31
32 =head1 DESCRIPTION
33
34 This class should be used in all scripts. It sets the interface and userenv appropriately.
35
36 =cut
37
38 use File::Basename qw( fileparse );
39 use Fcntl qw( LOCK_EX LOCK_NB );
40
41 use C4::Context;
42 use Koha::Exceptions;
43 use Koha::Exception;
44
45 sub import {
46     my $class = shift;
47     my @flags = @_;
48
49     if ( ( $flags[0] || '' ) eq '-cron' ) {
50
51         # Set userenv
52         C4::Context->set_userenv(
53             undef, undef, undef, 'CRON', 'CRON',
54             undef, undef, undef, undef,  undef
55         );
56
57         # Set interface
58         C4::Context->interface('cron');
59
60     }
61     else {
62         # Set userenv
63         C4::Context->set_userenv(
64             undef, undef, undef, 'CLI', 'CLI',
65             undef, undef, undef, undef,  undef
66         );
67
68         # Set interface
69         C4::Context->interface('commandline');
70     }
71 }
72
73 =head1 API
74
75 =head2 Class methods
76
77 =head3 new
78
79     my $script = Koha::Script->new(
80         {
81             script    => $0, # mandatory
82           [ lock_name => 'my_script' ]
83         }
84     );
85
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.
89
90 =cut
91
92 sub new {
93     my ($class, $params) = @_;
94     my $script   = $params->{script};
95
96     Koha::Exceptions::MissingParameter->throw(
97         "The 'script' parameter is mandatory. You should usually pass \$0"
98     ) unless $script;
99
100     my $self = { script => $script };
101     $self->{lock_name} = $params->{lock_name}
102         if exists $params->{lock_name} and $params->{lock_name};
103
104     bless $self, $class;
105     return $self;
106 }
107
108 =head3 lock_exec
109
110     # die if cannot get the lock
111     try {
112         $script->lock_exec;
113     }
114     catch {
115         die "$_";
116     };
117
118     # wait for the lock to be released
119     $script->lock_exec({ wait => 1 });
120
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.
125
126 =cut
127
128 sub lock_exec {
129     my ($self, $params) = @_;
130
131     $self->_initialize_locking
132         unless $self->{lock_file};
133
134     my $lock_params = ($params->{wait}) ? LOCK_EX : LOCK_EX | LOCK_NB;
135
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}.": $!");
141 }
142
143 =head2 Internal methods
144
145 =head3 _initialize_locking
146
147     $self->_initialize_locking
148
149 This method initializes the locking configuration.
150
151 =cut
152
153 sub _initialize_locking {
154     my ($self) = @_;
155
156     my $lock_dir = C4::Context->config('lockdir')
157         // C4::Context->temporary_directory();
158
159     my $lock_name = $self->{lock_name} // fileparse( $self->{script} );
160     $self->{lock_file} = "$lock_dir/$lock_name";
161
162     return $self;
163 }
164
165 =head1 AUTHOR
166
167 Martin Renvoize <martin.renvoize@ptfs-europe.com>
168
169 =cut
170
171 1;