/
BaseTaskWorker.pm
160 lines (117 loc) · 4.54 KB
/
BaseTaskWorker.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# Copyright (C) 2021-2022 Znuny GmbH, https://znuny.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 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