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 1 commit
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
56 changes: 45 additions & 11 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 @@ -30,6 +30,23 @@
];
}

private function sleep(int $milliseconds): void

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L33 was not covered by tests
{
$interval = 100; // check every 100 ms
$elapsed = 0;

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

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L35-L36

Added lines #L35 - L36 were not covered by tests

while ($elapsed < $milliseconds)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L38 was not covered by tests
{
if ($this->cancelled)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L40 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we return 1 and use $log->fail() instead?
https://laravel.com/docs/11.x/artisan#exit-codes

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both of us are wrong. https://tldp.org/LDP/abs/html/exitcodes.html
This should be 128 + {the signal received}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The goal is to let the parent process know which signal terminated the process.
E.g. in PHPStorm when I stop the run:
image

{
return;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L42 was not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L45-L46

Added lines #L45 - L46 were not covered by tests
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pushrbx Is this hack specific to docker containers?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, would it make sense to move this to helpers?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanted to have a sleep function which can be cancelled. Anytime you cancel the process, it should gracefully exit.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll move this to helpers.

private function getExistingIds(string $mediaType): array
{
$existingIdsHash = "";
Expand Down Expand Up @@ -91,19 +108,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 111 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

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

Added line #L111 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 124 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

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

Added line #L124 was not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L144 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 +161,16 @@
$this->warn("[SKIPPED] Failed to fetch $url");
$failedIds[] = $id;
Storage::put("indexer/incremental/$mediaType.failed", json_encode($failedIds));
continue;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L164 was not covered by tests
}
finally
{
$this->sleep($delay * 1000);
if ($this->cancelled)

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

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L168-L169

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

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

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L171-L172

Added lines #L171 - L172 were not covered by tests
}
}

$success[] = $id;
Expand All @@ -168,18 +197,20 @@
[
'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 201 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/Indexer/IncrementalIndexer.php#L200-L201

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L204 - L205 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 208 in app/Console/Commands/Indexer/IncrementalIndexer.php

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L207 - L208 were not covered by tests
]
);

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L212 was not covered by tests
{
$this->error($validator->errors()->toJson());
return 1;
}
Expand All @@ -189,6 +220,7 @@

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L223 was not covered by tests

/**
* @var $mediaTypes array
Expand All @@ -211,16 +243,18 @@

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

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#L246-L247

Added lines #L246 - L247 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L257 was not covered by tests
}

return 0;
Expand Down
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