Bug 42714: (QA follow-up) Limit auto-logging to -cron scripts

The INIT/END blocks in Koha::Script fire for every consumer of the
module, including non-cron admin and migration tools. Logging those as
CRONJOBS when CronjobLog is enabled would pollute the action log with
entries for tools that are not scheduled jobs.

Introduce a package-level $_cron flag that is set by import() when the
-cron argument is passed (import() runs at compile time, before INIT
blocks execute, so the flag is in the correct state when INIT fires).
Guard both the INIT and END blocks behind this flag so only scripts that
explicitly declare themselves as cron scripts receive automatic logging.

Also update the import() POD to document the auto-logging behaviour.

Signed-off-by: Pedro Amorim <pedro.amorim@openfifth.co.uk>
This commit is contained in:
Martin Renvoize 2026-06-03 16:15:25 +01:00 committed by Pedro Amorim
parent 1652191b7f
commit ece2d64e76
No known key found for this signature in database
GPG Key ID: 06B75BA7F3894D55
1 changed files with 11 additions and 3 deletions

View File

@ -43,13 +43,17 @@ use C4::Log qw( cronlogaction );
use Koha::Exceptions;
use Koha::Exception;
our $_cron = 0;
INIT {
my $command_line_options = join( " ", @ARGV );
cronlogaction( { info => $command_line_options } );
if ($_cron) {
my $command_line_options = join( " ", @ARGV );
cronlogaction( { info => $command_line_options } );
}
}
END {
cronlogaction( { action => 'End', info => "COMPLETED" } );
cronlogaction( { action => 'End', info => "COMPLETED" } ) if $_cron;
}
=head2 import
@ -58,6 +62,9 @@ END {
use Koha::Script -cron;
Sets the interface and userenv appropriately based on the flags passed.
When the C<-cron> flag is used the script is also automatically logged to
the CRONJOBS action log (start and end) when the C<CronjobLog> system
preference is enabled.
=cut
@ -66,6 +73,7 @@ sub import {
my @flags = @_;
if ( ( $flags[0] || '' ) eq '-cron' ) {
$_cron = 1;
# Set userenv
C4::Context->set_userenv(