Skip to content

Commit

Permalink
Shifted watch progress event queueing to mappers, having it in reques…
Browse files Browse the repository at this point in the history
…t process proven to be difficult to balance with endless loops.
  • Loading branch information
arabcoders committed Aug 18, 2024
1 parent 522827f commit a82ebf8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 49 deletions.
12 changes: 8 additions & 4 deletions frontend/pages/events/view.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
</span>
<div class="is-pulled-right">
<div class="field is-grouped">

<p class="control">
<button class="button is-warning" @click="resetEvent" :disabled="item.status < 2">
<span class="icon"><i class="fas fa-trash-arrow-up"></i></span>
<button class="button is-warning" @click="resetEvent(0 === item.status ? 4 : 0)"
:disabled="1 === item.status">
<span class="icon">
<i class="fas" :class="{'fa-trash-arrow-up': 0!== item.status, 'fa-power-off': 0=== item.status}"></i>
</span>
</button>
</p>
<p class="control">
Expand Down Expand Up @@ -175,7 +179,7 @@ const deleteItem = async () => {
}
}
const resetEvent = async () => {
const resetEvent = async (status = 0) => {
if (!confirm(`Reset '${makeName(item.value.id)}'?`)) {
return
}
Expand All @@ -184,7 +188,7 @@ const resetEvent = async () => {
const response = await request(`/system/events/${item.value.id}`, {
method: 'PATCH',
body: JSON.stringify({
status: 0,
status: status,
reset_logs: true,
})
})
Expand Down
11 changes: 8 additions & 3 deletions src/Libs/Mappers/Import/DirectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ private function handleOldEntity(iState $local, iState $entity, array $opts = []

$newPlayProgress = (int)ag($entity->getMetadata($entity->via), iState::COLUMN_META_DATA_PROGRESS);
$oldPlayProgress = (int)ag($cloned->getMetadata($entity->via), iState::COLUMN_META_DATA_PROGRESS);
$playChanged = $newPlayProgress != $oldPlayProgress;
$playChanged = $newPlayProgress > ($oldPlayProgress + 10);

// -- this sometimes leads to never ending updates as data from backends conflicts.
if ($playChanged || true === (bool)ag($this->options, Options::MAPPER_ALWAYS_UPDATE_META)) {
Expand All @@ -422,13 +422,18 @@ private function handleOldEntity(iState $local, iState $entity, array $opts = []
$progress = !$entity->isWatched() && $playChanged && $entity->hasPlayProgress();

if (count($changes) >= 1) {
$_keys = array_merge($keys, [iState::COLUMN_EXTRA]);
if ($playChanged && $progress) {
$_keys[] = iState::COLUMN_VIA;
}
$local = $local->apply($entity, fields: $_keys);
$this->logger->notice(
$progress ? "MAPPER: '{backend}' updated '{title}' due to play progress change." : "MAPPER: '{backend}' updated '{title}' metadata.",
[
'id' => $cloned->id,
'backend' => $entity->via,
'title' => $cloned->getName(),
'changes' => $changes,
'changes' => $progress ? $local->diff(fields: $_keys) : $changes,
]
);
}
Expand All @@ -444,7 +449,7 @@ private function handleOldEntity(iState $local, iState $entity, array $opts = []
'id' => ag($entity->getMetadata($entity->via), iState::COLUMN_ID, '??'),
]);

$this->progressItems[$itemId] = $entity;
$this->progressItems[$itemId] = $local;
}
}

Expand Down
15 changes: 11 additions & 4 deletions src/Libs/Mappers/Import/MemoryMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private function handleOldEntity(string|int $pointer, iState $cloned, iState $en

$newPlayProgress = (int)ag($entity->getMetadata($entity->via), iState::COLUMN_META_DATA_PROGRESS);
$oldPlayProgress = (int)ag($cloned->getMetadata($entity->via), iState::COLUMN_META_DATA_PROGRESS);
$playChanged = $newPlayProgress != $oldPlayProgress;
$playChanged = $newPlayProgress > ($oldPlayProgress + 10);

// -- this sometimes leads to never ending updates as data from backends conflicts.
if ($playChanged || true === (bool)ag($this->options, Options::MAPPER_ALWAYS_UPDATE_META)) {
Expand All @@ -295,16 +295,23 @@ private function handleOldEntity(string|int $pointer, iState $cloned, iState $en
$this->removePointers($cloned)->addPointers($this->objects[$pointer], $pointer);

$changes = $this->objects[$pointer]->diff(fields: $keys);

$progress = !$entity->isWatched() && $playChanged && $entity->hasPlayProgress();

if (count($changes) >= 1) {
$_keys = array_merge($keys, [iState::COLUMN_EXTRA]);
if ($playChanged && $progress) {
$_keys[] = iState::COLUMN_VIA;
}

$this->objects[$pointer] = $this->objects[$pointer]->apply(entity: $entity, fields: $_keys);

$this->logger->notice(
$progress ? "MAPPER: '{backend}' updated '{title}' due to play progress change." : "MAPPER: '{backend}' updated '{title}' metadata.",
[
'id' => $cloned->id,
'backend' => $entity->via,
'title' => $cloned->getName(),
'changes' => $changes,
'changes' => $progress ? $this->objects[$pointer]->diff(fields: $_keys) : $changes,
'fields' => implode(',', $keys),
]
);
Expand All @@ -317,7 +324,7 @@ private function handleOldEntity(string|int $pointer, iState $cloned, iState $en
'id' => ag($entity->getMetadata($entity->via), iState::COLUMN_ID, '??'),
]);

$this->progressItems[$itemId] = $entity;
$this->progressItems[$itemId] = $this->objects[$pointer];
}
}

Expand Down
24 changes: 4 additions & 20 deletions src/Listeners/ProcessProgressEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,21 @@ public function __invoke(DataEvent $e): DataEvent
$e->stopPropagation();

$options = $e->getOptions();
$entity = Container::get(iState::class)::fromArray($e->getData());

if (null === ($item = $this->db->get($entity))) {
$writer(Level::Error, "Item '{title}' Is not referenced locally yet.", ['title' => $entity->getName()]);
if (null === ($item = $this->db->get(Container::get(iState::class)::fromArray($e->getData())))) {
$writer(Level::Error, "Item '{id}' Is not referenced locally yet.", ['id' => ag($e->getData(), 'id', '?')]);
return $e;
}

if ($item->isWatched() || $entity->isWatched()) {
if ($item->isWatched()) {
$writer(Level::Info, "Item '{id}: {title}' is marked as watched. Not updating watch process.", [
'id' => $item->id,
'title' => $item->getName()
]);
return $e;
}

if ($item->hasPlayProgress() && ($item->getPlayProgress() + 10) >= $entity->getPlayProgress()) {
$writer(
Level::Info, "Local item '{id}: {title}' has higher/equal progress to the event item. not processing.",
[
'id' => $item->id,
'title' => $item->title,
'local' => formatDuration($item->getPlayProgress()) . ' +10s',
'event' => formatDuration($entity->getPlayProgress()),
]
);
return $e;
}

$item = $item->apply($entity);

if (!$item->hasPlayProgress()) {
if (false === $item->hasPlayProgress()) {
$writer(Level::Info, "Item '{title}' has no watch progress to export.", ['title' => $item->title]);
return $e;
}
Expand Down
18 changes: 0 additions & 18 deletions src/Listeners/ProcessRequestEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use App\Libs\Mappers\Import\DirectMapper;
use App\Libs\Options;
use App\Model\Events\EventListener;
use App\Model\Events\EventsTable;
use Monolog\Logger;
use Psr\Log\LoggerInterface as iLogger;

Expand Down Expand Up @@ -72,23 +71,6 @@ public function __invoke(DataEvent $e): DataEvent
$this->mapper->commit();
$this->mapper->setLogger($oldLogger);

$pEnabled = (bool)Config::get('sync.progress', false);
if (true === $pEnabled && true === $entity->hasPlayProgress() && !$entity->isWatched()) {
$logger->notice(r("Scheduling '{title}' for watch progress update via '{backend}' event.", [
'backend' => $entity->via,
'title' => $entity->getName(),
]));

queueEvent(ProcessProgressEvent::NAME, $entity->getAll(), [
'unique' => true,
EventsTable::COLUMN_REFERENCE => r('{type}://{id}@{backend}', [
'type' => $entity->type,
'backend' => $entity->via,
'id' => ag($entity->getMetadata($entity->via), iState::COLUMN_ID, '??'),
]),
]);
}

$handler->close();

return $e;
Expand Down

0 comments on commit a82ebf8

Please sign in to comment.