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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
|
# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# Copyright (C) 2021 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.
# --
## no critic (Modules::RequireExplicitPackage)
use strict;
use warnings;
use utf8;
use vars (qw($Self));
# Broken on certain Perl 5.28 versions due to a Perl crash that we can't work around.
my @BlacklistPerlVersions = (
v5.26.1,
v5.26.3,
v5.28.1,
v5.28.2,
v5.30.0,
v5.30.1,
v5.30.2,
v5.30.3,
);
if ( grep { $^V eq $_ } @BlacklistPerlVersions ) {
$Self->True( 1, "Current Perl version $^V is known to be buggy for this test, skipping." );
return 1;
}
# get config object
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
my $Home = $ConfigObject->Get('Home');
my $Daemon = $Home . '/bin/otrs.Daemon.pl';
# get current daemon status
my $PreviousDaemonStatus = `$Daemon status`;
# stop daemon if it was already running before this test
if ( $PreviousDaemonStatus =~ m{Daemon running}i ) {
`$^X $Daemon stop`;
my $SleepTime = 2;
# wait to get daemon fully stopped before test continues
print "A running Daemon was detected and need to be stopped...\n";
print 'Sleeping ' . $SleepTime . "s\n";
sleep $SleepTime;
}
# get needed objects
my $TaskWorkerObject = $Kernel::OM->Get('Kernel::System::Daemon::DaemonModules::SchedulerTaskWorker');
my $SchedulerDBObject = $Kernel::OM->Get('Kernel::System::Daemon::SchedulerDB');
my $HelperObject = $Kernel::OM->Get('Kernel::System::UnitTest::Helper');
my $RunTasks = sub {
local $SIG{CHLD} = "IGNORE";
# wait until task is executed
ACTIVESLEEP:
for my $Sec ( 1 .. 120 ) {
# run the worker
$TaskWorkerObject->Run();
$TaskWorkerObject->_WorkerPIDsCheck();
my @List = $SchedulerDBObject->TaskList();
last ACTIVESLEEP if !scalar @List;
sleep 1;
print "Waiting $Sec secs for scheduler tasks to be executed\n";
}
};
$RunTasks->();
# get original Cron settings
my $OriginalSettings = $ConfigObject->Get('Daemon::SchedulerCronTaskManager::Task') || {};
# remove all Cron jobs from config
$ConfigObject->Set(
Key => 'Daemon::SchedulerCronTaskManager::Task',
Value => {},
);
# freeze time
$HelperObject->FixedTimeSet();
my $CurSysDTObject = $Kernel::OM->Create('Kernel::System::DateTime');
# go back in time to have 0 seconds in the current minute
$HelperObject->FixedTimeAddSeconds( $CurSysDTObject->Get()->{Second} - 60 );
# get random ID
my $RandomID = $HelperObject->GetRandomID();
my @Tests = (
{
Name => 'No Schedule',
CronJob => {
Name => 'NoSchedule' . $RandomID,
Data => {
TaskName => 'NoSchedule' . $RandomID,
Module => 'Admin::Package::List',
Function => 'run',
},
},
TaskAdd => 0,
},
{
Name => 'Wrong Schedule',
CronJob => {
Name => 'WrongSchedue' . $RandomID,
Data => {
TaskName => 'NoSchedule' . $RandomID,
Module => 'Admin::Package::List',
Function => 'run',
Schedule => '\1 * * * *',
},
},
TaskAdd => 0,
},
# Test block (this tests needs to be run together in the same order)
{
Name => 'All Minutes',
CronJob => {
Name => 'CronAllMinutes' . $RandomID,
Data => {
TaskName => 'CronAllMinutes' . $RandomID,
Module => 'Admin::Package::List',
Function => 'run',
Schedule => '* * * * *',
},
},
TaskAdd => 1,
KeepCron => 1,
},
{
Name => 'All Minutes (same minute)',
CronName => 'CronAllMinutes' . $RandomID,
TaskAdd => 0,
SecondsAdd => 50,
KeepCron => 1,
},
{
Name => 'All Minutes (same minute w/o cache)',
CronName => 'CronAllMinutes' . $RandomID,
TaskAdd => 0,
NoCache => 1,
KeepCron => 1,
},
{
Name => 'All Minutes (next minute)',
CronName => 'CronAllMinutes' . $RandomID,
TaskAdd => 1,
SecondsAdd => 10,
},
# Test block end
);
# get needed objects
my $TaskManagerObject = $Kernel::OM->Get('Kernel::System::Daemon::DaemonModules::SchedulerCronTaskManager');
my $CacheObject = $Kernel::OM->Get('Kernel::System::Cache');
TESTCASE:
for my $Test (@Tests) {
my $CronName;
# create a Cron job if needed
if ( $Test->{CronJob} ) {
my $CronAdd = $ConfigObject->Set(
Key => "Daemon::SchedulerCronTaskManager::Task###$Test->{CronJob}->{Name}",
Value => $Test->{CronJob}->{Data},
);
$Self->True(
$CronAdd,
"$Test->{Name} Cron setting added - for Cron job '$Test->{CronJob}->{Name}' with true",
);
$CronName = $Test->{CronJob}->{Name};
# run the task manager
my $RunSuccess = $TaskManagerObject->Run();
$Self->True(
$RunSuccess,
"$Test->{Name} task manager Run() - with true (this will create the recurrent task but not execution)",
);
my $StartSystemTimeObject = $Kernel::OM->Create('Kernel::System::DateTime');
my $SecondsAdd = ( 60 - $StartSystemTimeObject->Get()->{Second} );
$HelperObject->FixedTimeAddSeconds($SecondsAdd);
my $EndSystemTimeObject = $Kernel::OM->Create('Kernel::System::DateTime');
printf(
"Added %s seconds to time (initial adjustment) from %s to %s\n",
$SecondsAdd,
$StartSystemTimeObject->ToEpoch(),
$EndSystemTimeObject->ToEpoch(),
);
}
$CronName //= $Test->{CronName} || '';
# add seconds if needed
if ( $Test->{SecondsAdd} ) {
my $StartSystemTime = $Kernel::OM->Create('Kernel::System::DateTime')->ToEpoch();
$HelperObject->FixedTimeAddSeconds( $Test->{SecondsAdd} );
my $EndSystemTime = $Kernel::OM->Create('Kernel::System::DateTime')->ToEpoch();
printf(
" Added %s seconds to time from %s to %s\n",
$Test->{SecondsAdd},
$StartSystemTime,
$EndSystemTime,
);
}
# cleanup Task Manager Cache
my $Cache;
if ( $Test->{NoCache} ) {
$Cache = $CacheObject->Get(
Type => 'SchedulerDBRecurrentTaskExecute',
Key => $CronName . '::Cron',
CacheInMemory => 0,
CacheInBackend => 1,
);
$CacheObject->CleanUp(
Type => 'SchedulerDBRecurrentTaskExecute',
);
print " Cache cleared before task manager execution...\n";
}
# run the task manager
my $RunSuccess = $TaskManagerObject->Run();
$Self->True(
$RunSuccess,
"$Test->{Name} task manager Run() - with true",
);
# return the cache as it was
if ( $Test->{NoCache} ) {
$CacheObject->Set(
Type => 'SchedulerDBRecurrentTaskExecute',
Key => $CronName . '::Cron',
Value => $Cache,
TTL => 60 * 5,
CacheInMemory => 0,
CacheInBackend => 1,
);
print " Cache restored after task manager execution...\n";
}
my @List = $SchedulerDBObject->TaskList(
Type => 'Cron',
);
my @FilteredList = grep { $_->{Name} eq $CronName } @List;
if ( $Test->{TaskAdd} ) {
$Self->Is(
scalar @FilteredList,
1,
"$Test->{Name} - task manager creates one task",
);
local $SIG{CHLD} = "IGNORE";
# wait until task is executed
ACTIVESLEEP:
for my $Sec ( 1 .. 120 ) {
# run the worker
$TaskWorkerObject->Run();
$TaskWorkerObject->_WorkerPIDsCheck();
@List = $SchedulerDBObject->TaskList(
Type => 'Cron',
);
@FilteredList = grep {$CronName} @List;
last ACTIVESLEEP if !scalar @FilteredList;
sleep 1;
print " Waiting $Sec secs for the task to be executed\n";
}
@List = $SchedulerDBObject->TaskList(
Type => 'Cron',
);
@FilteredList = grep {$CronName} @List;
$Self->Is(
scalar @FilteredList,
0,
"$Test->{Name} - task worker execute the task",
);
}
else {
$Self->Is(
scalar @FilteredList,
0,
"$Test->{Name} - task manager should not create any tasks"
);
}
next TESTCASE if $Test->{KeepCron};
# remove all Cron jobs from config
my $CronDelete = $ConfigObject->Set(
Key => 'Daemon::SchedulerCronTaskManager::Task',
Value => {},
);
$Self->True(
$CronDelete,
"$Test->{Name} Delete Cron Tasks - with true",
);
# recurrent task delete
@List = $SchedulerDBObject->RecurrentTaskList(
Type => 'Cron',
);
@FilteredList = grep { $_->{Name} eq $CronName } @List;
TASK:
for my $Task (@FilteredList) {
next TASK if $Task->{Type} ne 'Cron';
next TASK if $Task->{Name} ne $CronName;
my $TaskID = $Task->{TaskID};
my $Success = $SchedulerDBObject->RecurrentTaskDelete(
TaskID => $TaskID,
);
$Self->True(
$Success,
"RecurrentTaskDelete() - for $TaskID with true",
);
}
}
# cleanup
my $CleanupSuccess = $SchedulerDBObject->CronTaskCleanup();
$Self->True(
$CleanupSuccess,
"CronTaskCleanup() - executed with true",
);
# CronTaskToExecute() tests
my $CronName = 'CronAllMinutes' . $RandomID;
my $CronAdd = $ConfigObject->Set(
Key => "Daemon::SchedulerCronTaskManager::Task###$CronName",
Value => {
TaskName => $CronName,
Module => 'Admin::Package::List',
Function => 'run',
Schedule => '* * * * *',
},
);
$Self->True(
$CronAdd,
"Cron setting added - for Cron job '$CronName' with true",
);
my %TestJobNames = (
"CronAllMinutes$RandomID" => 1,
"UnitTest$RandomID" => 1,
"Test$RandomID" => 1,
"SafeToDelete$RandomID" => 1,
);
# get db object
my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
# add tasks
for my $Name ( sort keys %TestJobNames ) {
my $AddSucess = $DBObject->Do(
SQL => "
INSERT INTO scheduler_recurrent_task
(name, task_type, last_execution_time, lock_key, create_time, change_time)
VALUES
(?, ?, current_timestamp, 0, current_timestamp, current_timestamp)",
Bind => [
\$Name,
\'Cron',
],
);
$Self->True(
$AddSucess,
"Recurrent task added - for Cron job '$Name' with true",
);
}
# make sure all task are listed
my @List = $SchedulerDBObject->RecurrentTaskList(
Type => 'Cron',
);
my %FilteredTaskNames = map { $_->{Name} => 1 } grep { $TestJobNames{ $_->{Name} } } @List;
for my $Name ( sort keys %TestJobNames ) {
$Self->True(
$FilteredTaskNames{$Name},
"Task $Name exists with true",
);
}
# remove fake test tasks
$CleanupSuccess = $SchedulerDBObject->CronTaskCleanup();
$Self->True(
$CleanupSuccess,
"CronTaskCleanup() - executed with true",
);
# make sure only CronAllMinutes is listed
@List = $SchedulerDBObject->RecurrentTaskList(
Type => 'Cron',
);
%FilteredTaskNames = map { $_->{Name} => 1 } grep { $TestJobNames{ $_->{Name} } } @List;
for my $Name ( sort keys %TestJobNames ) {
if ( $Name eq "CronAllMinutes$RandomID" ) {
$Self->True(
$FilteredTaskNames{$Name},
"Task $Name exists with true",
);
}
else {
$Self->False(
$FilteredTaskNames{$Name},
"Task $Name exists with False",
);
}
}
# remove all Cron jobs from config
my $JobDelete = $ConfigObject->Set(
Key => 'Daemon::SchedulerCronTaskManager::Task',
Value => {},
);
$Self->True(
$JobDelete,
"Removed all cron task settings - executed with true",
);
# remove all cron jobs from config
$ConfigObject->Set(
Key => 'Daemon::SchedulerCronTaskManager::Task',
Value => $OriginalSettings,
);
# re-create original tasks
my $RunSuccess = $TaskManagerObject->Run();
$Self->True(
$RunSuccess,
"Task manager Run() - with true",
);
# also remove the task form the just removed Cron job
$CleanupSuccess = $SchedulerDBObject->CronTaskCleanup();
$Self->True(
$CleanupSuccess,
"CronTaskCleanup() - executed with true",
);
# start daemon if it was already running before this test
if ( $PreviousDaemonStatus =~ m{Daemon running}i ) {
system("$^X $Daemon start");
}
# cleanup cache
$Kernel::OM->Get('Kernel::System::Cache')->CleanUp();
1;
|