-
Notifications
You must be signed in to change notification settings - Fork 250
Expand file tree
/
Copy pathDevLinkInstall.php
More file actions
132 lines (111 loc) · 4.53 KB
/
Copy pathDevLinkInstall.php
File metadata and controls
132 lines (111 loc) · 4.53 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace ProcessMaker\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Client\RequestException;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use ProcessMaker\Exception\DevLinkRemoteBundleException;
use ProcessMaker\Exception\DevLinkRemoteValidationException;
use ProcessMaker\Exception\ValidationException;
use ProcessMaker\ImportExport\Logger;
use ProcessMaker\Jobs\ImportV2;
use ProcessMaker\Models\Bundle;
use ProcessMaker\Models\DevLink;
use Throwable;
class DevLinkInstall implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private const REMOTE_ERROR_MESSAGE = 'The remote instance could not complete the DevLink request. Check the source instance logs and try again.';
private const UNEXPECTED_ERROR_MESSAGE = 'The DevLink operation could not be completed. Check the target instance logs and try again.';
const TYPE_INSTALL_BUNDLE = 'install_bundle';
const TYPE_REINSTALL_BUNDLE = 'reinstall_bundle';
const TYPE_IMPORT_ASSET = 'import_asset';
const MODE_UPDATE = 'update';
const MODE_COPY = 'copy';
public $tries = 1;
public $maxExceptions = 1;
/**
* Correlates progress events with the DevLink operation that started them.
*
* This remains nullable so jobs queued before this property was introduced
* can still be processed after an application upgrade.
*/
public $operationId = null;
public function __construct(
public int $userId,
public int $devLinkId,
public string $class,
public int $id,
public string $importMode,
public string $type,
$operationId = null,
) {
$this->operationId = $operationId;
}
/**
* Execute the job.
*/
public function handle(): void
{
//log
Log::info('DevLinkInstall job started: ' . $this->devLinkId);
$devLink = DevLink::findOrFail($this->devLinkId);
$logger = new Logger($this->userId, $this->operationId);
$lock = Cache::lock(ImportV2::CACHE_LOCK_KEY, ImportV2::RELEASE_LOCK_AFTER);
if ($lock->get()) {
DB::transaction(function () use ($devLink, $logger) {
switch($this->type) {
case self::TYPE_INSTALL_BUNDLE:
$devLink->logger = $logger;
$devLink->installRemoteBundle($this->id, $this->importMode);
break;
case self::TYPE_REINSTALL_BUNDLE:
$bundle = Bundle::findOrFail($this->id);
$bundle->reinstall($this->importMode, $logger);
break;
case self::TYPE_IMPORT_ASSET:
$devLink->installRemoteAsset($this->class, $this->id, $this->importMode, $logger);
break;
default:
break;
}
});
$lock->release();
} else {
// Don't throw exception because that will unlock the running job
$logger->error('Already running!');
}
}
public function failed(Throwable $exception): void
{
$logger = new Logger($this->userId, $this->operationId);
Log::error('DevLink operation failed.', [
'exception' => $exception,
'operation_id' => $this->operationId,
'dev_link_id' => $this->devLinkId,
'type' => $this->type,
]);
if (
$exception instanceof DevLinkRemoteBundleException
|| $exception instanceof DevLinkRemoteValidationException
|| $exception instanceof ValidationException
) {
$message = $exception instanceof ValidationException
? collect($exception->errors())->flatten()->first(fn ($message) => is_string($message))
: $exception->getMessage();
$logger->error($message ?: $exception->getMessage());
} elseif ($exception instanceof RequestException) {
$logger->error(__(self::REMOTE_ERROR_MESSAGE));
} else {
$logger->error(__(self::UNEXPECTED_ERROR_MESSAGE));
}
// Unlock the job
// We can't use $this->lock->release() here because this is run in a new instance
Cache::lock(ImportV2::CACHE_LOCK_KEY)->forceRelease();
}
}