From 76f0b8482721fad2e0c460839a3050de99225e2a Mon Sep 17 00:00:00 2001 From: Attila Kerekes Date: Tue, 22 Nov 2022 00:05:57 +0100 Subject: [PATCH] feat: Move app update to background job --- app/Application.php | 20 +++++-------- app/Jobs/ProcessApps.php | 3 +- app/Jobs/UpdateApps.php | 45 ++++++++++++++++++++++++++++ app/Providers/AppServiceProvider.php | 28 ++++++----------- app/SupportedApps.php | 16 ++++++++-- 5 files changed, 78 insertions(+), 34 deletions(-) create mode 100644 app/Jobs/UpdateApps.php diff --git a/app/Application.php b/app/Application.php index 2d13411d..88c10c16 100644 --- a/app/Application.php +++ b/app/Application.php @@ -120,18 +120,14 @@ class Application extends Model $application = ($localapp) ? $localapp : new self; - if (! file_exists(app_path('SupportedApps/'.className($app->name)))) { - SupportedApps::getFiles($app); - SupportedApps::saveApp($app, $application); - } else { - // check if there has been an update for this app - if ($localapp) { - if ($localapp->sha !== $app->sha) { - SupportedApps::getFiles($app); - $app = SupportedApps::saveApp($app, $application); - } - } else { - SupportedApps::getFiles($app); + // Files missing? || app not in db || old sha version + if ( + ! file_exists(app_path('SupportedApps/'.className($app->name))) || + ! $localapp || + $localapp->sha !== $app->sha + ) { + $gotFiles = SupportedApps::getFiles($app); + if($gotFiles) { $app = SupportedApps::saveApp($app, $application); } } diff --git a/app/Jobs/ProcessApps.php b/app/Jobs/ProcessApps.php index 17510654..6dadc32f 100644 --- a/app/Jobs/ProcessApps.php +++ b/app/Jobs/ProcessApps.php @@ -6,6 +6,7 @@ use App\Application; use App\Item; use App\SupportedApps; use Illuminate\Bus\Queueable; +use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; @@ -13,7 +14,7 @@ use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; -class ProcessApps implements ShouldQueue +class ProcessApps implements ShouldQueue, ShouldBeUnique { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; diff --git a/app/Jobs/UpdateApps.php b/app/Jobs/UpdateApps.php new file mode 100644 index 00000000..32cad92e --- /dev/null +++ b/app/Jobs/UpdateApps.php @@ -0,0 +1,45 @@ +toArray(); + + // We onl update the apps that are actually in use by items + // 1 sec delay after each update to throttle the requests + foreach ($apps as $appKey => $app) { + Application::getApp($app['appid']); + sleep(1); + } + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 5a92ca39..31e24a3b 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -3,14 +3,13 @@ namespace App\Providers; use App\Application; -use App\Item; use App\Jobs\ProcessApps; +use App\Jobs\UpdateApps; use App\Setting; use App\User; use Illuminate\Support\Facades\Artisan; -use Illuminate\Support\Facades\Log; -use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Schema; +use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { @@ -21,6 +20,10 @@ class AppServiceProvider extends ServiceProvider */ public function boot() { + if (! class_exists('ZipArchive')) { + die('You are missing php-zip'); + } + $this->createEnvFile(); $this->setupDatabase(); @@ -31,12 +34,9 @@ class AppServiceProvider extends ServiceProvider } $applications = Application::all(); + if ($applications->count() <= 0) { - if (class_exists('ZipArchive')) { - ProcessApps::dispatch(); - } else { - die('You are missing php-zip'); - } + ProcessApps::dispatch(); } // User specific settings need to go here as session isn't available at this point in the app @@ -170,16 +170,6 @@ class AppServiceProvider extends ServiceProvider */ private function updateApps() { - Log::debug('Update of apps triggered'); - $items = Item::whereNotNull('appid')->get('appid')->toArray(); - $items = array_unique($items, SORT_REGULAR); - - // We onl update the apps that are actually in use by items - // 1 sec delay after each update to throttle the requests - // Todo: move this to some background task? - foreach ($items as $itemKey => $item) { - Application::getApp($item['appid']); - usleep(250000); - } + UpdateApps::dispatchAfterResponse(); } } diff --git a/app/SupportedApps.php b/app/SupportedApps.php index 2f5742f6..fdc06eb3 100644 --- a/app/SupportedApps.php +++ b/app/SupportedApps.php @@ -126,15 +126,25 @@ abstract class SupportedApps } } - public static function getFiles($app) + /** + * @param $app + * @return bool|false + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public static function getFiles($app): bool { - Log::debug("Download triggered for $app->name"); + Log::debug("Download triggered for ".print_r($app, true)); $zipurl = config('app.appsource').'files/'.$app->sha.'.zip'; $client = new Client(['http_errors' => false, 'timeout' => 60, 'connect_timeout' => 15, 'verify' => false]); $res = $client->request('GET', $zipurl); + // Something went wrong? + if ($res->getStatusCode() !== 200) { + return false; + } + if (! file_exists(app_path('SupportedApps'))) { mkdir(app_path('SupportedApps'), 0777, true); } @@ -150,7 +160,9 @@ abstract class SupportedApps unlink($src); //Deleting the Zipped file } else { var_dump($x); + return false; } + return true; } public static function saveApp($details, $app)