init III
This commit is contained in:
@@ -0,0 +1,387 @@
|
||||
# --
|
||||
# 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::Console::Command::Admin::ITSM::Change::Check;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use parent qw(
|
||||
Kernel::System::Console::BaseCommand
|
||||
Kernel::System::EventHandler);
|
||||
|
||||
our @ObjectDependencies = (
|
||||
'Kernel::Config',
|
||||
'Kernel::System::DateTime',
|
||||
'Kernel::System::PID',
|
||||
'Kernel::System::ITSMChange',
|
||||
'Kernel::System::ITSMChange::ITSMWorkOrder',
|
||||
'Kernel::System::ITSMChange::History',
|
||||
);
|
||||
|
||||
sub Configure {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
$Self->Description('Check if ITSM changes have reached specific times.');
|
||||
$Self->AddOption(
|
||||
Name => 'force-pid',
|
||||
Description => "Start even if another process is still registered in the database.",
|
||||
Required => 0,
|
||||
HasValue => 0,
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sub PreRun {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# get PID object
|
||||
my $PIDObject = $Kernel::OM->Get('Kernel::System::PID');
|
||||
|
||||
# create PID lock
|
||||
my $PIDCreated = $PIDObject->PIDCreate(
|
||||
Name => $Self->Name(),
|
||||
Force => $Self->GetOption('force-pid'),
|
||||
TTL => 60 * 60 * 2,
|
||||
);
|
||||
if ( !$PIDCreated ) {
|
||||
my $Error = "Unable to register the process in the database. Is another instance still running?\n";
|
||||
$Error .= "You can use --force-pid to override this check.\n";
|
||||
die $Error;
|
||||
}
|
||||
|
||||
# init of event handler
|
||||
$Self->EventHandlerInit(
|
||||
Config => 'ITSMChangeCronjob::EventModule',
|
||||
);
|
||||
|
||||
# get time object
|
||||
my $DateTimeObject = $Kernel::OM->Create('Kernel::System::DateTime');
|
||||
|
||||
$Self->{SystemTime} = $DateTimeObject->ToEpoch();
|
||||
$Self->{Now} = $DateTimeObject->ToString();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sub Run {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
$Self->Print("<yellow>Checking ITSM changes...</yellow>\n");
|
||||
|
||||
# get change object
|
||||
my $ChangeObject = $Kernel::OM->Get('Kernel::System::ITSMChange');
|
||||
|
||||
# notifications for changes' plannedXXXtime events
|
||||
for my $Type (qw(StartTime EndTime)) {
|
||||
|
||||
# get changes with PlannedStartTime older than now
|
||||
my $PlannedChangeIDs = $ChangeObject->ChangeSearch(
|
||||
"Planned${Type}OlderDate" => $Self->{Now},
|
||||
MirrorDB => 1,
|
||||
UserID => 1,
|
||||
) || [];
|
||||
|
||||
CHANGEID:
|
||||
for my $ChangeID ( @{$PlannedChangeIDs} ) {
|
||||
|
||||
# get change data
|
||||
my $Change = $ChangeObject->ChangeGet(
|
||||
ChangeID => $ChangeID,
|
||||
UserID => 1,
|
||||
);
|
||||
|
||||
# skip change if there is already an actualXXXtime set or notification was sent
|
||||
next CHANGEID if $Change->{"Actual$Type"};
|
||||
|
||||
my $LastNotificationSentDate = $Self->ChangeNotificationSent(
|
||||
ChangeID => $ChangeID,
|
||||
Type => "Planned${Type}",
|
||||
);
|
||||
|
||||
my $AlreadySentWithinPeriod = $Self->SentWithinPeriod(
|
||||
LastNotificationSentDate => $LastNotificationSentDate,
|
||||
);
|
||||
|
||||
next CHANGEID if $AlreadySentWithinPeriod;
|
||||
|
||||
# trigger ChangePlannedStartTimeReachedPost-Event
|
||||
$Self->EventHandler(
|
||||
Event => "ChangePlanned${Type}ReachedPost",
|
||||
Data => {
|
||||
ChangeID => $ChangeID,
|
||||
},
|
||||
UserID => 1,
|
||||
);
|
||||
}
|
||||
|
||||
# get changes with actualxxxtime
|
||||
my $ActualChangeIDs = $ChangeObject->ChangeSearch(
|
||||
"Actual${Type}OlderDate" => $Self->{Now},
|
||||
MirrorDB => 1,
|
||||
UserID => 1,
|
||||
) || [];
|
||||
|
||||
ACTUALCHANGEID:
|
||||
for my $ChangeID ( @{$ActualChangeIDs} ) {
|
||||
|
||||
# get change data
|
||||
my $Change = $ChangeObject->ChangeGet(
|
||||
ChangeID => $ChangeID,
|
||||
UserID => 1,
|
||||
);
|
||||
|
||||
my $LastNotificationSentDate = $Self->ChangeNotificationSent(
|
||||
ChangeID => $ChangeID,
|
||||
Type => "Actual${Type}",
|
||||
);
|
||||
|
||||
next ACTUALCHANGEID if $LastNotificationSentDate;
|
||||
|
||||
# trigger Event
|
||||
$Self->EventHandler(
|
||||
Event => "ChangeActual${Type}ReachedPost",
|
||||
Data => {
|
||||
ChangeID => $ChangeID,
|
||||
},
|
||||
UserID => 1,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
# get changes with RequestedTimeOlderDate
|
||||
my $RequestedTimeChangeIDs = $ChangeObject->ChangeSearch(
|
||||
RequestedTimeOlderDate => $Self->{Now},
|
||||
MirrorDB => 1,
|
||||
UserID => 1,
|
||||
) || [];
|
||||
|
||||
CHANGEID:
|
||||
for my $ChangeID ( @{$RequestedTimeChangeIDs} ) {
|
||||
|
||||
# get change data
|
||||
my $Change = $ChangeObject->ChangeGet(
|
||||
ChangeID => $ChangeID,
|
||||
UserID => 1,
|
||||
);
|
||||
|
||||
my $LastNotificationSentDate = $Self->ChangeNotificationSent(
|
||||
ChangeID => $ChangeID,
|
||||
Type => "RequestedTime",
|
||||
);
|
||||
|
||||
next CHANGEID if $LastNotificationSentDate;
|
||||
|
||||
# trigger Event
|
||||
$Self->EventHandler(
|
||||
Event => "ChangeRequestedTimeReachedPost",
|
||||
Data => {
|
||||
ChangeID => $ChangeID,
|
||||
},
|
||||
UserID => 1,
|
||||
);
|
||||
}
|
||||
|
||||
# notifications for workorders' plannedXXXtime events
|
||||
for my $Type (qw(StartTime EndTime)) {
|
||||
|
||||
my $WorkOrderObject = $Kernel::OM->Get('Kernel::System::ITSMChange::ITSMWorkOrder');
|
||||
|
||||
# get workorders with PlannedStartTime older than now
|
||||
my $PlannedWorkOrderIDs = $WorkOrderObject->WorkOrderSearch(
|
||||
"Planned${Type}OlderDate" => $Self->{Now},
|
||||
MirrorDB => 1,
|
||||
UserID => 1,
|
||||
) || [];
|
||||
|
||||
WORKORDERID:
|
||||
for my $WorkOrderID ( @{$PlannedWorkOrderIDs} ) {
|
||||
|
||||
# get workorder data
|
||||
my $WorkOrder = $WorkOrderObject->WorkOrderGet(
|
||||
WorkOrderID => $WorkOrderID,
|
||||
UserID => 1,
|
||||
);
|
||||
|
||||
# skip workorder if there is already an actualXXXtime set or notification was sent
|
||||
next WORKORDERID if $WorkOrder->{"Actual$Type"};
|
||||
|
||||
my $LastNotificationSentDate = $Self->WorkOrderNotificationSent(
|
||||
WorkOrderID => $WorkOrderID,
|
||||
Type => "Planned${Type}",
|
||||
);
|
||||
|
||||
my $AlreadySentWithinPeriod = $Self->SentWithinPeriod(
|
||||
LastNotificationSentDate => $LastNotificationSentDate,
|
||||
);
|
||||
|
||||
next WORKORDERID if $AlreadySentWithinPeriod;
|
||||
|
||||
# trigger WorkOrderPlannedStartTimeReachedPost-Event
|
||||
$Self->EventHandler(
|
||||
Event => "WorkOrderPlanned${Type}ReachedPost",
|
||||
Data => {
|
||||
WorkOrderID => $WorkOrderID,
|
||||
ChangeID => $WorkOrder->{ChangeID},
|
||||
},
|
||||
UserID => 1,
|
||||
);
|
||||
}
|
||||
|
||||
# get workorders with actualxxxtime
|
||||
my $ActualWorkOrderIDs = $WorkOrderObject->WorkOrderSearch(
|
||||
"Actual${Type}OlderDate" => $Self->{Now},
|
||||
MirrorDB => 1,
|
||||
UserID => 1,
|
||||
) || [];
|
||||
|
||||
WORKORDERID:
|
||||
for my $WorkOrderID ( @{$ActualWorkOrderIDs} ) {
|
||||
|
||||
# get workorder data
|
||||
my $WorkOrder = $WorkOrderObject->WorkOrderGet(
|
||||
WorkOrderID => $WorkOrderID,
|
||||
UserID => 1,
|
||||
);
|
||||
|
||||
my $LastNotificationSentDate = $Self->WorkOrderNotificationSent(
|
||||
WorkOrderID => $WorkOrderID,
|
||||
Type => "Actual${Type}",
|
||||
);
|
||||
|
||||
next WORKORDERID if $LastNotificationSentDate;
|
||||
|
||||
# trigger Event
|
||||
$Self->EventHandler(
|
||||
Event => "WorkOrderActual${Type}ReachedPost",
|
||||
Data => {
|
||||
WorkOrderID => $WorkOrderID,
|
||||
ChangeID => $WorkOrder->{ChangeID},
|
||||
},
|
||||
UserID => 1,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$Self->Print("<green>Done.</green>\n");
|
||||
return $Self->ExitCodeOk();
|
||||
|
||||
}
|
||||
|
||||
# check if a notification was already sent for the given change
|
||||
sub ChangeNotificationSent {
|
||||
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
for my $Needed (qw(ChangeID Type)) {
|
||||
return if !$Param{$Needed};
|
||||
}
|
||||
|
||||
# get history entries
|
||||
my $History = $Kernel::OM->Get('Kernel::System::ITSMChange::History')->ChangeHistoryGet(
|
||||
ChangeID => $Param{ChangeID},
|
||||
UserID => 1,
|
||||
);
|
||||
|
||||
# search for notifications sent earlier
|
||||
for my $HistoryEntry ( reverse @{$History} ) {
|
||||
if (
|
||||
$HistoryEntry->{HistoryType} eq 'Change' . $Param{Type} . 'Reached'
|
||||
&& $HistoryEntry->{ContentNew} =~ m{ Notification \s Sent $ }xms
|
||||
)
|
||||
{
|
||||
return $HistoryEntry->{CreateTime};
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
# check if a notification was already sent for the given workorder
|
||||
sub WorkOrderNotificationSent {
|
||||
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# check needed stuff
|
||||
for my $Needed (qw(WorkOrderID Type)) {
|
||||
return if !$Param{$Needed};
|
||||
}
|
||||
|
||||
# get history entries
|
||||
my $History = $Kernel::OM->Get('Kernel::System::ITSMChange::History')->WorkOrderHistoryGet(
|
||||
WorkOrderID => $Param{WorkOrderID},
|
||||
UserID => 1,
|
||||
);
|
||||
|
||||
# search for notifications sent earlier
|
||||
for my $HistoryEntry ( reverse @{$History} ) {
|
||||
if (
|
||||
$HistoryEntry->{HistoryType} eq 'WorkOrder' . $Param{Type} . 'Reached'
|
||||
&& $HistoryEntry->{ContentNew} =~ m{ Notification \s Sent }xms
|
||||
)
|
||||
{
|
||||
return $HistoryEntry->{CreateTime};
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sub SentWithinPeriod {
|
||||
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
return if !$Param{LastNotificationSentDate};
|
||||
|
||||
# get SysConfig option
|
||||
my $Config = $Kernel::OM->Get('Kernel::Config')->Get('ITSMChange::TimeReachedNotifications');
|
||||
|
||||
# if notifications should be sent only once
|
||||
return 1 if $Config->{Frequency} eq 'once';
|
||||
|
||||
# get epoche seconds of send time
|
||||
my $SentEpoche = $Kernel::OM->Create(
|
||||
'Kernel::System::DateTime',
|
||||
ObjectParams => {
|
||||
String => $Param{LastNotificationSentDate},
|
||||
}
|
||||
)->ToEpoch();
|
||||
|
||||
# calc diff
|
||||
my $EpocheSinceSent = $Self->{SystemTime} - $SentEpoche;
|
||||
my $HoursSinceSent = int( $EpocheSinceSent / ( 60 * 60 ) );
|
||||
|
||||
if ( $HoursSinceSent >= $Config->{Hours} ) {
|
||||
return;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub PostRun {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
# delete pid lock
|
||||
$Kernel::OM->Get('Kernel::System::PID')->PIDDelete( Name => $Self->Name() );
|
||||
|
||||
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,178 @@
|
||||
# --
|
||||
# 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::Console::Command::Admin::ITSM::Change::Delete;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use parent qw(Kernel::System::Console::BaseCommand);
|
||||
|
||||
our @ObjectDependencies = (
|
||||
'Kernel::System::ITSMChange',
|
||||
);
|
||||
|
||||
sub Configure {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
$Self->Description('Delete changes (all or by number).');
|
||||
$Self->AddOption(
|
||||
Name => 'all',
|
||||
Description => "Delete all changes.",
|
||||
Required => 0,
|
||||
HasValue => 0,
|
||||
);
|
||||
$Self->AddArgument(
|
||||
Name => 'accept',
|
||||
Description => "Accept delete all or cancel.",
|
||||
Required => 0,
|
||||
ValueRegex => qr/(y|n)/smx,
|
||||
);
|
||||
$Self->AddOption(
|
||||
Name => 'change-number',
|
||||
Description => "Delete listed changes.",
|
||||
Required => 0,
|
||||
HasValue => 1,
|
||||
ValueRegex => qr/\d+/smx,
|
||||
Multiple => 1,
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sub PreRun {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
my @ChangeNumbers = @{ $Self->GetOption('change-number') // [] };
|
||||
|
||||
if ( !$Self->GetOption('all') && !@ChangeNumbers ) {
|
||||
die "Please provide option --all or --change-number. For more details use --help\n";
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
sub Run {
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
$Self->Print("<yellow>Deleting changes...</yellow>\n");
|
||||
|
||||
$Self->Print( "<green>" . ( '=' x 69 ) . "</green>\n" );
|
||||
|
||||
# get change object
|
||||
my $ChangeObject = $Kernel::OM->Get('Kernel::System::ITSMChange');
|
||||
|
||||
# get change numbers
|
||||
my @ChangeNumbers = @{ $Self->GetOption('change-number') // [] };
|
||||
|
||||
# delete all changes
|
||||
if ( $Self->GetOption('all') ) {
|
||||
|
||||
# get all change ids
|
||||
my @ChangesIDs = @{ $ChangeObject->ChangeList( UserID => 1 ) };
|
||||
|
||||
# get number of changes
|
||||
my $ChangeCount = scalar @ChangesIDs;
|
||||
|
||||
# if there are any changes to delete
|
||||
if ($ChangeCount) {
|
||||
|
||||
$Self->Print("<yellow>Are you sure that you want to delete ALL $ChangeCount changes?</yellow>\n");
|
||||
$Self->Print("<yellow>This is irrevocable. [y/n] </yellow>\n");
|
||||
my $Confirmation = $Self->GetArgument('accept');
|
||||
chomp( $Confirmation = lc <STDIN> ) if !defined $Confirmation;
|
||||
|
||||
# if the user confirms the deletion
|
||||
if ( $Confirmation eq 'y' ) {
|
||||
|
||||
# delete changes
|
||||
$Self->Print("<yellow>Deleting all changes...</yellow>\n");
|
||||
$Self->DeleteChanges( ChangesIDs => \@ChangesIDs );
|
||||
}
|
||||
else {
|
||||
$Self->Print("<yellow>Command delete was canceled!</yellow>\n");
|
||||
return $Self->ExitCodeOk();
|
||||
}
|
||||
}
|
||||
else {
|
||||
$Self->Print("<yellow>There are NO changes to delete.</yellow>\n");
|
||||
}
|
||||
}
|
||||
|
||||
# delete listed changes
|
||||
elsif (@ChangeNumbers) {
|
||||
|
||||
my @ChangesIDs;
|
||||
|
||||
for my $ChangeNumber (@ChangeNumbers) {
|
||||
|
||||
# checks the validity of the change id
|
||||
my $ID = $ChangeObject->ChangeLookup(
|
||||
ChangeNumber => $ChangeNumber,
|
||||
);
|
||||
|
||||
if ($ID) {
|
||||
push @ChangesIDs, $ID;
|
||||
}
|
||||
else {
|
||||
$Self->PrintError("Unable to find change $ChangeNumber.");
|
||||
}
|
||||
}
|
||||
|
||||
# delete changes (if any valid number was given)
|
||||
if (@ChangesIDs) {
|
||||
$Self->Print("<yellow>Deleting specified changes...</yellow>\n");
|
||||
$Self->DeleteChanges( ChangesIDs => \@ChangesIDs );
|
||||
}
|
||||
}
|
||||
else {
|
||||
$Self->PrintError("No change for delete.");
|
||||
}
|
||||
|
||||
$Self->Print( "<green>" . ( '=' x 69 ) . "</green>\n" );
|
||||
$Self->Print("<green>Done.</green>\n");
|
||||
return $Self->ExitCodeOk();
|
||||
}
|
||||
|
||||
sub DeleteChanges {
|
||||
|
||||
my ( $Self, %Param ) = @_;
|
||||
|
||||
my $DeletedChanges = 0;
|
||||
|
||||
# delete specified changes
|
||||
for my $ChangeID ( @{ $Param{ChangesIDs} } ) {
|
||||
my $True = $Kernel::OM->Get('Kernel::System::ITSMChange')->ChangeDelete(
|
||||
ChangeID => $ChangeID,
|
||||
UserID => 1,
|
||||
);
|
||||
if ( !$True ) {
|
||||
$Self->PrintError("Unable to delete change with id $ChangeID\n");
|
||||
}
|
||||
else {
|
||||
$DeletedChanges++;
|
||||
}
|
||||
}
|
||||
|
||||
$Self->Print("<green>Deleted $DeletedChanges change(s).</green>\n\n");
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user