Completed
Push — typo3v9 ( 0dcd72...98ac46 )
by Tomas Norre
06:11
created

ProcessQueueCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 101
ccs 0
cts 63
cp 0
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

2 Methods

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