1# --
2# Copyright (C) 2001-2020 OTRS AG, https://otrs.com/
3# --
4# This software comes with ABSOLUTELY NO WARRANTY. For details, see
5# the enclosed file COPYING for license information (GPL). If you
6# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
7# --
8
9package Kernel::System::Daemon::DaemonModules::SchedulerFutureTaskManager;
10
11use strict;
12use warnings;
13use utf8;
14
15use parent qw(Kernel::System::Daemon::BaseDaemon);
16
17our @ObjectDependencies = (
18    'Kernel::Config',
19    'Kernel::System::DB',
20    'Kernel::System::Daemon::SchedulerDB',
21    'Kernel::System::Cache',
22    'Kernel::System::Log',
23);
24
25=head1 NAME
26
27Kernel::System::Daemon::DaemonModules::SchedulerFutureTaskManager - daemon to manage scheduler future tasks
28
29=head1 DESCRIPTION
30
31Scheduler future task daemon
32
33=head1 PUBLIC INTERFACE
34
35=head2 new()
36
37Create scheduler future task manager object.
38
39=cut
40
41sub new {
42    my ( $Type, %Param ) = @_;
43
44    # Allocate new hash for object.
45    my $Self = {};
46    bless $Self, $Type;
47
48    # Get objects in constructor to save performance.
49    $Self->{ConfigObject}      = $Kernel::OM->Get('Kernel::Config');
50    $Self->{CacheObject}       = $Kernel::OM->Get('Kernel::System::Cache');
51    $Self->{DBObject}          = $Kernel::OM->Get('Kernel::System::DB');
52    $Self->{SchedulerDBObject} = $Kernel::OM->Get('Kernel::System::Daemon::SchedulerDB');
53
54    # Disable in memory cache to be clusterable.
55    $Self->{CacheObject}->Configure(
56        CacheInMemory  => 0,
57        CacheInBackend => 1,
58    );
59
60    # Get the NodeID from the SysConfig settings, this is used on High Availability systems.
61    $Self->{NodeID} = $Self->{ConfigObject}->Get('NodeID') || 1;
62
63    # Check NodeID, if does not match is impossible to continue.
64    if ( $Self->{NodeID} !~ m{ \A \d+ \z }xms && $Self->{NodeID} > 0 && $Self->{NodeID} < 1000 ) {
65        $Kernel::OM->Get('Kernel::System::Log')->Log(
66            Priority => 'error',
67            Message  => "NodeID '$Self->{NodeID}' is invalid!",
68        );
69        return;
70    }
71
72    # Do not change the following values!
73    # Modulo in PreRun() can be damaged after a change.
74    $Self->{SleepPost} = 1;          # sleep 1 second after each loop
75    $Self->{Discard}   = 60 * 60;    # discard every hour
76
77    $Self->{DiscardCount} = $Self->{Discard} / $Self->{SleepPost};
78
79    $Self->{Debug}      = $Param{Debug};
80    $Self->{DaemonName} = 'Daemon: SchedulerFutureTaskManager';
81
82    return $Self;
83}
84
85sub PreRun {
86    my ( $Self, %Param ) = @_;
87
88    # Check the database connection each 10 seconds.
89    return 1 if $Self->{DiscardCount} % ( 10 / $Self->{SleepPost} );
90
91    # Check if database is on-line.
92    return 1 if $Self->{DBObject}->Ping();
93
94    sleep 10;
95
96    return;
97}
98
99sub Run {
100    my ( $Self, %Param ) = @_;
101
102    return if !$Self->{SchedulerDBObject}->FutureTaskToExecute(
103        NodeID => $Self->{NodeID},
104        PID    => $$,
105    );
106
107    return 1;
108}
109
110sub PostRun {
111    my ( $Self, %Param ) = @_;
112
113    sleep $Self->{SleepPost};
114
115    $Self->{DiscardCount}--;
116
117    if ( $Self->{Debug} && $Self->{DiscardCount} == 0 ) {
118        print "  $Self->{DaemonName} will be stopped and set for restart!\n";
119    }
120
121    return if $Self->{DiscardCount} <= 0;
122    return 1;
123}
124
125sub Summary {
126    my ( $Self, %Param ) = @_;
127
128    return $Self->{SchedulerDBObject}->FutureTaskSummary();
129}
130
131sub DESTROY {
132    my $Self = shift;
133
134    return 1;
135}
136
1371;
138
139=head1 TERMS AND CONDITIONS
140
141This software is part of the OTRS project (L<https://otrs.org/>).
142
143This software comes with ABSOLUTELY NO WARRANTY. For details, see
144the enclosed file COPYING for license information (GPL). If you
145did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
146
147=cut
148