Completed
Push — master ( aa84bd...826843 )
by Robin
15:54 queued 13:13
created

CrawlController::startCrawler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Famdirksen\LaravelJobHandler\Http\Controllers;
4
5
use Famdirksen\LaravelJobHandler\Models\Crawlers;
6
use Famdirksen\LaravelJobHandler\Models\CrawlerStatus;
7
use Famdirksen\LaravelJobHandler\Models\CrawlerStatusLogs;
8
9
class CrawlController
10
{
11
    protected $crawler;
12
    protected $crawler_id;
13
14
15
    /**
16
     * Get the latest crawler data from the database
17
     */
18
    protected function getCrawler()
19
    {
20
        $this->crawler = Crawlers::findOrFail($this->crawler_id);
21
    }
22
23
    /**
24
     * Setup the crawler so it won't run twice at the same time
25
     *
26
     * @param $crawler_id
27
     */
28
    public function setupCrawler($crawler_id)
29
    {
30
        $this->crawler_id = $crawler_id;
31
        $times = config('laravel-job-handler.run_times', 10);
32
33
        for ($x = 0; $x <= $times; $x++) {
34
            //fetch the last data
35
            $this->getCrawler();
36
37
            if (!$this->crawler->enabled) {
38
                throw new \Exception('Crawler (#'.$this->crawler_id.') - crawler isnt enabled in database');
39
            }
40
            if ($this->crawler->latest_status == 4) {
0 ignored issues
show
Bug introduced by
The property latest_status does not seem to exist on Famdirksen\LaravelJobHandler\Models\Crawlers. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
41
                throw new \Exception('Crawler (#'.$this->crawler_id.') - last run had an error and the developer needs to reenable this crawler');
42
            }
43
            if ($this->crawler->latest_status == 3) {
44
                throw new \Exception('Crawler (#'.$this->crawler_id.') - last run had an error');
45
            }
46
            if ($this->crawler->latest_status == 2) {
47
                //Done running...
48
                break;
49
            }
50
51
            if (is_null($this->crawler->latest_status)) {
52
                //first time it runs...
53
                break;
54
            }
55
56
            if ($x == $times) {
57
                $this->failCrawler('Crawler (#'.$this->crawler_id.') - max execution time');
58
            }
59
60
            if ($this->crawler->status == 1) {
0 ignored issues
show
Bug introduced by
The property status does not seem to exist on Famdirksen\LaravelJobHandler\Models\Crawlers. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
61
                if ($this->crawler->multiple) {
0 ignored issues
show
Bug introduced by
The property multiple does not seem to exist on Famdirksen\LaravelJobHandler\Models\Crawlers. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
62
                    break;
63
                }
64
65
                sleep(config('laravel-job-handler.retry_in_seconds', 3)); //retry in 3 seconds
66
            }
67
        }
68
69
        $this->startCrawler();
70
    }
71
    /**
72
     * Start the crawler and save it to the database
73
     *
74
     * @param string $output
75
     */
76
    public function startCrawler($output = '')
77
    {
78
        return $this->addStatus(1, $output); //start running
79
    }
80
    /**
81
     * set the crawler as done so other scripts can run
82
     *
83
     * @param string $output
84
     */
85
    public function doneCrawler($output = '')
86
    {
87
        return $this->addStatus(2, $output); //done running
88
    }
89
    public function finish($output = '') {
90
        return $this->doneCrawler($output);
91
    }
92
    /**
93
     * crawler failed...
94
     *
95
     * @param string $output
96
     */
97
    public function failCrawler($output = '')
98
    {
99
        $this->addStatus(3, $output); //failed
100
101
        throw new \Exception($output.' - status 3');
102
    }
103
    /**
104
     * save the latest crawler status to the database
105
     *
106
     * @param $status
107
     * @param string $output
108
     * @return bool
109
     */
110
    protected function addStatus($status, $output = '')
111
    {
112
        $crawlerstatus = new CrawlerStatus();
113
114
        $crawlerstatus->crawler_id = $this->crawler_id;
115
        $crawlerstatus->status = $status;
116
117
        if ($crawlerstatus->save()) {
118
            $this->crawler->latest_status = $status;
119
120
            $this->crawler->save();
121
122
            if (!empty($output)) {
123
                $crawlerstatuslog = new CrawlerStatusLogs();
124
125
                $crawlerstatuslog->status_id = $crawlerstatus->id;
0 ignored issues
show
Bug introduced by
The property status_id does not seem to exist on Famdirksen\LaravelJobHan...odels\CrawlerStatusLogs. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
126
                $crawlerstatuslog->output = $output;
0 ignored issues
show
Bug introduced by
The property output does not seem to exist on Famdirksen\LaravelJobHan...odels\CrawlerStatusLogs. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
127
128
                $crawlerstatuslog->save();
129
            }
130
131
            return true;
132
        } else {
133
            throw new \Exception('Cannot save crawlerstatus to database...');
134
        }
135
    }
136
}
137