File: Run.pm

package info (click to toggle)
otrs2 6.0.32-6
  • links: PTS
  • area: non-free
  • in suites: bullseye
  • size: 197,336 kB
  • sloc: perl: 1,003,018; javascript: 75,060; xml: 70,883; php: 51,819; sql: 22,361; sh: 379; makefile: 51
file content (168 lines) | stat: -rw-r--r-- 4,814 bytes parent folder | download
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
161
162
163
164
165
166
167
168
# --
# Copyright (C) 2001-2021 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::Console::Command::Maint::GenericAgent::Run;

use strict;
use warnings;

use vars qw(%Jobs);

use parent qw(Kernel::System::Console::BaseCommand);

our @ObjectDependencies = (
    'Kernel::System::Cache',
    'Kernel::System::GenericAgent',
    'Kernel::System::Main',
    'Kernel::System::PID',
);

sub Configure {
    my ( $Self, %Param ) = @_;

    $Self->Description('Run all generic agent jobs from a configuration file.');
    $Self->AddOption(
        Name => 'configuration-module',
        Description =>
            "Specify the name of the generic agent configuration module (e.g. 'Kernel::System::GenericAgent')",
        Required   => 1,
        HasValue   => 1,
        ValueRegex => qr/.*/smx,
    );
    $Self->AddOption(
        Name        => 'ticket-limit',
        Description => "Maximum number of tickets to process per job.",
        Required    => 0,
        HasValue    => 1,
        ValueRegex  => qr/\d+/smx,
    );
    $Self->AddOption(
        Name        => 'force-pid',
        Description => "Start even if another process is still registered in the database.",
        Required    => 0,
        HasValue    => 0,
    );
    $Self->AddOption(
        Name        => 'debug',
        Description => "Print debug info to the OTRS log.",
        Required    => 0,
        HasValue    => 0,
    );

    $Self->AdditionalHelp(
        "This script only runs file based generic agent jobs, database based jobs are handled by the OTRS Daemon."
    );
    return;
}

sub PreRun {
    my ( $Self, %Param ) = @_;

    # check configuration module
    my $ConfigurationModule = $Self->GetOption('configuration-module');
    if ( !$Kernel::OM->Get('Kernel::System::Main')->Require($ConfigurationModule) ) {
        die "Could not load agent job file '$ConfigurationModule': $!\n";
    }

    $Self->{JobName} = substr 'GenericAgentFile-' . $ConfigurationModule, 0, 200;

    # create PID lock
    my $ForcePID = $Self->GetOption('force-pid');

    # get PID object
    my $PIDObject = $Kernel::OM->Get('Kernel::System::PID');

    if ( !$ForcePID && !$PIDObject->PIDCreate( Name => $Self->{JobName} ) ) {
        die "Generic agent is already running for $ConfigurationModule module!\n";
    }
    elsif ( $ForcePID && !$PIDObject->PIDCreate( Name => $Self->{JobName} ) ) {
        $Self->Print(
            "NOTICE: generic agent is already running for $ConfigurationModule module, but is starting again!\n"
        );
    }

    # set new PID
    $PIDObject->PIDCreate(
        Name  => $Self->{JobName},
        Force => 1,
    );

    return;
}

sub Run {
    my ( $Self, %Param ) = @_;

    $Self->Print("<yellow>Running generic agent jobs...</yellow>\n");

    # make sure generic agent object is destroyed before continue
    $Kernel::OM->ObjectsDiscard(
        Objects => ['Kernel::System::GenericAgent'],
    );

    my $Debug = $Self->GetOption('debug') ? 1 : 0;

    # add parameters to generic agent object
    $Kernel::OM->ObjectParamAdd(
        'Kernel::System::GenericAgent' => {
            NoticeSTDOUT => 1,
            Debug        => $Debug,
        },
    );

    # disable in memory cache
    $Kernel::OM->Get('Kernel::System::Cache')->Configure(
        CacheInMemory  => 0,
        CacheInBackend => 1,
    );

    # get generic agent config module (job file)
    my $ConfigurationModule = $Self->GetOption('configuration-module');

    # load/import config jobs
    if ( !$Kernel::OM->Get('Kernel::System::Main')->Require($ConfigurationModule) ) {
        $Self->PrintError("Could not load agent job file '$ConfigurationModule': $!\n");
        return $Self->ExitCodeError();
    }
    eval "import $ConfigurationModule";    ## no critic

    # set the maximum number of affected tickets
    my $Limit = $Self->GetOption('ticket-limit');

    # set generic agent UserID
    my $UserIDOfGenericAgent = 1;

    # get generic agent object
    my $GenericAgentObject = $Kernel::OM->Get('Kernel::System::GenericAgent');

    # process all config file jobs
    for my $Job ( sort keys %Jobs ) {

        # execute generic agent job
        $GenericAgentObject->JobRun(
            Job    => $Job,
            Limit  => $Limit,
            Config => $Jobs{$Job},
            UserID => $UserIDOfGenericAgent,
        );
    }

    $Self->Print("<green>Done.</green>\n");
    return $Self->ExitCodeOk();
}

sub PostRun {
    my ($Self) = @_;

    # delete pid lock
    return $Kernel::OM->Get('Kernel::System::PID')->PIDDelete(
        Name => $Self->{JobName},
    );
}

1;