Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fixed issues with incremental indexer command #561

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 39 additions & 15 deletions app/Console/Commands/Indexer/IncrementalIndexer.php
irfan-dahir marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
namespace App\Console\Commands\Indexer;

use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;

class IncrementalIndexer extends Command
class IncrementalIndexer extends Command implements PromptsForMissingInput
{
/**
* @var bool
*/
private bool $cancelled = false;
private int $receivedSignal = 0;

/**
* The name and signature of the console command.
Expand Down Expand Up @@ -54,10 +56,10 @@
return [];
}

$this->info("Fetching MAL ID Cache https://raw.githubusercontent.com/purarue/mal-id-cache/master/cache/${mediaType}_cache.json...");

Check warning on line 59 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L59

Added line #L59 was not covered by tests
$newIdsRaw = file_get_contents("https://raw.githubusercontent.com/purarue/mal-id-cache/master/cache/${mediaType}_cache.json");
$newIdsHash = sha1($newIdsRaw);

/** @noinspection PhpConditionAlreadyCheckedInspection */
if ($this->cancelled)
{
return [];
Expand Down Expand Up @@ -91,19 +93,20 @@
return json_decode(Storage::get("indexer/incremental/{$mediaType}_failed.json"));
}

private function fetchIds(string $mediaType, array $idsToFetch, bool $resume): void
private function fetchIds(string $mediaType, array $idsToFetch, int $delay, bool $resume): void

Check warning on line 96 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L96

Added line #L96 was not covered by tests
{
$index = 0;
$success = [];
$failedIds = [];
$idCount = count($idsToFetch);

if ($resume && Storage::exists("indexer/incremental/{$mediaType}_resume.save"))
{
$index = (int)Storage::get("indexer/incremental/{$mediaType}_resume.save");
$this->info("Resuming from index: $index");
}

$ids = array_merge($idsToFetch['sfw'], $idsToFetch['nsfw']);
$idCount = count($ids);

Check warning on line 109 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L109

Added line #L109 was not covered by tests

if ($index > 0 && !isset($ids[$index]))
{
Expand All @@ -119,10 +122,11 @@
{
if ($this->cancelled)
{
$this->info("Cancelling...");

Check warning on line 125 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L125

Added line #L125 was not covered by tests
return;
}

$id = $ids[$index];
$id = $ids[$i];

Check warning on line 129 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L129

Added line #L129 was not covered by tests

$url = env('APP_URL') . "/v4/$mediaType/$id";
$this->info("Indexing/Updating " . ($i + 1) . "/$idCount $url [MAL ID: $id]");
Expand All @@ -142,6 +146,16 @@
$this->warn("[SKIPPED] Failed to fetch $url");
$failedIds[] = $id;
Storage::put("indexer/incremental/$mediaType.failed", json_encode($failedIds));
continue;

Check warning on line 149 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L149

Added line #L149 was not covered by tests
}
finally
{
cancellable_sleep($delay * 1000, fn() => $this->cancelled);
if ($this->cancelled)

Check warning on line 154 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L153-L154

Added lines #L153 - L154 were not covered by tests
{
$this->info("Cancelling...");
return;

Check warning on line 157 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L156-L157

Added lines #L156 - L157 were not covered by tests
}
}

$success[] = $id;
Expand All @@ -168,27 +182,35 @@
[
'mediaType' => $this->argument('mediaType'),
'delay' => $this->option('delay'),
'resume' => $this->option('resume') ?? false,
'failed' => $this->option('failed') ?? false
'resume' => $this->option('resume'),
'failed' => $this->option('failed')

Check warning on line 186 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L185-L186

Added lines #L185 - L186 were not covered by tests
],
[
'mediaType' => 'required|in:anime,manga',
'mediaType' => 'required|array',
'mediaType.*' => 'in:anime,manga',

Check warning on line 190 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L189-L190

Added lines #L189 - L190 were not covered by tests
'delay' => 'integer|min:1',
'resume' => 'bool|prohibited_with:failed',
'failed' => 'bool|prohibited_with:resume'
'resume' => 'bool',
'failed' => 'bool'

Check warning on line 193 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L192-L193

Added lines #L192 - L193 were not covered by tests
]
);

if ($validator->fails()) {
if ($validator->fails())

Check warning on line 197 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L197

Added line #L197 was not covered by tests
{
$this->error($validator->errors()->toJson());
return 1;
}

// we want to handle signals from the OS
$this->trap([SIGTERM, SIGQUIT, SIGINT], fn () => $this->cancelled = true);
$this->trap([SIGTERM, SIGQUIT, SIGINT], function (int $signal) {
$this->cancelled = true;
$this->receivedSignal = $signal;
});

Check warning on line 207 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L204-L207

Added lines #L204 - L207 were not covered by tests

$this->info("Info: IncrementalIndexer uses purarue/mal-id-cache fetch available MAL IDs and updates/indexes them\n\n");

Check warning on line 209 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L209

Added line #L209 was not covered by tests

$resume = $this->option('resume') ?? false;
$onlyFailed = $this->option('failed') ?? false;
$delay = $this->option('delay') ?? 3;

Check warning on line 213 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L213

Added line #L213 was not covered by tests

/**
* @var $mediaTypes array
Expand All @@ -211,18 +233,20 @@

if ($this->cancelled)
{
return 127;
$this->info("Cancelling...");
return 128 + $this->receivedSignal;

Check warning on line 237 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L236-L237

Added lines #L236 - L237 were not covered by tests
}

$idCount = count($idsToFetch);
if ($idCount === 0)
{
$this->info("No $mediaType entries to index");

Check warning on line 243 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L243

Added line #L243 was not covered by tests
continue;
}

$this->fetchIds($mediaType, $idsToFetch, $resume);
$this->fetchIds($mediaType, $idsToFetch, $delay, $resume);

Check warning on line 247 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L247

Added line #L247 was not covered by tests
}

return 0;
return $this->cancelled && $this->receivedSignal > 0 ? 128 + $this->receivedSignal : 0;

Check warning on line 250 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L250

Added line #L250 was not covered by tests
}
}
9 changes: 8 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@
use App\Support\DefaultMediator;
use App\Support\JikanConfig;
use App\Support\JikanUnitOfWork;
use Illuminate\Console\Signals;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Http\JsonResponse;
use Laravel\Lumen\Application;
use Illuminate\Http\Response;
use Illuminate\Support\Env;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Collection;
use Jikan\MyAnimeList\MalClient;
use Laravel\Scout\Builder as ScoutBuilder;
use Typesense\LaravelTypesense\Typesense;
use App\Features;
Expand Down Expand Up @@ -96,6 +96,13 @@
$this->app->singleton(\App\Services\TypesenseCollectionDescriptor::class);
}
$this->registerModelRepositories();

// lumen hack for signal handling in artisan commands
Signals::resolveAvailabilityUsing(function () {
return $this->app->runningInConsole()
&& ! $this->app->runningUnitTests()
&& extension_loaded('pcntl');

Check warning on line 104 in app/Providers/AppServiceProvider.php

View check run for this annotation

Codecov / codecov/patch

app/Providers/AppServiceProvider.php#L102-L104

Added lines #L102 - L104 were not covered by tests
irfan-dahir marked this conversation as resolved.
Show resolved Hide resolved
});
}

private function getSearchService(Repository $repository): SearchService
Expand Down
18 changes: 18 additions & 0 deletions app/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,21 @@
return $value;
}
}

if (!function_exists("cancellable_sleep")) {
function cancellable_sleep(int $milliseconds, callable $isCancelled): void {
$interval = 100; // check every 100 ms
$elapsed = 0;

Check warning on line 130 in app/Support/helpers.php

View check run for this annotation

Codecov / codecov/patch

app/Support/helpers.php#L129-L130

Added lines #L129 - L130 were not covered by tests

while ($elapsed < $milliseconds)

Check warning on line 132 in app/Support/helpers.php

View check run for this annotation

Codecov / codecov/patch

app/Support/helpers.php#L132

Added line #L132 was not covered by tests
{
if ($isCancelled())

Check warning on line 134 in app/Support/helpers.php

View check run for this annotation

Codecov / codecov/patch

app/Support/helpers.php#L134

Added line #L134 was not covered by tests
{
return;

Check warning on line 136 in app/Support/helpers.php

View check run for this annotation

Codecov / codecov/patch

app/Support/helpers.php#L136

Added line #L136 was not covered by tests
}

usleep($interval * 1000);
$elapsed += $interval;

Check warning on line 140 in app/Support/helpers.php

View check run for this annotation

Codecov / codecov/patch

app/Support/helpers.php#L139-L140

Added lines #L139 - L140 were not covered by tests
}
}
}