init III
This commit is contained in:
159
Perl OTRS/Kernel/System/Daemon/DaemonModules/BaseTaskWorker.pm
Normal file
159
Perl OTRS/Kernel/System/Daemon/DaemonModules/BaseTaskWorker.pm
Normal file
@@ -0,0 +1,159 @@
|
||||
# --
|
||||
# Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
|
||||
# --
|
||||
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
# the enclosed file COPYING for license information (GPL). If you
|
||||
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
|
||||
# --
|
||||
|
||||
package Kernel::System::Daemon::DaemonModules::BaseTaskWorker;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
our @ObjectDependencies = (
|
||||
'Kernel::Config',
|
||||
'Kernel::System::Email',
|
||||
'Kernel::System::Log',
|
||||
);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Kernel::System::Daemon::DaemonModules::BaseTaskWorker - scheduler task worker base class
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
Base class for scheduler daemon task worker modules.
|
||||
|
||||
=head1 PUBLIC INTERFACE
|
||||
|
||||
=begin Internal:
|
||||
|
||||
=head2 _HandleError()
|
||||
|
||||
Creates a system error message and sends an email with the error messages form a task execution.
|
||||
|
||||
my $Success = $TaskWorkerObject->_HandleError(
|
||||
TaskName => 'some name',
|
||||
TaskType => 'some type',
|
||||
LogMessage => 'some message', # message to set in the OTRS error log
|
||||
ErrorMessage => 'some message', # message to be sent as a body of the email, usually contains
|
||||
# all messages from STDERR including tracebacks
|
||||
);
|
||||
|
||||
=cut
|
||||
|
||||
sub _HandleError {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => $Param{LogMessage},
|
||||
);
|
||||
|
||||
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
|
||||
|
||||
my $From = $ConfigObject->Get('NotificationSenderName') . ' <'
|
||||
. $ConfigObject->Get('NotificationSenderEmail') . '>';
|
||||
|
||||
my $To = $ConfigObject->Get('Daemon::SchedulerTaskWorker::NotificationRecipientEmail') || '';
|
||||
|
||||
if ( $From && $To ) {
|
||||
|
||||
my $Sent = $Kernel::OM->Get('Kernel::System::Email')->Send(
|
||||
From => $From,
|
||||
To => $To,
|
||||
Subject => "OTRS Scheduler Daemon $Param{TaskType}: $Param{TaskName}",
|
||||
Charset => 'utf-8',
|
||||
MimeType => 'text/plain',
|
||||
Body => $Param{ErrorMessage},
|
||||
);
|
||||
|
||||
return 1 if $Sent->{Success};
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
=head2 _CheckTaskParams()
|
||||
|
||||
Performs basic checks for common task parameters.
|
||||
|
||||
my $Success = $TaskWorkerObject->_CheckTaskParams(
|
||||
TaskID => 123,
|
||||
TaskName => 'some name', # optional
|
||||
Data => $TaskDataHasRef,
|
||||
NeededDataAttributes => ['Object', 'Function'], # optional, list of attributes that task needs in Data hash
|
||||
DataParamsRef => 'HASH', # optional, 'HASH' or 'ARRAY', kind of reference of Data->Params
|
||||
);
|
||||
|
||||
=cut
|
||||
|
||||
sub _CheckTaskParams {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
for my $Needed (qw(TaskID Data)) {
|
||||
if ( !$Param{$Needed} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need $Needed! - Task: $Param{TaskName}",
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
# Check data.
|
||||
if ( ref $Param{Data} ne 'HASH' ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Got no valid Data! - Task: $Param{TaskName}",
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
# Check mandatory attributes in Data.
|
||||
if ( $Param{NeededDataAttributes} && ref $Param{NeededDataAttributes} eq 'ARRAY' ) {
|
||||
|
||||
for my $Needed ( @{ $Param{NeededDataAttributes} } ) {
|
||||
if ( !$Param{Data}->{$Needed} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Need Data->$Needed! - Task: $Param{TaskName}",
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Check the structure of Data params.
|
||||
if ( $Param{DataParamsRef} ) {
|
||||
|
||||
if ( $Param{Data}->{Params} && ref $Param{Data}->{Params} ne uc $Param{DataParamsRef} ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Data->Params is invalid, reference is not $Param{DataParamsRef}! - Task: $Param{TaskName}",
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
1;
|
||||
|
||||
=end Internal:
|
||||
|
||||
=head1 TERMS AND CONDITIONS
|
||||
|
||||
This software is part of the OTRS project (L<https://otrs.org/>).
|
||||
|
||||
This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
the enclosed file COPYING for license information (GPL). If you
|
||||
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
|
||||
|
||||
=cut
|
||||
@@ -0,0 +1,155 @@
|
||||
# --
|
||||
# Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
|
||||
# --
|
||||
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
# the enclosed file COPYING for license information (GPL). If you
|
||||
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
|
||||
# --
|
||||
|
||||
package Kernel::System::Daemon::DaemonModules::SchedulerCronTaskManager;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use utf8;
|
||||
|
||||
use Kernel::System::VariableCheck qw(:all);
|
||||
|
||||
use parent qw(Kernel::System::Daemon::BaseDaemon);
|
||||
|
||||
our @ObjectDependencies = (
|
||||
'Kernel::Config',
|
||||
'Kernel::System::Cache',
|
||||
'Kernel::System::DB',
|
||||
'Kernel::System::Daemon::SchedulerDB',
|
||||
'Kernel::System::Log',
|
||||
);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Kernel::System::Daemon::DaemonModules::SchedulerCronTaskManager - daemon to manage scheduler cron tasks
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
Scheduler cron task daemon
|
||||
|
||||
=head1 PUBLIC INTERFACE
|
||||
|
||||
=head2 new()
|
||||
|
||||
Create scheduler cron task manager object.
|
||||
|
||||
=cut
|
||||
|
||||
sub new {
|
||||
my ( $Type, %Param ) = @_;
|
||||
|
||||
# Allocate new hash for object.
|
||||
my $Self = {};
|
||||
bless $Self, $Type;
|
||||
|
||||
# Get objects in constructor to save performance.
|
||||
$Self->{ConfigObject} = $Kernel::OM->Get('Kernel::Config');
|
||||
$Self->{CacheObject} = $Kernel::OM->Get('Kernel::System::Cache');
|
||||
$Self->{DBObject} = $Kernel::OM->Get('Kernel::System::DB');
|
||||
$Self->{SchedulerDBObject} = $Kernel::OM->Get('Kernel::System::Daemon::SchedulerDB');
|
||||
|
||||
# Disable in memory cache to be clusterable.
|
||||
$Self->{CacheObject}->Configure(
|
||||
CacheInMemory => 0,
|
||||
CacheInBackend => 1,
|
||||
);
|
||||
|
||||
# Get the NodeID from the SysConfig settings, this is used on High Availability systems.
|
||||
$Self->{NodeID} = $Self->{ConfigObject}->Get('NodeID') || 1;
|
||||
|
||||
# Check NodeID, if does not match is impossible to continue.
|
||||
if ( $Self->{NodeID} !~ m{ \A \d+ \z }xms && $Self->{NodeID} > 0 && $Self->{NodeID} < 1000 ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "NodeID '$Self->{NodeID}' is invalid!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
# Do not change the following values!
|
||||
$Self->{SleepPost} = 20; # sleep 20 seconds after each loop
|
||||
$Self->{Discard} = 60 * 60; # discard every hour
|
||||
|
||||
$Self->{DiscardCount} = $Self->{Discard} / $Self->{SleepPost};
|
||||
|
||||
$Self->{Debug} = $Param{Debug};
|
||||
$Self->{DaemonName} = 'Daemon: SchedulerCronTaskManager';
|
||||
|
||||
return $Self;
|
||||
}
|
||||
|
||||
sub PreRun {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# Check if database is on-line.
|
||||
return 1 if $Self->{DBObject}->Ping();
|
||||
|
||||
sleep 10;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sub Run {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
return if !$Self->{SchedulerDBObject}->CronTaskToExecute(
|
||||
NodeID => $Self->{NodeID},
|
||||
PID => $$,
|
||||
);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub PostRun {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
sleep $Self->{SleepPost};
|
||||
|
||||
$Self->{DiscardCount}--;
|
||||
|
||||
# Unlock long locked tasks.
|
||||
$Self->{SchedulerDBObject}->RecurrentTaskUnlockExpired(
|
||||
Type => 'Cron',
|
||||
);
|
||||
|
||||
# Remove obsolete tasks before destroy.
|
||||
if ( $Self->{DiscardCount} == 0 ) {
|
||||
$Self->{SchedulerDBObject}->CronTaskCleanup();
|
||||
|
||||
if ( $Self->{Debug} ) {
|
||||
print " $Self->{DaemonName} will be stopped and set for restart!\n";
|
||||
}
|
||||
}
|
||||
|
||||
return if $Self->{DiscardCount} <= 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub Summary {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
return $Self->{SchedulerDBObject}->CronTaskSummary();
|
||||
}
|
||||
|
||||
sub DESTROY {
|
||||
my $Self = shift;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
=head1 TERMS AND CONDITIONS
|
||||
|
||||
This software is part of the OTRS project (L<https://otrs.org/>).
|
||||
|
||||
This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
the enclosed file COPYING for license information (GPL). If you
|
||||
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
|
||||
|
||||
=cut
|
||||
|
||||
1;
|
||||
@@ -0,0 +1,147 @@
|
||||
# --
|
||||
# Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
|
||||
# --
|
||||
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
# the enclosed file COPYING for license information (GPL). If you
|
||||
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
|
||||
# --
|
||||
|
||||
package Kernel::System::Daemon::DaemonModules::SchedulerFutureTaskManager;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use utf8;
|
||||
|
||||
use parent qw(Kernel::System::Daemon::BaseDaemon);
|
||||
|
||||
our @ObjectDependencies = (
|
||||
'Kernel::Config',
|
||||
'Kernel::System::DB',
|
||||
'Kernel::System::Daemon::SchedulerDB',
|
||||
'Kernel::System::Cache',
|
||||
'Kernel::System::Log',
|
||||
);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Kernel::System::Daemon::DaemonModules::SchedulerFutureTaskManager - daemon to manage scheduler future tasks
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
Scheduler future task daemon
|
||||
|
||||
=head1 PUBLIC INTERFACE
|
||||
|
||||
=head2 new()
|
||||
|
||||
Create scheduler future task manager object.
|
||||
|
||||
=cut
|
||||
|
||||
sub new {
|
||||
my ( $Type, %Param ) = @_;
|
||||
|
||||
# Allocate new hash for object.
|
||||
my $Self = {};
|
||||
bless $Self, $Type;
|
||||
|
||||
# Get objects in constructor to save performance.
|
||||
$Self->{ConfigObject} = $Kernel::OM->Get('Kernel::Config');
|
||||
$Self->{CacheObject} = $Kernel::OM->Get('Kernel::System::Cache');
|
||||
$Self->{DBObject} = $Kernel::OM->Get('Kernel::System::DB');
|
||||
$Self->{SchedulerDBObject} = $Kernel::OM->Get('Kernel::System::Daemon::SchedulerDB');
|
||||
|
||||
# Disable in memory cache to be clusterable.
|
||||
$Self->{CacheObject}->Configure(
|
||||
CacheInMemory => 0,
|
||||
CacheInBackend => 1,
|
||||
);
|
||||
|
||||
# Get the NodeID from the SysConfig settings, this is used on High Availability systems.
|
||||
$Self->{NodeID} = $Self->{ConfigObject}->Get('NodeID') || 1;
|
||||
|
||||
# Check NodeID, if does not match is impossible to continue.
|
||||
if ( $Self->{NodeID} !~ m{ \A \d+ \z }xms && $Self->{NodeID} > 0 && $Self->{NodeID} < 1000 ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "NodeID '$Self->{NodeID}' is invalid!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
# Do not change the following values!
|
||||
# Modulo in PreRun() can be damaged after a change.
|
||||
$Self->{SleepPost} = 1; # sleep 1 second after each loop
|
||||
$Self->{Discard} = 60 * 60; # discard every hour
|
||||
|
||||
$Self->{DiscardCount} = $Self->{Discard} / $Self->{SleepPost};
|
||||
|
||||
$Self->{Debug} = $Param{Debug};
|
||||
$Self->{DaemonName} = 'Daemon: SchedulerFutureTaskManager';
|
||||
|
||||
return $Self;
|
||||
}
|
||||
|
||||
sub PreRun {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# Check the database connection each 10 seconds.
|
||||
return 1 if $Self->{DiscardCount} % ( 10 / $Self->{SleepPost} );
|
||||
|
||||
# Check if database is on-line.
|
||||
return 1 if $Self->{DBObject}->Ping();
|
||||
|
||||
sleep 10;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sub Run {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
return if !$Self->{SchedulerDBObject}->FutureTaskToExecute(
|
||||
NodeID => $Self->{NodeID},
|
||||
PID => $$,
|
||||
);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub PostRun {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
sleep $Self->{SleepPost};
|
||||
|
||||
$Self->{DiscardCount}--;
|
||||
|
||||
if ( $Self->{Debug} && $Self->{DiscardCount} == 0 ) {
|
||||
print " $Self->{DaemonName} will be stopped and set for restart!\n";
|
||||
}
|
||||
|
||||
return if $Self->{DiscardCount} <= 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub Summary {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
return $Self->{SchedulerDBObject}->FutureTaskSummary();
|
||||
}
|
||||
|
||||
sub DESTROY {
|
||||
my $Self = shift;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
=head1 TERMS AND CONDITIONS
|
||||
|
||||
This software is part of the OTRS project (L<https://otrs.org/>).
|
||||
|
||||
This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
the enclosed file COPYING for license information (GPL). If you
|
||||
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
|
||||
|
||||
=cut
|
||||
@@ -0,0 +1,155 @@
|
||||
# --
|
||||
# Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
|
||||
# --
|
||||
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
# the enclosed file COPYING for license information (GPL). If you
|
||||
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
|
||||
# --
|
||||
|
||||
package Kernel::System::Daemon::DaemonModules::SchedulerGenericAgentTaskManager;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use utf8;
|
||||
|
||||
use parent qw(Kernel::System::Daemon::BaseDaemon);
|
||||
|
||||
our @ObjectDependencies = (
|
||||
'Kernel::Config',
|
||||
'Kernel::System::Cache',
|
||||
'Kernel::System::CronEvent',
|
||||
'Kernel::System::DB',
|
||||
'Kernel::System::Daemon::SchedulerDB',
|
||||
'Kernel::System::GenericAgent',
|
||||
'Kernel::System::Log',
|
||||
);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Kernel::System::Daemon::DaemonModules::SchedulerGenericAgentTaskManager - daemon to manage scheduler generic agent tasks
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
Scheduler generic agent task daemon
|
||||
|
||||
=head1 PUBLIC INTERFACE
|
||||
|
||||
=head2 new()
|
||||
|
||||
Create scheduler future task manager object.
|
||||
|
||||
=cut
|
||||
|
||||
sub new {
|
||||
my ( $Type, %Param ) = @_;
|
||||
|
||||
# Allocate new hash for object.
|
||||
my $Self = {};
|
||||
bless $Self, $Type;
|
||||
|
||||
# Get objects in constructor to save performance.
|
||||
$Self->{CacheObject} = $Kernel::OM->Get('Kernel::System::Cache');
|
||||
$Self->{GenericAgentObject} = $Kernel::OM->Get('Kernel::System::GenericAgent');
|
||||
$Self->{DBObject} = $Kernel::OM->Get('Kernel::System::DB');
|
||||
$Self->{SchedulerDBObject} = $Kernel::OM->Get('Kernel::System::Daemon::SchedulerDB');
|
||||
|
||||
# Disable in memory cache to be clusterable.
|
||||
$Self->{CacheObject}->Configure(
|
||||
CacheInMemory => 0,
|
||||
CacheInBackend => 1,
|
||||
);
|
||||
|
||||
# Get the NodeID from the SysConfig settings, this is used on High Availability systems.
|
||||
$Self->{NodeID} = $Kernel::OM->Get('Kernel::Config')->Get('NodeID') || 1;
|
||||
|
||||
# Check NodeID, if does not match is impossible to continue.
|
||||
if ( $Self->{NodeID} !~ m{ \A \d+ \z }xms && $Self->{NodeID} > 0 && $Self->{NodeID} < 1000 ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "NodeID '$Self->{NodeID}' is invalid!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
# Do not change the following values!
|
||||
$Self->{SleepPost} = 20; # sleep 20 seconds after each loop
|
||||
$Self->{Discard} = 60 * 60; # discard every hour
|
||||
|
||||
$Self->{DiscardCount} = $Self->{Discard} / $Self->{SleepPost};
|
||||
|
||||
$Self->{Debug} = $Param{Debug};
|
||||
$Self->{DaemonName} = 'Daemon: SchedulerGenericAgentTaskManager';
|
||||
|
||||
return $Self;
|
||||
}
|
||||
|
||||
sub PreRun {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# Check if database is on-line.
|
||||
return 1 if $Self->{DBObject}->Ping();
|
||||
|
||||
sleep 10;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sub Run {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
return if !$Self->{SchedulerDBObject}->GenericAgentTaskToExecute(
|
||||
NodeID => $Self->{NodeID},
|
||||
PID => $$,
|
||||
);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub PostRun {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
sleep $Self->{SleepPost};
|
||||
|
||||
$Self->{DiscardCount}--;
|
||||
|
||||
# Unlock long locked tasks.
|
||||
$Self->{SchedulerDBObject}->RecurrentTaskUnlockExpired(
|
||||
Type => 'GenericAgent',
|
||||
);
|
||||
|
||||
# Remove obsolete tasks before destroy.
|
||||
if ( $Self->{DiscardCount} == 0 ) {
|
||||
$Self->{SchedulerDBObject}->GenericAgentTaskCleanup();
|
||||
|
||||
if ( $Self->{Debug} ) {
|
||||
print " $Self->{DaemonName} will be stopped and set for restart!\n";
|
||||
}
|
||||
}
|
||||
|
||||
return if $Self->{DiscardCount} <= 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub Summary {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
return $Self->{SchedulerDBObject}->GenericAgentTaskSummary();
|
||||
}
|
||||
|
||||
sub DESTROY {
|
||||
my $Self = shift;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
=head1 TERMS AND CONDITIONS
|
||||
|
||||
This software is part of the OTRS project (L<https://otrs.org/>).
|
||||
|
||||
This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
the enclosed file COPYING for license information (GPL). If you
|
||||
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
|
||||
|
||||
=cut
|
||||
@@ -0,0 +1,427 @@
|
||||
# --
|
||||
# Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
|
||||
# --
|
||||
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
# the enclosed file COPYING for license information (GPL). If you
|
||||
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
|
||||
# --
|
||||
|
||||
package Kernel::System::Daemon::DaemonModules::SchedulerTaskWorker;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use utf8;
|
||||
|
||||
use File::Path qw();
|
||||
use Time::HiRes qw(sleep);
|
||||
|
||||
use parent qw(Kernel::System::Daemon::BaseDaemon);
|
||||
|
||||
our @ObjectDependencies = (
|
||||
'Kernel::Config',
|
||||
'Kernel::System::DB',
|
||||
'Kernel::System::Daemon::SchedulerDB',
|
||||
'Kernel::System::DateTime',
|
||||
'Kernel::System::Cache',
|
||||
'Kernel::System::Log',
|
||||
'Kernel::System::Main',
|
||||
'Kernel::System::Storable',
|
||||
);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Kernel::System::Daemon::DaemonModules::SchedulerTaskWorker - worker daemon for the scheduler
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
Scheduler worker daemon
|
||||
|
||||
=head1 PUBLIC INTERFACE
|
||||
|
||||
=head2 new()
|
||||
|
||||
Create scheduler task worker object.
|
||||
|
||||
=cut
|
||||
|
||||
sub new {
|
||||
my ( $Type, %Param ) = @_;
|
||||
|
||||
# Allocate new hash for object.
|
||||
my $Self = {};
|
||||
bless $Self, $Type;
|
||||
|
||||
# Get objects in constructor to save performance.
|
||||
$Self->{ConfigObject} = $Kernel::OM->Get('Kernel::Config');
|
||||
$Self->{CacheObject} = $Kernel::OM->Get('Kernel::System::Cache');
|
||||
$Self->{MainObject} = $Kernel::OM->Get('Kernel::System::Main');
|
||||
$Self->{DBObject} = $Kernel::OM->Get('Kernel::System::DB');
|
||||
$Self->{StorableObject} = $Kernel::OM->Get('Kernel::System::Storable');
|
||||
$Self->{SchedulerDBObject} = $Kernel::OM->Get('Kernel::System::Daemon::SchedulerDB');
|
||||
$Self->{DateTimeObject} = $Kernel::OM->Create('Kernel::System::DateTime');
|
||||
|
||||
# Disable in memory cache to be clusterable.
|
||||
$Self->{CacheObject}->Configure(
|
||||
CacheInMemory => 0,
|
||||
CacheInBackend => 1,
|
||||
);
|
||||
|
||||
# Get the NodeID from the SysConfig settings, this is used on High Availability systems.
|
||||
$Self->{NodeID} = $Self->{ConfigObject}->Get('NodeID') || 1;
|
||||
|
||||
# Check NodeID, if does not match is impossible to continue.
|
||||
if ( $Self->{NodeID} !~ m{ \A \d+ \z }xms && $Self->{NodeID} > 0 && $Self->{NodeID} < 1000 ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "NodeID '$Self->{NodeID}' is invalid!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
# Get pid directory.
|
||||
my $BaseDir = $Self->{ConfigObject}->Get('Daemon::PID::Path') || $Self->{ConfigObject}->Get('Home') . '/var/run/';
|
||||
$Self->{PIDDir} = $BaseDir . 'Daemon/Scheduler/';
|
||||
$Self->{PIDFile} = $Self->{PIDDir} . "Worker-NodeID-$Self->{NodeID}.pid";
|
||||
|
||||
# Check pid hash and pid file.
|
||||
return if !$Self->_WorkerPIDsCheck();
|
||||
|
||||
# Get the maximum number of workers (forks to execute the tasks).
|
||||
$Self->{MaximumWorkers} = $Self->{ConfigObject}->Get('Daemon::SchedulerTaskWorker::MaximumWorkers') || 5;
|
||||
|
||||
# Do not change the following values!
|
||||
# Modulo in PreRun() can be damaged after a change.
|
||||
$Self->{SleepPost} = 0.25; # sleep 60 seconds after each loop
|
||||
$Self->{Discard} = 60 * 60; # discard every hour
|
||||
|
||||
$Self->{DiscardCount} = $Self->{Discard} / $Self->{SleepPost};
|
||||
|
||||
$Self->{Debug} = $Param{Debug};
|
||||
$Self->{DaemonName} = 'Daemon: SchedulerTaskWorker';
|
||||
|
||||
return $Self;
|
||||
}
|
||||
|
||||
sub PreRun {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# Check each 10 seconds.
|
||||
return 1 if $Self->{DiscardCount} % ( 10 / $Self->{SleepPost} );
|
||||
|
||||
# Set running daemon cache.
|
||||
$Self->{CacheObject}->Set(
|
||||
Type => 'DaemonRunning',
|
||||
Key => $Self->{NodeID},
|
||||
Value => 1,
|
||||
TTL => 30,
|
||||
CacheInMemory => 0,
|
||||
CacheInBackend => 1,
|
||||
);
|
||||
|
||||
# Check if database is on-line.
|
||||
return 1 if $Self->{DBObject}->Ping();
|
||||
|
||||
sleep 10;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sub Run {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
$Self->{CurrentWorkersCount} = scalar keys %{ $Self->{CurrentWorkers} };
|
||||
|
||||
my @TaskList = $Self->{SchedulerDBObject}->TaskListUnlocked();
|
||||
|
||||
TASK:
|
||||
for my $TaskID (@TaskList) {
|
||||
|
||||
last TASK if $Self->{CurrentWorkersCount} >= $Self->{MaximumWorkers};
|
||||
|
||||
# Disconnect database before fork.
|
||||
$Self->{DBObject}->Disconnect();
|
||||
|
||||
# Create a fork of the current process
|
||||
# parent gets the PID of the child
|
||||
# child gets PID = 0
|
||||
my $PID = fork;
|
||||
|
||||
# At the child, execute task.
|
||||
if ( !$PID ) {
|
||||
|
||||
# Remove the ZZZAAuto.pm from %INC to force reloading it.
|
||||
delete $INC{'Kernel/Config/Files/ZZZAAuto.pm'};
|
||||
|
||||
# Destroy objects.
|
||||
$Kernel::OM->ObjectsDiscard(
|
||||
ForcePackageReload => 1,
|
||||
);
|
||||
|
||||
# Disable in memory cache because many processes runs at the same time.
|
||||
$Kernel::OM->Get('Kernel::System::Cache')->Configure(
|
||||
CacheInMemory => 0,
|
||||
CacheInBackend => 1,
|
||||
);
|
||||
|
||||
my $SchedulerDBObject = $Kernel::OM->Get('Kernel::System::Daemon::SchedulerDB');
|
||||
|
||||
# Try to lock the task.
|
||||
my $LockSucess = $SchedulerDBObject->TaskLock(
|
||||
TaskID => $TaskID,
|
||||
NodeID => $Self->{NodeID},
|
||||
PID => $$,
|
||||
);
|
||||
|
||||
exit 1 if !$LockSucess;
|
||||
|
||||
my %Task = $SchedulerDBObject->TaskGet(
|
||||
TaskID => $TaskID,
|
||||
);
|
||||
|
||||
# Do error handling.
|
||||
if ( !%Task || !$Task{Type} || !$Task{Data} || ref $Task{Data} ne 'HASH' ) {
|
||||
|
||||
$SchedulerDBObject->TaskDelete(
|
||||
TaskID => $TaskID,
|
||||
);
|
||||
|
||||
my $TaskName = $Task{Name} || '';
|
||||
my $TaskType = $Task{Type} || '';
|
||||
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Task $TaskType $TaskName ($TaskID) was deleted due missing task data!",
|
||||
);
|
||||
|
||||
exit 1;
|
||||
}
|
||||
|
||||
my $TaskHandlerModule = 'Kernel::System::Daemon::DaemonModules::SchedulerTaskWorker::' . $Task{Type};
|
||||
|
||||
my $TaskHandlerObject;
|
||||
eval {
|
||||
|
||||
$Kernel::OM->ObjectParamAdd(
|
||||
$TaskHandlerModule => {
|
||||
Debug => $Self->{Debug},
|
||||
},
|
||||
);
|
||||
|
||||
$TaskHandlerObject = $Kernel::OM->Get($TaskHandlerModule);
|
||||
};
|
||||
|
||||
# Do error handling.
|
||||
if ( !$TaskHandlerObject ) {
|
||||
|
||||
$SchedulerDBObject->TaskDelete(
|
||||
TaskID => $TaskID,
|
||||
);
|
||||
|
||||
my $TaskName = $Task{Name} || '';
|
||||
my $TaskType = $Task{Type} || '';
|
||||
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Task $TaskType $TaskName ($TaskID) was deleted due missing handler object!",
|
||||
);
|
||||
|
||||
exit 1;
|
||||
}
|
||||
|
||||
$TaskHandlerObject->Run(
|
||||
TaskID => $TaskID,
|
||||
TaskName => $Task{Name} || '',
|
||||
Data => $Task{Data},
|
||||
);
|
||||
|
||||
# Force transactional events to run by discarding all objects before deleting the task.
|
||||
$Kernel::OM->ObjectEventsHandle();
|
||||
|
||||
$SchedulerDBObject->TaskDelete(
|
||||
TaskID => $TaskID,
|
||||
);
|
||||
|
||||
exit 0;
|
||||
}
|
||||
|
||||
# Check if fork was not possible.
|
||||
if ( $PID < 0 ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Could not create a child process (worker) for task id $TaskID!",
|
||||
);
|
||||
next TASK;
|
||||
}
|
||||
|
||||
# Populate current workers hash to the parent knows witch task is executing each worker.
|
||||
$Self->{CurrentWorkers}->{$PID} = {
|
||||
PID => $PID,
|
||||
TaskID => $TaskID,
|
||||
StartTime => $Self->{DateTimeObject}->ToEpoch(),
|
||||
};
|
||||
|
||||
$Self->{CurrentWorkersCount}++;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub PostRun {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
sleep $Self->{SleepPost};
|
||||
|
||||
# Check pid hash and pid file after sleep time to give the workers time to finish.
|
||||
return if !$Self->_WorkerPIDsCheck();
|
||||
|
||||
$Self->{DiscardCount}--;
|
||||
|
||||
# Update task locks and remove expired each 60 seconds.
|
||||
if ( !int $Self->{DiscardCount} % ( 60 / $Self->{SleepPost} ) ) {
|
||||
|
||||
# Extract current working task IDs.
|
||||
my @LockedTaskIDs = map { $Self->{CurrentWorkers}->{$_}->{TaskID} } sort keys %{ $Self->{CurrentWorkers} };
|
||||
|
||||
# Update locks (only for this node).
|
||||
if (@LockedTaskIDs) {
|
||||
$Self->{SchedulerDBObject}->TaskLockUpdate(
|
||||
TaskIDs => \@LockedTaskIDs,
|
||||
);
|
||||
}
|
||||
|
||||
# Unlock expired tasks (for all nodes).
|
||||
$Self->{SchedulerDBObject}->TaskUnlockExpired();
|
||||
}
|
||||
|
||||
# Remove obsolete tasks before destroy.
|
||||
if ( $Self->{DiscardCount} == 0 ) {
|
||||
$Self->{SchedulerDBObject}->TaskCleanup();
|
||||
|
||||
if ( $Self->{Debug} ) {
|
||||
print " $Self->{DaemonName} will be stopped and set for restart!\n";
|
||||
}
|
||||
}
|
||||
|
||||
return if $Self->{DiscardCount} <= 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub Summary {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
return $Self->{SchedulerDBObject}->TaskSummary();
|
||||
}
|
||||
|
||||
sub _WorkerPIDsCheck {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# Check pid directory.
|
||||
if ( !-e $Self->{PIDDir} ) {
|
||||
|
||||
File::Path::mkpath( $Self->{PIDDir}, 0, 0770 ); ## no critic
|
||||
|
||||
if ( !-e $Self->{PIDDir} ) {
|
||||
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Can't create directory '$Self->{PIDDir}': $!",
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
# Load current workers initially.
|
||||
if ( !defined $Self->{CurrentWorkers} ) {
|
||||
|
||||
# read pid file
|
||||
if ( -e $Self->{PIDFile} ) {
|
||||
|
||||
my $PIDFileContent = $Self->{MainObject}->FileRead(
|
||||
Location => $Self->{PIDFile},
|
||||
Mode => 'binmode',
|
||||
Type => 'Local',
|
||||
Result => 'SCALAR',
|
||||
DisableWarnings => 1,
|
||||
);
|
||||
|
||||
# Deserialize the content of the PID file.
|
||||
if ($PIDFileContent) {
|
||||
|
||||
my $WorkerPIDs = $Self->{StorableObject}->Deserialize(
|
||||
Data => ${$PIDFileContent},
|
||||
) || {};
|
||||
|
||||
$Self->{CurrentWorkers} = $WorkerPIDs;
|
||||
}
|
||||
}
|
||||
|
||||
$Self->{CurrentWorkers} ||= {};
|
||||
}
|
||||
|
||||
# Check worker PIDs.
|
||||
WORKERPID:
|
||||
for my $WorkerPID ( sort keys %{ $Self->{CurrentWorkers} } ) {
|
||||
|
||||
# Check if PID is still alive.
|
||||
next WORKERPID if kill 0, $WorkerPID;
|
||||
|
||||
delete $Self->{CurrentWorkers}->{$WorkerPID};
|
||||
}
|
||||
|
||||
$Self->{WrittenPids} //= 'REWRITE REQUIRED';
|
||||
|
||||
my $PidsString = join '-', sort keys %{ $Self->{CurrentWorkers} };
|
||||
$PidsString ||= '';
|
||||
|
||||
# Return if nothing has changed.
|
||||
return 1 if $PidsString eq $Self->{WrittenPids};
|
||||
|
||||
# Update pid file.
|
||||
if ( %{ $Self->{CurrentWorkers} } ) {
|
||||
|
||||
# Serialize the current worker hash.
|
||||
my $CurrentWorkersString = $Self->{StorableObject}->Serialize(
|
||||
Data => $Self->{CurrentWorkers},
|
||||
);
|
||||
|
||||
# Write new pid file.
|
||||
my $Success = $Self->{MainObject}->FileWrite(
|
||||
Location => $Self->{PIDFile},
|
||||
Content => \$CurrentWorkersString,
|
||||
Mode => 'binmode',
|
||||
Type => 'Local',
|
||||
Permission => '600',
|
||||
);
|
||||
|
||||
return if !$Success;
|
||||
}
|
||||
elsif ( -e $Self->{PIDFile} ) {
|
||||
|
||||
# Remove empty file.
|
||||
return if !unlink $Self->{PIDFile};
|
||||
}
|
||||
|
||||
# Save last written PIDs.
|
||||
$Self->{WrittenPids} = $PidsString;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub DESTROY {
|
||||
my $Self = shift;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
=head1 TERMS AND CONDITIONS
|
||||
|
||||
This software is part of the OTRS project (L<https://otrs.org/>).
|
||||
|
||||
This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
the enclosed file COPYING for license information (GPL). If you
|
||||
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
|
||||
|
||||
=cut
|
||||
@@ -0,0 +1,169 @@
|
||||
# --
|
||||
# Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
|
||||
# --
|
||||
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
# the enclosed file COPYING for license information (GPL). If you
|
||||
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
|
||||
# --
|
||||
|
||||
package Kernel::System::Daemon::DaemonModules::SchedulerTaskWorker::AsynchronousExecutor;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use parent qw(Kernel::System::Daemon::DaemonModules::BaseTaskWorker);
|
||||
|
||||
our @ObjectDependencies = (
|
||||
'Kernel::System::Log',
|
||||
);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Kernel::System::Daemon::DaemonModules::SchedulerTaskWorker::AsynchronousExecutor - Scheduler daemon task handler module for generic asynchronous tasks
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
This task handler executes scheduler generic asynchronous tasks.
|
||||
|
||||
=head1 PUBLIC INTERFACE
|
||||
|
||||
=head2 new()
|
||||
|
||||
my $TaskHandlerObject = $Kernel::OM->Get('Kernel::System::Daemon::DaemonModules::SchedulerTaskWorker::AsynchronousExecutor');
|
||||
|
||||
=cut
|
||||
|
||||
sub new {
|
||||
my ( $Type, %Param ) = @_;
|
||||
|
||||
my $Self = {};
|
||||
bless( $Self, $Type );
|
||||
|
||||
$Self->{Debug} = $Param{Debug};
|
||||
$Self->{WorkerName} = 'Worker: AsynchronousExecutor';
|
||||
|
||||
return $Self;
|
||||
}
|
||||
|
||||
=head2 Run()
|
||||
|
||||
Performs the selected asynchronous task.
|
||||
|
||||
my $Success = $TaskHandlerObject->Run(
|
||||
TaskID => 123,
|
||||
TaskName => 'some name', # optional
|
||||
Data => {
|
||||
Object => 'Some::Object::Name',
|
||||
Function => 'SomeFunctionName',
|
||||
Params => $Params , # HashRef with the needed parameters
|
||||
},
|
||||
);
|
||||
|
||||
Returns:
|
||||
|
||||
$Success => 1, # or fail in case of an error
|
||||
|
||||
=cut
|
||||
|
||||
sub Run {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# Check task params.
|
||||
my $CheckResult = $Self->_CheckTaskParams(
|
||||
%Param,
|
||||
NeededDataAttributes => [ 'Object', 'Function' ],
|
||||
);
|
||||
|
||||
# Stop execution if an error in params is detected.
|
||||
return if !$CheckResult;
|
||||
|
||||
$Param{Data}->{Params} //= {};
|
||||
|
||||
# Stop execution if invalid params ref is detected.
|
||||
return if !ref $Param{Data}->{Params};
|
||||
|
||||
my $LocalObject;
|
||||
eval {
|
||||
$LocalObject = $Kernel::OM->Get( $Param{Data}->{Object} );
|
||||
};
|
||||
if ( !$LocalObject ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Could not create a new object $Param{Data}->{Object}! - Task: $Param{TaskName}",
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
# Check if the module provide the required function().
|
||||
if ( !$LocalObject->can( $Param{Data}->{Function} ) ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "$Param{Data}->{Object} does not provide $Param{Data}->{Function}()! - Task: $Param{TaskName}",
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
my $Function = $Param{Data}->{Function};
|
||||
|
||||
my $ErrorMessage;
|
||||
|
||||
if ( $Self->{Debug} ) {
|
||||
print " $Self->{WorkerName} executes task: $Param{TaskName}\n";
|
||||
}
|
||||
|
||||
# Run given function on the object with the specified parameters in Data->{Params}
|
||||
eval {
|
||||
|
||||
# Restore child signal to default, main daemon set it to 'IGNORE' to be able to create
|
||||
# multiple process at the same time, but in workers this causes problems if function does
|
||||
# system calls (on linux), since system calls returns -1. See bug#12126.
|
||||
local $SIG{CHLD} = 'DEFAULT';
|
||||
|
||||
# Localize the standard error, everything will be restored after the eval block.
|
||||
local *STDERR;
|
||||
|
||||
# Redirect the standard error to a variable.
|
||||
open STDERR, ">>", \$ErrorMessage;
|
||||
|
||||
if ( ref $Param{Data}->{Params} eq 'ARRAY' ) {
|
||||
$LocalObject->$Function(
|
||||
@{ $Param{Data}->{Params} },
|
||||
);
|
||||
}
|
||||
else {
|
||||
$LocalObject->$Function(
|
||||
%{ $Param{Data}->{Params} // {} },
|
||||
);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
# Check if there are errors.
|
||||
if ($ErrorMessage) {
|
||||
|
||||
$Self->_HandleError(
|
||||
TaskName => $Param{TaskName},
|
||||
TaskType => 'AsynchronousExecutor',
|
||||
LogMessage => "There was an error executing $Function() in $Param{Data}->{Object}: $ErrorMessage",
|
||||
ErrorMessage => $ErrorMessage,
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
=head1 TERMS AND CONDITIONS
|
||||
|
||||
This software is part of the OTRS project (L<https://otrs.org/>).
|
||||
|
||||
This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
the enclosed file COPYING for license information (GPL). If you
|
||||
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
|
||||
|
||||
=cut
|
||||
@@ -0,0 +1,109 @@
|
||||
# --
|
||||
# Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
|
||||
# --
|
||||
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
# the enclosed file COPYING for license information (GPL). If you
|
||||
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
|
||||
# --
|
||||
|
||||
package Kernel::System::Daemon::DaemonModules::SchedulerTaskWorker::CalendarAppointment;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use parent qw(Kernel::System::Daemon::DaemonModules::BaseTaskWorker);
|
||||
|
||||
our @ObjectDependencies = (
|
||||
'Kernel::System::Log',
|
||||
'Kernel::System::Calendar::Appointment',
|
||||
);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Kernel::System::Daemon::DaemonModules::SchedulerTaskWorker::CalendarAppointment - Scheduler daemon task handler module for CalendarAppointment
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
This task handler executes calendar appointment jobs.
|
||||
|
||||
=head1 PUBLIC INTERFACE
|
||||
|
||||
=head2 new()
|
||||
|
||||
use Kernel::System::ObjectManager;
|
||||
local $Kernel::OM = Kernel::System::ObjectManager->new();
|
||||
my $TaskHandlerObject = $Kernel::OM-Get('Kernel::System::Daemon::DaemonModules::SchedulerTaskWorker::CalendarAppointment');
|
||||
|
||||
=cut
|
||||
|
||||
sub new {
|
||||
my ( $Type, %Param ) = @_;
|
||||
|
||||
my $Self = {};
|
||||
bless( $Self, $Type );
|
||||
|
||||
$Self->{Debug} = $Param{Debug};
|
||||
$Self->{WorkerName} = 'Worker: CalendarAppointment';
|
||||
|
||||
return $Self;
|
||||
}
|
||||
|
||||
=head2 Run()
|
||||
|
||||
performs the selected task.
|
||||
|
||||
my $Result = $TaskHandlerObject->Run(
|
||||
TaskID => 123,
|
||||
TaskName => 'some name', # optional
|
||||
Data => { # appointment id as got from Kernel::System::Calendar::Appointment::AppointmentGet()
|
||||
NotifyTime => '2016-08-02 03:59:00',
|
||||
},
|
||||
);
|
||||
|
||||
Returns:
|
||||
|
||||
$Result = 1; # or fail in case of an error
|
||||
|
||||
=cut
|
||||
|
||||
sub Run {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check task params
|
||||
my $CheckResult = $Self->_CheckTaskParams(
|
||||
%Param,
|
||||
NeededDataAttributes => ['NotifyTime'],
|
||||
);
|
||||
|
||||
# stop execution if an error in params is detected
|
||||
return if !$CheckResult;
|
||||
|
||||
if ( $Self->{Debug} ) {
|
||||
print " $Self->{WorkerName} executes task: $Param{TaskName}\n";
|
||||
}
|
||||
|
||||
# trigger the appointment notification
|
||||
my $Success
|
||||
= $Kernel::OM->Get('Kernel::System::Calendar::Appointment')->AppointmentNotification( %{ $Param{Data} } );
|
||||
|
||||
if ( !$Success ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Could not trigger appointment notification for AppointmentID $Param{Data}->{AppointmentID}!",
|
||||
);
|
||||
}
|
||||
|
||||
return $Success;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
=head1 TERMS AND CONDITIONS
|
||||
|
||||
This software is part of the OTRS project (L<https://otrs.org/>).
|
||||
|
||||
This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
the enclosed file COPYING for license information (GPL). If you
|
||||
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
|
||||
|
||||
=cut
|
||||
@@ -0,0 +1,205 @@
|
||||
# --
|
||||
# Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
|
||||
# --
|
||||
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
# the enclosed file COPYING for license information (GPL). If you
|
||||
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
|
||||
# --
|
||||
|
||||
package Kernel::System::Daemon::DaemonModules::SchedulerTaskWorker::Cron;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use IPC::Open3;
|
||||
use Symbol;
|
||||
|
||||
use parent qw(Kernel::System::Daemon::DaemonModules::BaseTaskWorker);
|
||||
|
||||
our @ObjectDependencies = (
|
||||
'Kernel::Config',
|
||||
'Kernel::System::Daemon::SchedulerDB',
|
||||
'Kernel::System::DateTime',
|
||||
'Kernel::System::Email',
|
||||
'Kernel::System::Log',
|
||||
);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Kernel::System::Daemon::DaemonModules::SchedulerTaskWorker::Cron - Scheduler daemon task handler module for cron like jobs
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
This task handler executes scheduler tasks based in cron notation.
|
||||
|
||||
=head1 PUBLIC INTERFACE
|
||||
|
||||
=head2 new()
|
||||
|
||||
my $TaskHandlerObject = $Kernel::OM-Get('Kernel::System::Daemon::DaemonModules::SchedulerTaskWorker::Cron');
|
||||
|
||||
=cut
|
||||
|
||||
sub new {
|
||||
my ( $Type, %Param ) = @_;
|
||||
|
||||
my $Self = {};
|
||||
bless( $Self, $Type );
|
||||
|
||||
$Self->{Debug} = $Param{Debug};
|
||||
$Self->{WorkerName} = 'Worker: Cron';
|
||||
|
||||
return $Self;
|
||||
}
|
||||
|
||||
=head2 Run()
|
||||
|
||||
Performs the selected Cron task.
|
||||
|
||||
my $Success = $TaskHandlerObject->Run(
|
||||
TaskID => 123,
|
||||
TaskName => 'some name', # optional
|
||||
Data => {
|
||||
Module => 'Kernel::System:::Console:Command::Help',
|
||||
Function => 'Execute',
|
||||
Params => [ # parameters array reference
|
||||
'--force',
|
||||
'--option',
|
||||
'my option',
|
||||
],
|
||||
},
|
||||
);
|
||||
|
||||
Returns:
|
||||
|
||||
$Success => 1, # or fail in case of an error
|
||||
|
||||
=cut
|
||||
|
||||
sub Run {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# Check task params.
|
||||
my $CheckResult = $Self->_CheckTaskParams(
|
||||
%Param,
|
||||
NeededDataAttributes => [ 'Module', 'Function' ],
|
||||
DataParamsRef => 'ARRAY',
|
||||
);
|
||||
|
||||
# Stop execution if an error in params is detected.
|
||||
return if !$CheckResult;
|
||||
|
||||
my $StartSystemTime = $Kernel::OM->Create('Kernel::System::DateTime')->ToEpoch();
|
||||
|
||||
my $ModuleObject;
|
||||
eval {
|
||||
$ModuleObject = $Kernel::OM->Get( $Param{Data}->{Module} );
|
||||
};
|
||||
if ( !$ModuleObject ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Can not create a new Object for Module: '$Param{Data}->{Module}'! - Task: $Param{TaskName}",
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
my $Function = $Param{Data}->{Function};
|
||||
|
||||
# Check if the module provide the required function.
|
||||
return if !$ModuleObject->can($Function);
|
||||
|
||||
my @Parameters = @{ $Param{Data}->{Params} || [] };
|
||||
|
||||
# To capture the standard error.
|
||||
my $ErrorMessage;
|
||||
|
||||
my $Result;
|
||||
|
||||
if ( $Self->{Debug} ) {
|
||||
print " $Self->{WorkerName} Executes task: $Param{TaskName}\n";
|
||||
}
|
||||
|
||||
eval {
|
||||
|
||||
# Restore child signal to default, main daemon set it to 'IGNORE' to be able to create
|
||||
# multiple process at the same time, but in workers this causes problems if function does
|
||||
# system calls (on linux), since system calls returns -1. See bug#12126.
|
||||
local $SIG{CHLD} = 'DEFAULT';
|
||||
|
||||
# Localize the standard error, everything will be restored after the eval block.
|
||||
local *STDERR;
|
||||
|
||||
# Redirect the standard error to a variable.
|
||||
open STDERR, ">>", \$ErrorMessage;
|
||||
|
||||
# Disable ANSI terminal colors for console commands, then in case of an error the output
|
||||
# will be clean.
|
||||
# Prevent used once warning, setting the variable as local and then assign the value
|
||||
# in the next statement.
|
||||
local $Kernel::System::Console::BaseCommand::SuppressANSI;
|
||||
$Kernel::System::Console::BaseCommand::SuppressANSI = 1;
|
||||
|
||||
# Run function on the module with the specified parameters in Data->{Params}
|
||||
$Result = $ModuleObject->$Function(
|
||||
@Parameters,
|
||||
);
|
||||
};
|
||||
|
||||
# Get current system time (as soon as the method has been called).
|
||||
my $EndSystemTime = $Kernel::OM->Create('Kernel::System::DateTime')->ToEpoch();
|
||||
|
||||
my $IsConsoleCommand;
|
||||
if (
|
||||
substr( $Param{Data}->{Module}, 0, length 'Kernel::System::Console' ) eq 'Kernel::System::Console'
|
||||
&& $Function eq 'Execute'
|
||||
)
|
||||
{
|
||||
$IsConsoleCommand = 1;
|
||||
}
|
||||
|
||||
my $ConsoleCommandFailure;
|
||||
|
||||
# Console commands send 1 as result if fail.
|
||||
if ( $IsConsoleCommand && $Result ) {
|
||||
$ConsoleCommandFailure = 1;
|
||||
}
|
||||
|
||||
my $Success = 1;
|
||||
|
||||
# Check if there are errors.
|
||||
if ( $ErrorMessage || $ConsoleCommandFailure ) {
|
||||
|
||||
$ErrorMessage //= "Console command '$Param{TaskName}' is failed.";
|
||||
|
||||
$Self->_HandleError(
|
||||
TaskName => $Param{TaskName},
|
||||
TaskType => 'Cron',
|
||||
LogMessage => "There was an error executing $Function() in $Param{Data}->{Module}: $ErrorMessage",
|
||||
ErrorMessage => $ErrorMessage,
|
||||
);
|
||||
|
||||
$Success = 0;
|
||||
}
|
||||
|
||||
# Update worker task.
|
||||
$Kernel::OM->Get('Kernel::System::Daemon::SchedulerDB')->RecurrentTaskWorkerInfoSet(
|
||||
LastWorkerTaskID => $Param{TaskID},
|
||||
LastWorkerStatus => $Success,
|
||||
LastWorkerRunningTime => $EndSystemTime - $StartSystemTime,
|
||||
);
|
||||
|
||||
return $Success;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
=head1 TERMS AND CONDITIONS
|
||||
|
||||
This software is part of the OTRS project (L<https://otrs.org/>).
|
||||
|
||||
This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
the enclosed file COPYING for license information (GPL). If you
|
||||
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
|
||||
|
||||
=cut
|
||||
@@ -0,0 +1,172 @@
|
||||
# --
|
||||
# Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
|
||||
# --
|
||||
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
# the enclosed file COPYING for license information (GPL). If you
|
||||
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
|
||||
# --
|
||||
|
||||
package Kernel::System::Daemon::DaemonModules::SchedulerTaskWorker::GenericAgent;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use parent qw(Kernel::System::Daemon::DaemonModules::BaseTaskWorker);
|
||||
|
||||
our @ObjectDependencies = (
|
||||
'Kernel::Config',
|
||||
'Kernel::System::Daemon::SchedulerDB',
|
||||
'Kernel::System::DateTime',
|
||||
'Kernel::System::GenericAgent',
|
||||
'Kernel::System::Log',
|
||||
);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Kernel::System::Daemon::DaemonModules::SchedulerTaskWorker::GenericAgent - Scheduler daemon task handler module for GenericAgent
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
This task handler executes generic agent jobs
|
||||
|
||||
=head1 PUBLIC INTERFACE
|
||||
|
||||
=head2 new()
|
||||
|
||||
my $TaskHandlerObject = $Kernel::OM-Get('Kernel::System::Daemon::DaemonModules::SchedulerTaskWorker::GenericAgent');
|
||||
|
||||
=cut
|
||||
|
||||
sub new {
|
||||
my ( $Type, %Param ) = @_;
|
||||
|
||||
my $Self = {};
|
||||
bless( $Self, $Type );
|
||||
|
||||
$Self->{Debug} = $Param{Debug};
|
||||
$Self->{WorkerName} = 'Worker: GenericAgent';
|
||||
|
||||
return $Self;
|
||||
}
|
||||
|
||||
=head2 Run()
|
||||
|
||||
Performs the selected task.
|
||||
|
||||
my $Result = $TaskHandlerObject->Run(
|
||||
TaskID => 123,
|
||||
TaskName => 'some name', # optional
|
||||
Data => { # job data as got from Kernel::System::GenericAgent::JobGet()
|
||||
Name 'job name',
|
||||
Valid 1,
|
||||
# ...
|
||||
},
|
||||
);
|
||||
|
||||
Returns:
|
||||
|
||||
$Result = 1; # or fail in case of an error
|
||||
|
||||
=cut
|
||||
|
||||
sub Run {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check task params
|
||||
my $CheckResult = $Self->_CheckTaskParams(
|
||||
%Param,
|
||||
NeededDataAttributes => [ 'Name', 'Valid' ],
|
||||
);
|
||||
|
||||
# Stop execution if an error in params is detected.
|
||||
return if !$CheckResult;
|
||||
|
||||
# Skip if job is not valid.
|
||||
return if !$Param{Data}->{Valid};
|
||||
|
||||
my %Job = %{ $Param{Data} };
|
||||
|
||||
my $StartSystemTime = $Kernel::OM->Create('Kernel::System::DateTime')->ToEpoch();
|
||||
|
||||
# Check if last run was less than 1 minute ago.
|
||||
if (
|
||||
$Job{ScheduleLastRunUnixTime}
|
||||
&& $StartSystemTime - $Job{ScheduleLastRunUnixTime} < 60
|
||||
)
|
||||
{
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "GenericAgent Job: $Job{Name}, was already executed less than 1 minute ago!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
|
||||
|
||||
my $TicketLimit = $ConfigObject->Get('Daemon::SchedulerGenericAgentTaskManager::TicketLimit') || 0;
|
||||
my $SleepTime = $ConfigObject->Get('Daemon::SchedulerGenericAgentTaskManager::SleepTime') || 0;
|
||||
|
||||
my $Success;
|
||||
my $ErrorMessage;
|
||||
|
||||
if ( $Self->{Debug} ) {
|
||||
print " $Self->{WorkerName} executes task: $Param{TaskName}\n";
|
||||
}
|
||||
|
||||
do {
|
||||
|
||||
# Restore child signal to default, main daemon set it to 'IGNORE' to be able to create
|
||||
# multiple process at the same time, but in workers this causes problems if function does
|
||||
# system calls (on linux), since system calls returns -1. See bug#12126.
|
||||
local $SIG{CHLD} = 'DEFAULT';
|
||||
|
||||
# Localize the standard error, everything will be restored after the eval block.
|
||||
local *STDERR;
|
||||
|
||||
# Redirect the standard error to a variable.
|
||||
open STDERR, ">>", \$ErrorMessage;
|
||||
|
||||
$Success = $Kernel::OM->Get('Kernel::System::GenericAgent')->JobRun(
|
||||
Job => $Job{Name},
|
||||
Limit => $TicketLimit,
|
||||
SleepTime => $SleepTime,
|
||||
UserID => 1,
|
||||
);
|
||||
};
|
||||
|
||||
# Get current system time (as soon as the job finish to run).
|
||||
my $EndSystemTime = $Kernel::OM->Create('Kernel::System::DateTime')->ToEpoch();
|
||||
|
||||
if ( !$Success ) {
|
||||
|
||||
$ErrorMessage ||= "$Job{Name} execution failed without an error message!";
|
||||
|
||||
$Self->_HandleError(
|
||||
TaskName => $Job{Name},
|
||||
TaskType => 'GenericAgent',
|
||||
LogMessage => "There was an error executing $Job{Name}: $ErrorMessage",
|
||||
ErrorMessage => "$ErrorMessage",
|
||||
);
|
||||
}
|
||||
|
||||
# Update worker task.
|
||||
$Kernel::OM->Get('Kernel::System::Daemon::SchedulerDB')->RecurrentTaskWorkerInfoSet(
|
||||
LastWorkerTaskID => $Param{TaskID},
|
||||
LastWorkerStatus => $Success,
|
||||
LastWorkerRunningTime => $EndSystemTime - $StartSystemTime,
|
||||
);
|
||||
|
||||
return $Success;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
=head1 TERMS AND CONDITIONS
|
||||
|
||||
This software is part of the OTRS project (L<https://otrs.org/>).
|
||||
|
||||
This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
the enclosed file COPYING for license information (GPL). If you
|
||||
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
|
||||
|
||||
=cut
|
||||
@@ -0,0 +1,200 @@
|
||||
# --
|
||||
# Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
|
||||
# --
|
||||
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
# the enclosed file COPYING for license information (GPL). If you
|
||||
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
|
||||
# --
|
||||
|
||||
package Kernel::System::Daemon::DaemonModules::SchedulerTaskWorker::GenericInterface;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use parent qw(Kernel::System::Daemon::DaemonModules::BaseTaskWorker);
|
||||
|
||||
use Kernel::System::VariableCheck qw(:all);
|
||||
|
||||
use Storable;
|
||||
|
||||
our @ObjectDependencies = (
|
||||
'Kernel::Config',
|
||||
'Kernel::GenericInterface::Requester',
|
||||
'Kernel::System::DateTime',
|
||||
'Kernel::System::GenericInterface::Webservice',
|
||||
'Kernel::System::Log',
|
||||
'Kernel::System::Scheduler',
|
||||
);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Kernel::System::Daemon::DaemonModules::SchedulerTaskWorker::GenericInterface - Scheduler daemon task handler module for GenericInterface
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
This task handler executes scheduler tasks delegated by asynchronous invoker configuration
|
||||
|
||||
=head1 PUBLIC INTERFACE
|
||||
|
||||
=head2 new()
|
||||
|
||||
my $TaskHandlerObject = $Kernel::OM-Get('Kernel::System::Daemon::DaemonModules::SchedulerTaskWorker::GenericInterface');
|
||||
|
||||
=cut
|
||||
|
||||
sub new {
|
||||
my ( $Type, %Param ) = @_;
|
||||
|
||||
my $Self = {};
|
||||
bless( $Self, $Type );
|
||||
|
||||
$Self->{Debug} = $Param{Debug};
|
||||
$Self->{WorkerName} = 'Worker: GenericInterface';
|
||||
|
||||
return $Self;
|
||||
}
|
||||
|
||||
=head2 Run()
|
||||
|
||||
Performs the selected Task, causing an Invoker call via GenericInterface.
|
||||
|
||||
my $Result = $TaskHandlerObject->Run(
|
||||
TaskID => 123,
|
||||
TaskName => 'some name', # optional
|
||||
Data => {
|
||||
WebserviceID => $WebserviceID,
|
||||
Invoker => 'configured_invoker',
|
||||
Data => { # data payload for the Invoker
|
||||
...
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
Returns:
|
||||
|
||||
$Result = 1; # or fail in case of an error
|
||||
|
||||
=cut
|
||||
|
||||
sub Run {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# Check task params.
|
||||
my $CheckResult = $Self->_CheckTaskParams(
|
||||
NeededDataAttributes => [ 'WebserviceID', 'Invoker', 'Data' ],
|
||||
%Param,
|
||||
);
|
||||
return if !$CheckResult;
|
||||
|
||||
if ( $Self->{Debug} ) {
|
||||
print " $Self->{WorkerName} executes task: $Param{TaskName}\n";
|
||||
}
|
||||
|
||||
my $Result = $Kernel::OM->Get('Kernel::GenericInterface::Requester')->Run(
|
||||
WebserviceID => $Param{Data}->{WebserviceID},
|
||||
Invoker => $Param{Data}->{Invoker},
|
||||
Asynchronous => 1,
|
||||
Data => Storable::dclone( $Param{Data}->{Data} ),
|
||||
PastExecutionData => $Param{Data}->{PastExecutionData},
|
||||
);
|
||||
return 1 if $Result->{Success};
|
||||
|
||||
my $Webservice = $Kernel::OM->Get('Kernel::System::GenericInterface::Webservice')->WebserviceGet(
|
||||
ID => $Param{Data}->{WebserviceID},
|
||||
);
|
||||
|
||||
my $WebServiceName = $Webservice->{Name} // 'N/A';
|
||||
|
||||
# No further retries for request.
|
||||
if (
|
||||
!IsHashRefWithData( $Result->{Data} )
|
||||
|| !$Result->{Data}->{ReSchedule}
|
||||
)
|
||||
{
|
||||
my $ErrorMessage
|
||||
= $Result->{ErrorMessage} || "$Param{Data}->{Invoker} execution failed without an error message";
|
||||
|
||||
$Self->_HandleError(
|
||||
TaskName => "$Param{Data}->{Invoker} WebService: $WebServiceName",
|
||||
TaskType => 'GenericInterface',
|
||||
LogMessage => "There was an error executing $Param{Data}->{Invoker} ($WebServiceName): $ErrorMessage",
|
||||
ErrorMessage => "$ErrorMessage",
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
# Schedule request for another try.
|
||||
|
||||
# Use the execution time from the return data (if any).
|
||||
my $ExecutionTime = $Result->{Data}->{ExecutionTime};
|
||||
my $ExecutionDateTime;
|
||||
|
||||
# Check if execution time is valid.
|
||||
if ( IsStringWithData($ExecutionTime) ) {
|
||||
|
||||
$ExecutionDateTime = $Kernel::OM->Create(
|
||||
'Kernel::System::DateTime',
|
||||
ObjectParams => {
|
||||
String => $ExecutionTime,
|
||||
},
|
||||
);
|
||||
if ( !$ExecutionDateTime ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message =>
|
||||
"WebService $WebServiceName, Invoker $Param{Data}->{Invoker} returned invalid execution time $ExecutionTime. Falling back to default!",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
# Set default execution time.
|
||||
if ( !$ExecutionTime || !$ExecutionDateTime ) {
|
||||
|
||||
# Get default time difference from config.
|
||||
my $FutureTaskTimeDiff = int(
|
||||
$Kernel::OM->Get('Kernel::Config')->Get('Daemon::SchedulerGenericInterfaceTaskManager::FutureTaskTimeDiff')
|
||||
)
|
||||
|| 300;
|
||||
|
||||
$ExecutionDateTime = $Kernel::OM->Create('Kernel::System::DateTime');
|
||||
$ExecutionDateTime->Add( Seconds => $FutureTaskTimeDiff );
|
||||
}
|
||||
|
||||
if ( $Self->{Debug} ) {
|
||||
print " $Self->{WorkerName} re-schedule task: $Param{TaskName} for: $ExecutionDateTime->ToString()\n";
|
||||
}
|
||||
|
||||
# Create a new task (replica) that will be executed in the future.
|
||||
my $Success = $Kernel::OM->Get('Kernel::System::Scheduler')->TaskAdd(
|
||||
ExecutionTime => $ExecutionDateTime->ToString(),
|
||||
Type => 'GenericInterface',
|
||||
Name => $Param{TaskName},
|
||||
Attempts => 10,
|
||||
Data => {
|
||||
%{ $Param{Data} },
|
||||
PastExecutionData => $Result->{Data}->{PastExecutionData},
|
||||
},
|
||||
);
|
||||
|
||||
if ( !$Success ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "Could not re-schedule a task in future for task $Param{TaskName}",
|
||||
);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
=head1 TERMS AND CONDITIONS
|
||||
|
||||
This software is part of the OTRS project (L<https://otrs.org/>).
|
||||
|
||||
This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
the enclosed file COPYING for license information (GPL). If you
|
||||
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
|
||||
|
||||
=cut
|
||||
@@ -0,0 +1,257 @@
|
||||
# --
|
||||
# Copyright (C) 2001-2019 OTRS AG, https://otrs.com/
|
||||
# --
|
||||
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
# the enclosed file COPYING for license information (GPL). If you
|
||||
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
|
||||
# --
|
||||
|
||||
package Kernel::System::Daemon::DaemonModules::SystemConfigurationSyncManager;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use utf8;
|
||||
|
||||
use File::Basename qw(basename);
|
||||
use Kernel::System::VariableCheck qw(:all);
|
||||
|
||||
use parent qw(Kernel::System::Daemon::BaseDaemon Kernel::System::Daemon::DaemonModules::BaseTaskWorker);
|
||||
|
||||
our @ObjectDependencies = (
|
||||
'Kernel::Config',
|
||||
'Kernel::System::DB',
|
||||
'Kernel::System::Cache',
|
||||
'Kernel::System::Log',
|
||||
'Kernel::System::Main',
|
||||
'Kernel::System::SysConfig',
|
||||
);
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Kernel::System::Daemon::DaemonModules::SystemConfigurationSyncManager - daemon to keep system configuration deployments in sync
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
System Configuration deployment sync daemon
|
||||
|
||||
=head1 PUBLIC INTERFACE
|
||||
|
||||
=head2 new()
|
||||
|
||||
Create system configuration deployment sync object.
|
||||
|
||||
=cut
|
||||
|
||||
sub new {
|
||||
my ( $Type, %Param ) = @_;
|
||||
|
||||
# Allocate new hash for object.
|
||||
my $Self = {};
|
||||
bless $Self, $Type;
|
||||
|
||||
# Get objects in constructor to save performance.
|
||||
$Self->{ConfigObject} = $Kernel::OM->Get('Kernel::Config');
|
||||
$Self->{CacheObject} = $Kernel::OM->Get('Kernel::System::Cache');
|
||||
$Self->{DBObject} = $Kernel::OM->Get('Kernel::System::DB');
|
||||
$Self->{SysConfigObject} = $Kernel::OM->Get('Kernel::System::SysConfig');
|
||||
$Self->{MainObject} = $Kernel::OM->Get('Kernel::System::Main');
|
||||
|
||||
# Disable in memory cache to be clusterable.
|
||||
$Self->{CacheObject}->Configure(
|
||||
CacheInMemory => 0,
|
||||
CacheInBackend => 1,
|
||||
);
|
||||
|
||||
# Get the NodeID from the SysConfig settings, this is used on High Availability systems.
|
||||
$Self->{NodeID} = $Self->{ConfigObject}->Get('NodeID') || 1;
|
||||
|
||||
# Check NodeID, if does not match is impossible to continue.
|
||||
if ( $Self->{NodeID} !~ m{ \A \d+ \z }xms && $Self->{NodeID} > 0 && $Self->{NodeID} < 1000 ) {
|
||||
$Kernel::OM->Get('Kernel::System::Log')->Log(
|
||||
Priority => 'error',
|
||||
Message => "NodeID '$Self->{NodeID}' is invalid!",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
# Do not change the following values!
|
||||
$Self->{SleepPost} = 60; # sleep 1 minute after each loop
|
||||
$Self->{Discard} = 60 * 60; # discard every hour
|
||||
|
||||
$Self->{DiscardCount} = $Self->{Discard} / $Self->{SleepPost};
|
||||
|
||||
$Self->{Debug} = $Param{Debug};
|
||||
$Self->{DaemonName} = 'Daemon: SystemConfigurationSyncManager';
|
||||
|
||||
return $Self;
|
||||
}
|
||||
|
||||
sub PreRun {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# Check if database is on-line.
|
||||
return 1 if $Self->{DBObject}->Ping();
|
||||
|
||||
sleep 10;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sub Run {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
$Kernel::OM->ObjectsDiscard(
|
||||
Objects => [ 'Kernel::Config', ],
|
||||
);
|
||||
|
||||
my $OldDeploymentID = $Kernel::OM->Get('Kernel::Config')->Get('CurrentDeploymentID') || 0;
|
||||
|
||||
# Execute the deployment sync
|
||||
my $ErrorMessage;
|
||||
my $Success;
|
||||
if ( $Self->{Debug} ) {
|
||||
print " $Self->{DaemonName} Executes function: ConfigurationDeploySync\n";
|
||||
}
|
||||
|
||||
eval {
|
||||
|
||||
# Restore child signal to default, main daemon set it to 'IGNORE' to be able to create
|
||||
# multiple process at the same time, but in workers this causes problems if function does
|
||||
# system calls (on linux), since system calls returns -1. See bug#12126.
|
||||
local $SIG{CHLD} = 'DEFAULT';
|
||||
|
||||
# Localize the standard error, everything will be restored after the eval block.
|
||||
local *STDERR;
|
||||
|
||||
# Redirect the standard error to a variable.
|
||||
open STDERR, ">>", \$ErrorMessage;
|
||||
|
||||
$Success = $Self->{SysConfigObject}->ConfigurationDeploySync();
|
||||
};
|
||||
|
||||
# Check if there are errors.
|
||||
if ( $ErrorMessage || !$Success ) {
|
||||
$Self->_HandleError(
|
||||
TaskName => 'ConfigurationDeploySync',
|
||||
TaskType => 'SystemConfigurationSyncManager',
|
||||
LogMessage => "There was an error executing ConfigurationDeploySync: $ErrorMessage",
|
||||
ErrorMessage => $ErrorMessage || 'ConfigurationDeploySync returns failure.',
|
||||
);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
$Kernel::OM->ObjectsDiscard(
|
||||
Objects => [ 'Kernel::Config', ],
|
||||
);
|
||||
|
||||
my $NewDeploymentID = $Kernel::OM->Get('Kernel::Config')->Get('CurrentDeploymentID') || 0;
|
||||
|
||||
my $ConfigChange;
|
||||
if ( $OldDeploymentID ne $NewDeploymentID ) {
|
||||
$ConfigChange = 1;
|
||||
}
|
||||
|
||||
my $ConfigDirectory = $Kernel::OM->Get('Kernel::Config')->Get('Home') . '/Kernel/Config/Files';
|
||||
my %KnownConfigFilesMD5Sum = %{ $Self->{ConfigFilesMD5Sum} // {} };
|
||||
my @ChangedFiles;
|
||||
|
||||
# If there is no record for new config files let it run
|
||||
my $InitialRun;
|
||||
if ( !defined $Self->{ConfigFilesMD5Sum} ) {
|
||||
$InitialRun = 1;
|
||||
}
|
||||
|
||||
# Check all (perl) config files for changes.
|
||||
my @ConfigFiles = $Self->{MainObject}->DirectoryRead(
|
||||
Directory => $ConfigDirectory,
|
||||
Filter => '*.pm',
|
||||
);
|
||||
|
||||
my $ConfigFileChanged;
|
||||
my %NewConfigFilesMD5Sum;
|
||||
FILE:
|
||||
for my $File (@ConfigFiles) {
|
||||
my $Basename = File::Basename::basename($File);
|
||||
|
||||
# Skip deployment based files.
|
||||
next FILE if $Basename eq 'ZZZAAuto.pm';
|
||||
|
||||
# Check MD5 against (potentially) stored value.
|
||||
my $KnownMD5Sum = delete $KnownConfigFilesMD5Sum{$Basename};
|
||||
$NewConfigFilesMD5Sum{$Basename} = $Self->{MainObject}->MD5sum(
|
||||
Filename => $File,
|
||||
);
|
||||
if (
|
||||
!$InitialRun
|
||||
&& (
|
||||
!$KnownMD5Sum || $KnownMD5Sum ne $NewConfigFilesMD5Sum{$Basename}
|
||||
)
|
||||
)
|
||||
{
|
||||
$ConfigFileChanged = 1;
|
||||
push @ChangedFiles, $Basename;
|
||||
}
|
||||
}
|
||||
|
||||
# Check for missing files.
|
||||
if ( scalar keys %KnownConfigFilesMD5Sum ) {
|
||||
$ConfigFileChanged = 1;
|
||||
}
|
||||
|
||||
$Self->{ConfigFilesMD5Sum} = \%NewConfigFilesMD5Sum;
|
||||
|
||||
# If there was no change in the configuration, do nothing and return gracefully.
|
||||
return 1 if ( !$ConfigChange && !$ConfigFileChanged );
|
||||
|
||||
# Stop all daemons and reload configuration from main daemon.
|
||||
kill 1, getppid;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub PostRun {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
sleep $Self->{SleepPost};
|
||||
|
||||
$Self->{DiscardCount}--;
|
||||
|
||||
if ( $Self->{Debug} && $Self->{DiscardCount} == 0 ) {
|
||||
print " $Self->{DaemonName} will be stopped and set for restart!\n";
|
||||
}
|
||||
|
||||
return if $Self->{DiscardCount} <= 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub Summary {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
return (
|
||||
{
|
||||
Header => "System configuration sync:",
|
||||
Column => [],
|
||||
Data => [],
|
||||
NoDataMessage => "Daemon is active.",
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
sub DESTROY {
|
||||
my $Self = shift;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
=head1 TERMS AND CONDITIONS
|
||||
|
||||
This software is part of the OTRS project (L<https://otrs.org/>).
|
||||
|
||||
This software comes with ABSOLUTELY NO WARRANTY. For details, see
|
||||
the enclosed file COPYING for license information (GPL). If you
|
||||
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
|
||||
|
||||
=cut
|
||||
|
||||
1;
|
||||
Reference in New Issue
Block a user