1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace plunner\Console\Commands\Optimise; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Console\Scheduling\Schedule; |
7
|
|
|
use plunner\Company; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class OptimiseCommand |
11
|
|
|
* @package plunner\Console\Commands\Optimise |
12
|
|
|
* @author Claudio Cardinale <[email protected]> |
13
|
|
|
* @copyright 2015 Claudio Cardinale |
14
|
|
|
* @version 1.0.0 |
15
|
|
|
*/ |
16
|
|
|
class OptimiseCommand extends Command |
17
|
|
|
{ |
18
|
|
|
const BACKGROUND_MOD_MEX = 'background mode'; |
19
|
|
|
const BACKGROUND_COMPLETED_MEX = 'All background tasks started'; |
20
|
|
|
/** |
21
|
|
|
* The name and signature of the console command. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $signature = 'optimise:meetings {companyId?} {--background}'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The console command description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Optimise meetings'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Schedule laravel schedule object needed to perform command in background |
36
|
|
|
*/ |
37
|
|
|
private $schedule; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create a new command instance. |
41
|
|
|
* @param Schedule $schedule |
42
|
|
|
* |
43
|
|
|
*/ |
44
|
15 |
|
public function __construct(Schedule $schedule) |
45
|
|
|
{ |
46
|
15 |
|
parent::__construct(); |
47
|
15 |
|
$this->schedule = $schedule; |
48
|
15 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Execute the console command. |
52
|
|
|
* |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
6 |
|
public function handle() |
56
|
|
|
{ |
57
|
|
|
// |
58
|
|
|
//TODO insert a timeout |
59
|
|
|
//TODO try...catch with destruct |
60
|
6 |
|
$companyId = $this->argument('companyId'); |
61
|
6 |
|
if (is_numeric($companyId)) |
62
|
5 |
|
$this->makeForeground(Company::findOrFail($companyId)); |
63
|
|
|
else |
64
|
3 |
|
$this->syncAll(); |
65
|
3 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* optimise company foreground |
69
|
|
|
* @param Company $company |
70
|
|
|
*/ |
71
|
6 |
|
private function makeForeground(Company $company) |
72
|
|
|
{ |
73
|
6 |
|
$this->info('Optimisation company ' . $company->id . ' started'); |
74
|
|
|
try { |
75
|
6 |
|
(new Optimise($company, $this->schedule, $this->laravel))->optimise()->save(); |
76
|
|
|
$this->info('Optimisation ' . $company->id . ' completed'); |
77
|
6 |
|
}catch(OptimiseException $e) { |
78
|
3 |
|
if ($e->isEmpty()) { |
79
|
3 |
|
$mex = 'Company ' . $company->id . ' doesn\'t have sufficient data'; |
80
|
3 |
|
$this->warn($mex); |
81
|
3 |
|
\Log::info($mex); |
82
|
2 |
|
} else { |
83
|
|
|
$mex = 'Error during optimisation of company ' . $company->id . ': ' . $e->getMessage(); |
84
|
|
|
$this->error($mex); |
85
|
|
|
//TODO log cause, but don't send it to the user |
86
|
|
|
//\Log::error($mex); //already logged in listener |
87
|
|
|
} |
88
|
|
|
} |
89
|
3 |
|
} |
90
|
|
|
|
91
|
3 |
|
private function syncAll() |
92
|
|
|
{ |
93
|
|
|
//TODO if are there no companies? |
94
|
3 |
|
$companies = Company::all(); |
95
|
3 |
|
if ($this->option('background')) { |
96
|
|
|
\Log::debug(self::BACKGROUND_MOD_MEX); |
97
|
|
|
$this->info(self::BACKGROUND_MOD_MEX); |
98
|
|
|
foreach ($companies as $company) |
99
|
|
|
$this->makeBackground($company); |
100
|
|
|
\Log::debug(self::BACKGROUND_COMPLETED_MEX); |
101
|
|
|
$this->info(self::BACKGROUND_COMPLETED_MEX); |
102
|
|
|
} else |
103
|
3 |
|
foreach ($companies as $company) |
104
|
3 |
|
$this->makeForeground($company); |
105
|
3 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* optimise company via exec command |
109
|
|
|
* @param Company $company |
110
|
|
|
*/ |
111
|
2 |
|
private function makeBackground(Company $company) |
112
|
|
|
{ |
113
|
|
|
$event = $this->schedule->command('optimise:meetings ' . $company->id)->withoutOverlapping(); |
114
|
|
|
if ($event->isDue($this->laravel)) |
115
|
|
|
$event->run($this->laravel); |
116
|
2 |
|
} |
117
|
|
|
} |
118
|
|
|
|