添加 更新锁

This commit is contained in:
iVampireSP.com 2023-03-02 12:30:54 +08:00
parent b24f26866b
commit c2e2185109
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132

View File

@ -6,6 +6,7 @@
use App\Jobs\Host\HostJob; use App\Jobs\Host\HostJob;
use App\Models\Host; use App\Models\Host;
use App\Notifications\WebNotification; use App\Notifications\WebNotification;
use Illuminate\Support\Facades\Cache;
class HostObserver class HostObserver
{ {
@ -41,8 +42,15 @@ public function created(Host $host): void
/** /**
* Handle the Host "updated" event. * Handle the Host "updated" event.
*/ */
public function updating(Host $host): void public function updating(Host $host): bool
{ {
// 取得锁
$lock = Cache::lock('host:updating:'.$host->id, 10);
if (! $lock->get()) {
return false;
}
if ($host->isDirty('status')) { if ($host->isDirty('status')) {
if ($host->status == 'suspended') { if ($host->status == 'suspended') {
$host->suspended_at = now(); $host->suspended_at = now();
@ -69,6 +77,11 @@ public function updating(Host $host): void
} }
broadcast(new Users($host->user_id, 'hosts.updating', $host)); broadcast(new Users($host->user_id, 'hosts.updating', $host));
// 释放锁
$lock->release();
return true;
} }
public function updated(Host $host): void public function updated(Host $host): void