Jonathan Druart
9d6d641d1f
On bug 17591 we discovered that there was something weird going on with the way we export and use subroutines/modules. This patch tries to standardize our EXPORT to use EXPORT_OK only. That way we will need to explicitely define the subroutine we want to use from a module. This patch is a squashed version of: Bug 17600: After export.pl Bug 17600: After perlimport Bug 17600: Manual changes Bug 17600: Other manual changes after second perlimports run Bug 17600: Fix tests And a lot of other manual changes. export.pl is a dirty script that can be found on bug 17600. "perlimport" is: git clone https://github.com/oalders/App-perlimports.git cd App-perlimports/ cpanm --installdeps . export PERL5LIB="$PERL5LIB:/kohadevbox/koha/App-perlimports/lib" find . \( -name "*.pl" -o -name "*.pm" \) -exec perl App-perlimports/script/perlimports --inplace-edit --no-preserve-unused --filename {} \; The ideas of this patch are to: * use EXPORT_OK instead of EXPORT * perltidy the EXPORT_OK list * remove '&' before the subroutine names * remove some uneeded use statements * explicitely import the subroutines we need within the controllers or modules Note that the private subroutines (starting with _) should not be exported (and not used from outside of the module except from tests). EXPORT vs EXPORT_OK (from https://www.thegeekstuff.com/2010/06/perl-exporter-examples/) """ Export allows to export the functions and variables of modules to user’s namespace using the standard import method. This way, we don’t need to create the objects for the modules to access it’s members. @EXPORT and @EXPORT_OK are the two main variables used during export operation. @EXPORT contains list of symbols (subroutines and variables) of the module to be exported into the caller namespace. @EXPORT_OK does export of symbols on demand basis. """ If this patch caused a conflict with a patch you wrote prior to its push: * Make sure you are not reintroducing a "use" statement that has been removed * "$subroutine" is not exported by the C4::$MODULE module means that you need to add the subroutine to the @EXPORT_OK list * Bareword "$subroutine" not allowed while "strict subs" means that you didn't imported the subroutine from the module: - use $MODULE qw( $subroutine list ); You can also use the fully qualified namespace: C4::$MODULE::$subroutine Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
155 lines
3.7 KiB
Perl
155 lines
3.7 KiB
Perl
package C4::Scheduler;
|
|
|
|
# Copyright 2007 Liblime Ltd
|
|
#
|
|
# This file is part of Koha.
|
|
#
|
|
# Koha is free software; you can redistribute it and/or modify it
|
|
# under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# Koha is distributed in the hope that it will be useful, but
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with Koha; if not, see <http://www.gnu.org/licenses>.
|
|
|
|
use Modern::Perl;
|
|
|
|
use C4::Context;
|
|
use Schedule::At;
|
|
|
|
our (@ISA, @EXPORT_OK);
|
|
BEGIN {
|
|
require Exporter;
|
|
@ISA = qw(Exporter);
|
|
@EXPORT_OK = qw(get_jobs get_at_jobs get_at_job add_at_job remove_at_job);
|
|
}
|
|
|
|
=head1 NAME
|
|
|
|
C4::Scheduler - Module for running jobs with the unix at command
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
use C4::Scheduler;
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
=cut
|
|
|
|
=head1 METHODS
|
|
|
|
=head2 get_jobs();
|
|
|
|
This will return all scheduled jobs
|
|
|
|
=cut
|
|
|
|
sub get_jobs {
|
|
my $jobs = get_at_jobs();
|
|
# add call to get cron jobs here too
|
|
return ($jobs);
|
|
}
|
|
|
|
=head2 get_at_jobs();
|
|
|
|
This will return all At scheduled jobs
|
|
|
|
=cut
|
|
|
|
sub get_at_jobs {
|
|
my %jobs = Schedule::At::getJobs();
|
|
return (\%jobs);
|
|
}
|
|
|
|
=head2 get_at_job($id)
|
|
|
|
This will return the At job with the given id
|
|
|
|
=cut
|
|
|
|
sub get_at_job {
|
|
my ($id)=@_;
|
|
my %jobs = Schedule::At::getJobs(JOBID => $id);
|
|
}
|
|
|
|
=head2 add_at_job ($time,$command)
|
|
|
|
Given a timestamp and a command this will schedule the job to run at that time.
|
|
|
|
Returns true if the job is added to the queue and false otherwise.
|
|
|
|
=cut
|
|
|
|
sub add_at_job {
|
|
my ($time,$command) = @_;
|
|
# FIXME - a description of the task to be run
|
|
# may be a better tag, since the tag is displayed
|
|
# in the job list that the administrator sees - e.g.,
|
|
# "run report foo, send to foo@bar.com"
|
|
Schedule::At::add(TIME => $time, COMMAND => $command, TAG => $command);
|
|
|
|
# FIXME - this method of checking whether the job was added
|
|
# to the queue is less than perfect:
|
|
#
|
|
# 1. Since the command is the tag, it is possible that there is
|
|
# already a job in the queue with the same tag. However, since
|
|
# the tag is what displays in the job list, we can't just
|
|
# give it a unique ID.
|
|
# 2. Schedule::At::add() is supposed to return a non-zero
|
|
# value if it fails to add a job - however, it does
|
|
# not check all error conditions - in particular, it does
|
|
# not check the return value of the "at" run; it basically
|
|
# complains only if it can't find at.
|
|
# 3. Similary, Schedule::At::add() does not do something more useful,
|
|
# such as returning the job ID. To be fair, it is possible
|
|
# that 'at' does not allow this in any portable way.
|
|
# 4. Although unlikely, it is possible that a job could be added
|
|
# and completed instantly, thus dropping off the queue.
|
|
my $job_found = 0;
|
|
eval {
|
|
my %jobs = Schedule::At::getJobs(TAG => $command);
|
|
$job_found = scalar(keys %jobs) > 0;
|
|
};
|
|
if ($@) {
|
|
return 0;
|
|
} else {
|
|
return $job_found;
|
|
}
|
|
}
|
|
|
|
sub remove_at_job {
|
|
my ($jobid)=@_;
|
|
Schedule::At::remove(JOBID => $jobid);
|
|
}
|
|
|
|
1;
|
|
__END__
|
|
|
|
=head1 BUGS
|
|
|
|
At some point C<C4::Scheduler> should be refactored:
|
|
|
|
=over
|
|
|
|
=item At and C<Schedule::At> does not work on Win32.
|
|
|
|
=item At is not installed by default on all platforms.
|
|
|
|
=item The At queue used by Koha is owned by the httpd user. If multiple
|
|
Koha databases share an Apache instance on a server, everybody can
|
|
see everybody's jobs.
|
|
|
|
=item There is no support for scheduling a job to run more than once.
|
|
|
|
=back
|
|
|
|
=head1 AUTHOR
|
|
|
|
Chris Cormack <crc@liblime.com>
|
|
|
|
=cut
|