Lines 35-41
This class should be used in all scripts. It sets the interface and userenv appr
Link Here
|
35 |
|
35 |
|
36 |
=cut |
36 |
=cut |
37 |
|
37 |
|
|
|
38 |
use File::Basename; |
39 |
use Fcntl qw(:flock); |
40 |
|
38 |
use C4::Context; |
41 |
use C4::Context; |
|
|
42 |
use Koha::Exceptions; |
43 |
use Koha::Exceptions::Exception; |
39 |
|
44 |
|
40 |
sub import { |
45 |
sub import { |
41 |
my $class = shift; |
46 |
my $class = shift; |
Lines 67-72
sub import {
Link Here
|
67 |
} |
72 |
} |
68 |
} |
73 |
} |
69 |
|
74 |
|
|
|
75 |
=head2 new |
76 |
|
77 |
Constructor |
78 |
|
79 |
=cut |
80 |
|
81 |
sub new { |
82 |
my ($class, $params) = @_; |
83 |
my $script = $params->{script}; |
84 |
my $use_lock = $params->{use_lock}; |
85 |
|
86 |
Koha::Exceptions::MissingParameter->throw( |
87 |
"The 'script' parameter is mandatory. You should usually pass \$0" |
88 |
) unless $script; |
89 |
|
90 |
my $self = { |
91 |
script => $script, |
92 |
}; |
93 |
|
94 |
if ($use_lock) { |
95 |
my $lock_dir = C4::Context->config('lock_dir') |
96 |
// C4::Context->temporary_directory(); |
97 |
|
98 |
my $lock_name = $params->{lock_name} // fileparse( $script ); |
99 |
$self->{lock_file} = "$lock_dir/$lock_name"; |
100 |
} |
101 |
|
102 |
bless $self, $class; |
103 |
return $self; |
104 |
} |
105 |
|
106 |
=head2 lock_exec |
107 |
|
108 |
my $script = Koha::Script->new({ script => $0, use_lock => 1[, lock_name => 'my_script'] }); |
109 |
try { |
110 |
$script->lock_exec; |
111 |
} |
112 |
catch { |
113 |
die "$_"; |
114 |
}; |
115 |
$script->lock_exec({ wait => 1 }); |
116 |
|
117 |
=cut |
118 |
|
119 |
sub lock_exec { |
120 |
my ($self, $params) = @_; |
121 |
|
122 |
my $lock_params = ($params->{wait}) ? LOCK_EX : LOCK_EX | LOCK_NB; |
123 |
|
124 |
open our $lock_handle, '>', $self->{lock_file} |
125 |
or Koha::Exceptions::Exception->throw("Unable to open the lock file ".$self->{lock_file}.": $!"); |
126 |
$self->{lock_handle} = $lock_handle; |
127 |
flock( $lock_handle, $lock_params ) |
128 |
or Koha::Exceptions::Exception->throw("Unable to acquire the lock"); |
129 |
} |
130 |
|
70 |
=head1 AUTHOR |
131 |
=head1 AUTHOR |
71 |
|
132 |
|
72 |
Martin Renvoize <martin.renvoize@ptfs-europe.com> |
133 |
Martin Renvoize <martin.renvoize@ptfs-europe.com> |