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
commit 756ab353f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 78 additions and 34 deletions

View file

@ -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);
}
}

View file

@ -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;

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;
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();
}
}

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';
$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)