Passed
Push — deprecate/CLI_releaseProcesses ( 9ee964...f0324c )
by Tomas Norre
06:52 queued 03:35
created

ProcessQueueCommand::execute()   B

Complexity

Conditions 8
Paths 33

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 9.5657

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 8
eloc 27
c 3
b 0
f 0
nc 33
nop 2
dl 0
loc 46
ccs 22
cts 31
cp 0.7097
crap 9.5657
rs 8.4444
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Command;
6
7
/***************************************************************
8
 *  Copyright notice
9
 *
10
 *  (c) 2019 AOE GmbH <[email protected]>
11
 *
12
 *  All rights reserved
13
 *
14
 *  This script is part of the TYPO3 project. The TYPO3 project is
15
 *  free software; you can redistribute it and/or modify
16
 *  it under the terms of the GNU General Public License as published by
17
 *  the Free Software Foundation; either version 3 of the License, or
18
 *  (at your option) any later version.
19
 *
20
 *  The GNU General Public License can be found at
21
 *  http://www.gnu.org/copyleft/gpl.html.
22
 *
23
 *  This script is distributed in the hope that it will be useful,
24
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
 *  GNU General Public License for more details.
27
 *
28
 *  This copyright notice MUST APPEAR in all copies of the script!
29
 ***************************************************************/
30
31
use AOE\Crawler\Controller\CrawlerController;
32
use AOE\Crawler\Crawler;
33
use AOE\Crawler\Domain\Repository\ProcessRepository;
34
use AOE\Crawler\Domain\Repository\QueueRepository;
35
use Symfony\Component\Console\Command\Command;
36
use Symfony\Component\Console\Input\InputInterface;
37
use Symfony\Component\Console\Input\InputOption;
38
use Symfony\Component\Console\Output\OutputInterface;
39
use TYPO3\CMS\Core\Utility\GeneralUtility;
40
use TYPO3\CMS\Extbase\Object\ObjectManager;
41
42
class ProcessQueueCommand extends Command
43
{
44
    public const CLI_STATUS_NOTHING_PROCCESSED = 0;
45
46
    //queue not empty
47
    public const CLI_STATUS_REMAIN = 1;
48
49
    //(some) queue items where processed
50
    public const CLI_STATUS_PROCESSED = 2;
51
52
    //instance didn't finish
53
    public const CLI_STATUS_ABORTED = 4;
54
55
    public const CLI_STATUS_POLLABLE_PROCESSED = 8;
56
57
    /**
58
     * Crawler Command - Crawling the URLs from the queue
59
     *
60
     * Examples:
61
     *
62
     * --- Will trigger the crawler which starts to process the queue entries
63
     * $ typo3 crawler:crawlQueue
64
     *
65
     * @return int
66
     */
67 2
    public function execute(InputInterface $input, OutputInterface $output)
68
    {
69 2
        $amount = $input->getOption('amount');
70 2
        $sleeptime = $input->getOption('sleeptime');
71 2
        $sleepafter = $input->getOption('sleepafter');
72
73 2
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
74
75 2
        $result = self::CLI_STATUS_NOTHING_PROCCESSED;
76
77
        /** @var CrawlerController $crawlerController */
78 2
        $crawlerController = $objectManager->get(CrawlerController::class);
79
        /** @var QueueRepository $queueRepository */
80 2
        $queueRepository = $objectManager->get(QueueRepository::class);
81
        /** @var ProcessRepository $processRepository */
82 2
        $processRepository = $objectManager->get(ProcessRepository::class);
83
84
        /** @var Crawler $crawler */
85 2
        $crawler = GeneralUtility::makeInstance(Crawler::class);
86
87 2
        if (! $crawler->isDisabled() && $crawlerController->CLI_checkAndAcquireNewProcess($crawlerController->CLI_buildProcessId())) {
88 2
            $countInARun = $amount ? (int) $amount : (int) $crawlerController->extensionSettings['countInARun'];
89 2
            $sleepAfterFinish = $sleepafter ? (int) $sleepafter : (int) $crawlerController->extensionSettings['sleepAfterFinish'];
90 2
            $sleepTime = $sleeptime ? (int) $sleeptime : (int) $crawlerController->extensionSettings['sleepTime'];
91
92
            try {
93
                // Run process:
94 2
                $result = $crawlerController->CLI_run($countInARun, $sleepTime, $sleepAfterFinish);
95
            } catch (\Throwable $e) {
96
                $output->writeln('<warning>' . get_class($e) . ': ' . $e->getMessage() . '</warning>');
97
                $result = self::CLI_STATUS_ABORTED;
98
            }
99
100
            // Cleanup
101 2
            $processRepository->deleteProcessesWithoutItemsAssigned();
102 2
            $processRepository->markRequestedProcessesAsNotActive([$crawlerController->CLI_buildProcessId()]);
103 2
            $queueRepository->unsetProcessScheduledAndProcessIdForQueueEntries([$crawlerController->CLI_buildProcessId()]);
104
105 2
            $output->writeln('<info>Unprocessed Items remaining:' . count($queueRepository->getUnprocessedItems()) . ' (' . $crawlerController->CLI_buildProcessId() . ')</info>');
106 2
            $result |= (count($queueRepository->getUnprocessedItems()) > 0 ? self::CLI_STATUS_REMAIN : self::CLI_STATUS_NOTHING_PROCCESSED);
107
        } else {
108
            $result |= self::CLI_STATUS_ABORTED;
109
        }
110
111 2
        $output->writeln($result);
112 2
        return $result & self::CLI_STATUS_ABORTED;
113
    }
114
115 2
    protected function configure(): void
116
    {
117 2
        $this->setDescription('Trigger the crawler to process the queue entries');
118
119 2
        $this->setHelp(
120 2
            'Crawler Command - Crawling the URLs from the queue' . chr(10) . chr(10) .
121 2
            '
122
            Examples:
123
              --- Will trigger the crawler which starts to process the queue entries
124
              $ typo3 crawler:processqueue --amount 15 --sleepafter 5 --sleeptime 2
125
            '
126
        );
127 2
        $this->addOption(
128 2
            'amount',
129 2
            '',
130 2
            InputOption::VALUE_OPTIONAL,
131 2
            'How many pages should be crawled during that run',
132
            0
133
        );
134
135 2
        $this->addOption(
136 2
            'sleepafter',
137 2
            '',
138 2
            InputOption::VALUE_OPTIONAL,
139 2
            'Amount of milliseconds which the system should use to relax between crawls',
140
            0
141
        );
142
143 2
        $this->addOption(
144 2
            'sleeptime',
145 2
            '',
146 2
            InputOption::VALUE_OPTIONAL,
147 2
            'Amount of seconds which the system should use to relax after all crawls are done.'
148
        );
149 2
    }
150
}
151