Skip to content

Commit f50d3ac

Browse files
committed
SIP-30 - added MIX_HEARTBEAT_ENABLED to disable the heartbeat
- possibility to set a specific key used for the lackable_id per model
1 parent 2619273 commit f50d3ac

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ The more advanced approach is to handle the locks via a heartbeat. This only wor
130130
| `HEARTBEAT_LOCK_DURATION` | 70 | The time in seconds a model is locked. |
131131
| `MIX_HEARTBEAT_REFRESH` | 60 | The time in seconds between the heartbeats. Should be a multiple of the `MIX_HEARTBEAT_STATUS`-interval. |
132132
| `MIX_HEARTBEAT_STATUS` | 30 | The time in seconds between the heartbeats for status-request (index-Listener). |
133+
| `MIX_HEARTBEAT_ENABLED` | true | Activates or deactivates the heartbeats. |
133134

134135
### config
135136
Beside the environment variables, there is a `middleware` key to determine the middleware(s) used by the heartbeat-route. Default it's set to `api`.

resources/js/heartbeat-manager.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const HeartbeatManager = {
3838
},
3939

4040
triggerBeat(scheduleBeat = true) {
41+
if (process.env.MIX_HEARTBEAT_ENABLED !== 'true') return;
42+
4143
this.beat.counter = 1;
4244
if (scheduleBeat) {
4345
clearTimeout(this.beat.handle);

src/Http/Controllers/Api/Dtos/Heartbeat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function modelStates()
102102
return $this->lockables()->map(function($lockable) {
103103
/** @var Model&IsLockable $lockable */
104104
return [
105-
'lockable_id' => $lockable->getKey(),
105+
'lockable_id' => $lockable->{$lockable::lockableIdField()},
106106
'lockable_type' => $lockable::class,
107107
'locked_at' => $lockable->locked_at,
108108
'locked_by' => $lockable->locked_by,

src/Http/Controllers/Api/Dtos/HeartbeatCollection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ public function lockables(string $modelClass, array $ids): Collection
4444
if ($modelCollection = data_get($this->models, $modelClass)) {
4545
$emptyModel = new $modelClass();
4646
/** @var \Illuminate\Database\Eloquent\Collection $models */
47-
$models = $modelCollection->whereIn($emptyModel->getKeyName(), $ids);
47+
$models = $modelCollection->whereIn($modelClass::$lockableIdField, $ids);
4848

4949
if ($models->count()) return $models;
5050
}
5151

52-
return $modelClass::find($ids);
52+
return $modelClass::whereIn($modelClass::$lockableIdField, $ids)->get();
5353
}
5454

5555
/**
@@ -77,7 +77,7 @@ public function handle(): self
7777
* @var array $ids
7878
*/
7979
foreach($modelsToFetch as $modelClass => $ids) {
80-
$this->models[$modelClass] = $modelClass::find($ids);
80+
$this->models[$modelClass] = $modelClass::whereIn($modelClass::$idField, $ids)->get();
8181
}
8282

8383
// handle the tasks

src/Models/Concerns/IsLockable.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ public function initializeIsLockable()
3232
$this->appends[] = 'locked_at';
3333
}
3434

35+
public static function lockableIdField(): string
36+
{
37+
if (property_exists(self::class, 'lockableIdField')) return self::lockableIdField;
38+
39+
return 'id';
40+
}
41+
3542
/**
3643
* Locks model.
3744
*
@@ -129,7 +136,7 @@ public function isLockedBy(Authenticatable|string|int|null $user = null, bool $s
129136

130137
return (string) $this->releaseLockIfExpired($saveOnRelease)->locked_by === (string) $userId;
131138
}
132-
139+
133140
public function isLockedByUser(): BelongsTo
134141
{
135142
$emptyUser = new (config('auth.providers.users.model', User::class))();
@@ -202,4 +209,4 @@ protected function releaseLockIfExpired(bool $saveOnRelease = false): self
202209

203210
return $this;
204211
}
205-
}
212+
}

0 commit comments

Comments
 (0)