Skip to content

Commit

Permalink
chore: cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
leohubert committed Aug 25, 2022
1 parent 181b778 commit e0f861f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 19 deletions.
22 changes: 9 additions & 13 deletions app/Jobs/ProcessModPackFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

use App\Events\ModPack\ModPackProcessProgress;
use App\Models\Modpack;
use App\Services\Modpacks\ModpackUpdaterService;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Throwable;
Expand Down Expand Up @@ -64,19 +64,15 @@ public function handle()
$this->modpack->path,
$this->modpack->name
);
$filePathPrevented = Str::of($filePath)
->replace('.', '-');

Redis::hIncrBy("modpackManifestInfoUpdate:{$this->modpack->id}", 'size', $fileSize);
Redis::hIncrBy("modpackManifestInfoUpdate:{$this->modpack->id}", 'files', 1);

Redis::hSet("modpackManifestUpdate:{$this->modpack->id}", $filePathPrevented, json_encode([
'url' => $fileUrl,
'size' => $fileSize,
'name' => $fileName,
'path' => $filePath,
'sha256' => $fileHash
]));
ModpackUpdaterService::fileProcessed(
modPack: $this->modpack,
fileName: $fileName,
fileSize: $fileSize,
fileUrl: $fileUrl,
filePath: $filePath,
fileHash: $fileHash
);

ModPackProcessProgress::broadcast($this->modpack, $this->batch()->progress());
}
Expand Down
10 changes: 4 additions & 6 deletions app/Listeners/Modpack/StartModPackUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
use App\Events\ModPack\ModPackProcessStarted;
use App\Events\ModPack\ModPackUpdateRequested;
use App\Jobs\ProcessModPackFile;
use App\Services\Modpacks\ModpackUpdaterService;
use Exception;
use Illuminate\Bus\Batch;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;
use Throwable;
use function React\Promise\map;

class StartModPackUpdate implements ShouldQueue
{
Expand All @@ -40,8 +39,7 @@ public function handle(ModPackUpdateRequested $event)
return true;
}

Redis::del("modpackManifestUpdate:$modpack->id");
Redis::del("modpackManifestInfoUpdate:$modpack->id");
ModpackUpdaterService::flush($modpack);

$jobs = $files->map(fn($file) => new ProcessModPackFile($modpack, $file));
$batch = Bus::batch($jobs->toArray())
Expand All @@ -50,8 +48,8 @@ public function handle(ModPackUpdateRequested $event)
ModPackProcessCanceled::broadcast($modpack);
return;
}
$manifest = Redis::hGetAll("modpackManifestUpdate:$modpack->id");
$manifestInfo = Redis::hGetAll("modpackManifestInfoUpdate:$modpack->id");

[$manifest, $manifestInfo] = ModpackUpdaterService::getUpdate($modpack);

if (!empty($manifest) && !empty($manifestInfo)) {
$modpack->update([
Expand Down
63 changes: 63 additions & 0 deletions app/Services/Modpacks/ModpackUpdaterService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace App\Services\Modpacks;

use App\Models\Modpack;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Str;

class ModpackUpdaterService
{
private static function getManifestKey(Modpack $modpack): string
{
return "modPack:manifestUpdate:$modpack->id";
}

private static function getManifestInfoKey(Modpack $modpack): string
{
return "modPack:manifestInfoUpdate:$modpack->id";
}


static public function flush(Modpack $modpack): void
{
Redis::del(self::getManifestKey($modpack));
Redis::del(self::getManifestInfoKey($modpack));
}

static public function getUpdate(Modpack $modpack): array
{
return [
Redis::hGetAll(self::getManifestKey($modpack)),
Redis::hGetAll(self::getManifestInfoKey($modpack)),
];
}

public static function fileProcessed(
Modpack $modPack,
string $fileName,
int $fileSize,
string $fileUrl,
string $filePath,
string $fileHash,
)
{
$manifestKey = self::getManifestKey($modPack);
$manifestInfoKey = self::getManifestInfoKey($modPack);

$safeFilePath = Str::of($filePath)
->replace('.', '-');


Redis::hIncrBy($manifestInfoKey, 'size', $fileSize);
Redis::hIncrBy($manifestInfoKey, 'files', 1);

Redis::hSet($manifestKey, $safeFilePath, json_encode([
'url' => $fileUrl,
'size' => $fileSize,
'name' => $fileName,
'path' => $filePath,
'sha256' => $fileHash
]));
}
}

0 comments on commit e0f861f

Please sign in to comment.