Merge pull request #1018 from keriati/feat/appupdatejob

feat: Move app update to background job
This commit is contained in:
KodeStar
2022-11-22 09:12:16 +00:00
committed by GitHub
5 changed files with 78 additions and 34 deletions

View File

@@ -120,18 +120,14 @@ class Application extends Model
$application = ($localapp) ? $localapp : new self; $application = ($localapp) ? $localapp : new self;
if (! file_exists(app_path('SupportedApps/'.className($app->name)))) { // Files missing? || app not in db || old sha version
SupportedApps::getFiles($app); if (
SupportedApps::saveApp($app, $application); ! file_exists(app_path('SupportedApps/'.className($app->name))) ||
} else { ! $localapp ||
// check if there has been an update for this app $localapp->sha !== $app->sha
if ($localapp) { ) {
if ($localapp->sha !== $app->sha) { $gotFiles = SupportedApps::getFiles($app);
SupportedApps::getFiles($app); if($gotFiles) {
$app = SupportedApps::saveApp($app, $application);
}
} else {
SupportedApps::getFiles($app);
$app = SupportedApps::saveApp($app, $application); $app = SupportedApps::saveApp($app, $application);
} }
} }

View File

@@ -6,6 +6,7 @@ use App\Application;
use App\Item; use App\Item;
use App\SupportedApps; use App\SupportedApps;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
@@ -13,7 +14,7 @@ use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
class ProcessApps implements ShouldQueue class ProcessApps implements ShouldQueue, ShouldBeUnique
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

45
app/Jobs/UpdateApps.php Normal file
View File

@@ -0,0 +1,45 @@
<?php
namespace App\Jobs;
use App\Application;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class UpdateApps implements ShouldQueue, ShouldBeUnique
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Log::debug('Update of all apps triggered!');
$apps = Application::all('appid')->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);
}
}
}

View File

@@ -3,14 +3,13 @@
namespace App\Providers; namespace App\Providers;
use App\Application; use App\Application;
use App\Item;
use App\Jobs\ProcessApps; use App\Jobs\ProcessApps;
use App\Jobs\UpdateApps;
use App\Setting; use App\Setting;
use App\User; use App\User;
use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
{ {
@@ -21,6 +20,10 @@ class AppServiceProvider extends ServiceProvider
*/ */
public function boot() public function boot()
{ {
if (! class_exists('ZipArchive')) {
die('You are missing php-zip');
}
$this->createEnvFile(); $this->createEnvFile();
$this->setupDatabase(); $this->setupDatabase();
@@ -31,12 +34,9 @@ class AppServiceProvider extends ServiceProvider
} }
$applications = Application::all(); $applications = Application::all();
if ($applications->count() <= 0) { if ($applications->count() <= 0) {
if (class_exists('ZipArchive')) {
ProcessApps::dispatch(); ProcessApps::dispatch();
} else {
die('You are missing php-zip');
}
} }
// User specific settings need to go here as session isn't available at this point in the app // 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() private function updateApps()
{ {
Log::debug('Update of apps triggered'); UpdateApps::dispatchAfterResponse();
$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);
}
} }
} }

View File

@@ -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'; $zipurl = config('app.appsource').'files/'.$app->sha.'.zip';
$client = new Client(['http_errors' => false, 'timeout' => 60, 'connect_timeout' => 15, 'verify' => false]); $client = new Client(['http_errors' => false, 'timeout' => 60, 'connect_timeout' => 15, 'verify' => false]);
$res = $client->request('GET', $zipurl); $res = $client->request('GET', $zipurl);
// Something went wrong?
if ($res->getStatusCode() !== 200) {
return false;
}
if (! file_exists(app_path('SupportedApps'))) { if (! file_exists(app_path('SupportedApps'))) {
mkdir(app_path('SupportedApps'), 0777, true); mkdir(app_path('SupportedApps'), 0777, true);
} }
@@ -150,7 +160,9 @@ abstract class SupportedApps
unlink($src); //Deleting the Zipped file unlink($src); //Deleting the Zipped file
} else { } else {
var_dump($x); var_dump($x);
return false;
} }
return true;
} }
public static function saveApp($details, $app) public static function saveApp($details, $app)