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::Scheduler;
10
11use strict;
12use warnings;
13
14our @ObjectDependencies = (
15    'Kernel::System::Daemon::SchedulerDB',
16    'Kernel::System::Log',
17);
18
19=head1 NAME
20
21Kernel::System::Scheduler - Scheduler lib
22
23=head1 DESCRIPTION
24
25Includes the functions to add a new task to the scheduler daemon.
26
27=head1 PUBLIC INTERFACE
28
29=head2 new()
30
31create a scheduler object. Do not use it directly, instead use:
32
33    my $SchedulerObject = $Kernel::OM->Get('Kernel::System::Scheduler');
34
35=cut
36
37sub new {
38    my ( $Type, %Param ) = @_;
39
40    # allocate new hash for object
41    my $Self = {};
42    bless( $Self, $Type );
43
44    return $Self;
45}
46
47=head2 TaskAdd()
48
49add a task to scheduler
50
51    my $Success = $SchedulerObject->TaskAdd(
52        ExecutionTime            => '2015-01-01 00:00:00',  # task will be executed immediately if no execution
53                                                            #   time is given
54        Type                     => 'GenericInterface',     # e. g. GenericInterface, Test
55        Name                     => 'any name',             # optional
56        Attempts                 => 5,                      # optional (default 1)
57        MaximumParallelInstances => 2,                      # optional, number of tasks with the same type
58                                                            #   (and name if provided) that can exists at
59                                                            #   the same time, value of 0 means unlimited
60        Data => {                                           # data payload
61            ...
62        },
63    );
64
65=cut
66
67sub TaskAdd {
68    my ( $Self, %Param ) = @_;
69
70    # check needed stuff
71    for my $Key (qw(Type Data)) {
72        if ( !$Param{$Key} ) {
73            $Kernel::OM->Get('Kernel::System::Log')->Log(
74                Priority => 'error',
75                Message  => "Need $Key!",
76            );
77
78            return;
79        }
80    }
81
82    # get scheduler database object
83    my $SchedulerDBObject = $Kernel::OM->Get('Kernel::System::Daemon::SchedulerDB');
84
85    my $TaskID;
86    if ( $Param{ExecutionTime} ) {
87        $TaskID = $SchedulerDBObject->FutureTaskAdd(%Param);
88    }
89    else {
90        $TaskID = $SchedulerDBObject->TaskAdd(%Param);
91    }
92
93    return 1 if $TaskID;
94    return;
95}
96
97=head2 FutureTaskList()
98
99get the list of scheduler future tasks
100
101    my @List = $SchedulerObject->FutureTaskList(
102        Type => 'some type',  # optional
103    );
104
105Returns:
106
107    @List = (
108        {
109            TaskID        => 123,
110            ExecutionTime => '2015-01-01 00:00:00',
111            Name          => 'any name',
112            Type          => 'GenericInterface',
113        },
114        {
115            TaskID        => 456,
116            ExecutionTime => '2015-01-01 00:00:00',
117            Name          => 'any other name',
118            Type          => 'GenericInterface',
119        },
120        # ...
121    );
122
123=cut
124
125sub FutureTaskList {
126    my ( $Self, %Param ) = @_;
127
128    my @List = $Kernel::OM->Get('Kernel::System::Daemon::SchedulerDB')->FutureTaskList(%Param);
129
130    return @List;
131}
132
133=head2 TaskList()
134
135get the list of scheduler tasks
136
137    my @List = $SchedulerObject->TaskList(
138        Type => 'some type',  # optional
139    );
140
141Returns:
142
143    @List = (
144        {
145            TaskID => 123,
146            Name   => 'any name',
147            Type   => 'GenericInterface',
148        },
149        {
150            TaskID => 456,
151            Name   => 'any other name',
152            Type   => 'GenericInterface',
153        },
154        # ...
155    );
156
157=cut
158
159sub TaskList {
160    my ( $Self, %Param ) = @_;
161
162    return $Kernel::OM->Get('Kernel::System::Daemon::SchedulerDB')->TaskList(%Param);
163}
164
165=head2 FutureTaskDelete()
166
167delete a task from scheduler future task list
168
169    my $Success = $Schedulerbject->FutureTaskDelete(
170        TaskID => 123,
171    );
172
173=cut
174
175sub FutureTaskDelete {
176    my ( $Self, %Param ) = @_;
177
178    # check needed stuff
179    if ( !$Param{TaskID} ) {
180        $Kernel::OM->Get('Kernel::System::Log')->Log(
181            Priority => 'error',
182            Message  => 'Need TaskID!',
183        );
184        return;
185    }
186
187    my $Success = $Kernel::OM->Get('Kernel::System::Daemon::SchedulerDB')->FutureTaskDelete(%Param);
188
189    return $Success;
190}
191
1921;
193
194=head1 TERMS AND CONDITIONS
195
196This software is part of the OTRS project (L<https://otrs.org/>).
197
198This software comes with ABSOLUTELY NO WARRANTY. For details, see
199the enclosed file COPYING for license information (GPL). If you
200did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
201
202=cut
203