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
|
6 |
|
public function __construct(Schedule $schedule) |
45
|
|
|
{ |
46
|
6 |
|
parent::__construct(); |
47
|
6 |
|
$this->schedule = $schedule; |
48
|
6 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Execute the console command. |
52
|
|
|
* |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
public function handle() |
56
|
|
|
{ |
57
|
|
|
// |
58
|
|
|
//TODO insert a timeout |
59
|
|
|
//TODO try...catch with destruct |
60
|
|
|
$companyId = $this->argument('companyId'); |
61
|
|
|
if(is_numeric($companyId)) |
62
|
|
|
$this->makeForeground(Company::findOrFail($companyId)); |
63
|
|
|
else |
64
|
|
|
$this->syncAll(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function syncAll() |
68
|
|
|
{ |
69
|
|
|
$calendars = Caldav::all(); |
70
|
|
|
if($this->option('background')) { |
71
|
|
|
\Log::debug(self::BACKGROUND_MOD_MEX); |
72
|
|
|
$this->info(self::BACKGROUND_MOD_MEX); |
73
|
|
|
foreach ($calendars as $calendar) |
74
|
|
|
$this->makeBackground($calendar); |
75
|
|
|
\Log::debug(self::BACKGROUND_COMPLETED_MEX); |
76
|
|
|
$this->info(self::BACKGROUND_COMPLETED_MEX); |
77
|
|
|
}else |
78
|
|
|
foreach($calendars as $calendar) |
79
|
|
|
$this->makeForeground($calendar); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* optimise company via exec command |
84
|
|
|
* @param Company $company |
85
|
|
|
*/ |
86
|
|
|
private function makeBackground(Company $company) |
87
|
|
|
{ |
88
|
|
|
$event = $this->schedule->command('optimise:meetings '.$company->id)->withoutOverlapping(); |
89
|
|
|
if($event->isDue($this->laravel)) |
90
|
|
|
$event->run($this->laravel); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* optimise company foreground |
95
|
|
|
* @param Company $company |
96
|
|
|
*/ |
97
|
|
|
private function makeForeground(Company $company) |
98
|
|
|
{ |
99
|
|
|
$this->info('Optimisation company '. $company->id.' started'); |
100
|
|
|
(new Optimise($company, $this->schedule, $this->laravel))->optimise()->save(); |
101
|
|
|
$this->info('Optimisation '. $company->id.' completed'); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|