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) { |
|
|
|
|
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) { |
|
|
|
|
61
|
|
|
if ($this->crawler->multiple) { |
|
|
|
|
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; |
|
|
|
|
126
|
|
|
$crawlerstatuslog->output = $output; |
|
|
|
|
127
|
|
|
|
128
|
|
|
$crawlerstatuslog->save(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return true; |
132
|
|
|
} else { |
133
|
|
|
throw new \Exception('Cannot save crawlerstatus to database...'); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.