-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Queue.php
112 lines (87 loc) · 3.01 KB
/
Queue.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace App\Providers;
use App\Notifications\Common\ImportFailed;
use App\Traits\Modules;
use Illuminate\Queue\Events\JobFailed;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Support\ServiceProvider as Provider;
class Queue extends Provider
{
use Modules;
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
app('queue')->createPayloadUsing(function ($connection, $queue, $payload) {
$company_id = company_id();
if (empty($company_id)) {
return [];
}
return ['company_id' => $company_id];
});
app('events')->listen(JobProcessing::class, function ($event) {
$payload = $event->job->payload();
if (! array_key_exists('company_id', $payload)) {
return;
}
$company = company($payload['company_id']);
if (empty($company)) {
$event->job->delete();
throw new \Exception('Company not found. Payload: ' . json_encode($payload));
}
$company->makeCurrent();
// TODO: Move queue control at the beginning of the closure
if (should_queue()) {
$this->registerModules();
}
});
app('events')->listen(JobFailed::class, function ($event) {
if (!$event->exception instanceof \Maatwebsite\Excel\Validators\ValidationException) {
return;
}
$body = $event->job->getRawBody();
if (empty($body) || !is_string($body)) {
return;
}
$payload = json_decode($body);
if (empty($payload) || empty($payload->data) || empty($payload->data->command)) {
return;
}
$excel_job = unserialize($payload->data->command);
if (!$excel_job instanceof \Maatwebsite\Excel\Jobs\ReadChunk) {
return;
}
$ref = new \ReflectionProperty($excel_job, 'import');
$ref->setAccessible(true);
// Get import class
$class = $ref->getValue($excel_job);
if (!$class instanceof \App\Abstracts\Import && !$class instanceof \App\Abstracts\ImportMultipleSheets) {
return;
}
$errors = [];
foreach ($event->exception->failures() as $failure) {
$message = trans('messages.error.import_column', [
'message' => collect($failure->errors())->first(),
'column' => $failure->attribute(),
'line' => $failure->row(),
]);
$errors[] = $message;
}
if (!empty($errors)) {
$class->user->notify(new ImportFailed($errors));
}
});
}
}