Passed
Push — tests/improvements ( 81b4b0...70a11f )
by Tomas Norre
06:38
created

ProcessQueueCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 23
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 33
ccs 20
cts 20
cp 1
crap 1
rs 9.552
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 4
    public function execute(InputInterface $input, OutputInterface $output)
68
    {
69 4
        $amount = $input->getOption('amount');
70 4
        $sleeptime = $input->getOption('sleeptime');
71 4
        $sleepafter = $input->getOption('sleepafter');
72
73 4
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
74
75 4
        $result = self::CLI_STATUS_NOTHING_PROCCESSED;
76
77
        /** @var CrawlerController $crawlerController */
78 4
        $crawlerController = $objectManager->get(CrawlerController::class);
79
        /** @var QueueRepository $queueRepository */
80 4
        $queueRepository = $objectManager->get(QueueRepository::class);
81
        /** @var ProcessRepository $processRepository */
82 4
        $processRepository = $objectManager->get(ProcessRepository::class);
83
84
        /** @var Crawler $crawler */
85 4
        $crawler = GeneralUtility::makeInstance(Crawler::class);
86
87 4
        if (! $crawler->isDisabled() && $crawlerController->CLI_checkAndAcquireNewProcess($crawlerController->CLI_buildProcessId())) {
88 4
            $countInARun = $amount ? (int) $amount : (int) $crawlerController->extensionSettings['countInARun'];
89 4
            $sleepAfterFinish = $sleepafter ? (int) $sleepafter : (int) $crawlerController->extensionSettings['sleepAfterFinish'];
90 4
            $sleepTime = $sleeptime ? (int) $sleeptime : (int) $crawlerController->extensionSettings['sleepTime'];
91
92
            try {
93
                // Run process:
94 4
                $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 4
            $processRepository->deleteProcessesWithoutItemsAssigned();
102 4
            $crawlerController->CLI_releaseProcesses($crawlerController->CLI_buildProcessId());
103
104 4
            $output->writeln('<info>Unprocessed Items remaining:' . count($queueRepository->getUnprocessedItems()) . ' (' . $crawlerController->CLI_buildProcessId() . ')</info>');
105 4
            $result |= (count($queueRepository->getUnprocessedItems()) > 0 ? self::CLI_STATUS_REMAIN : self::CLI_STATUS_NOTHING_PROCCESSED);
106
        } else {
107
            $result |= self::CLI_STATUS_ABORTED;
108
        }
109
110 4
        $output->writeln($result);
111 4
        return $result & self::CLI_STATUS_ABORTED;
112
    }
113
114 4
    protected function configure(): void
115
    {
116 4
        $this->setDescription('Trigger the crawler to process the queue entries');
117
118 4
        $this->setHelp(
119 4
            'Crawler Command - Crawling the URLs from the queue' . chr(10) . chr(10) .
120 4
            '
121
            Examples:
122
              --- Will trigger the crawler which starts to process the queue entries
123
              $ typo3 crawler:processqueue --amount 15 --sleepafter 5 --sleeptime 2
124
            '
125
        );
126 4
        $this->addOption(
127 4
            'amount',
128 4
            '',
129 4
            InputOption::VALUE_OPTIONAL,
130 4
            'How many pages should be crawled during that run',
131
            0
132
        );
133
134 4
        $this->addOption(
135 4
            'sleepafter',
136 4
            '',
137 4
            InputOption::VALUE_OPTIONAL,
138 4
            'Amount of milliseconds which the system should use to relax between crawls',
139
            0
140
        );
141
142 4
        $this->addOption(
143 4
            'sleeptime',
144 4
            '',
145 4
            InputOption::VALUE_OPTIONAL,
146 4
            'Amount of seconds which the system should use to relax after all crawls are done.'
147
        );
148 4
    }
149
}
150