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

Export logs #757

Merged
merged 5 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
22 changes: 19 additions & 3 deletions app/Jobs/Campaigns/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Jobs\FileCleanup;
use App\Models\Campaign;
use App\Models\CampaignExport;
use App\Services\Campaign\ExportService;
use App\User;
use Exception;
Expand Down Expand Up @@ -31,16 +32,19 @@ class Export implements ShouldQueue

protected int $userId;

protected int $campaignExportId;

protected bool $assets;

/**
* CampaignExport constructor.
*/
public function __construct(Campaign $campaign, User $user, bool $assets = false)
public function __construct(Campaign $campaign, User $user, CampaignExport $campaignExport, bool $assets = false)
{
$this->campaignId = $campaign->id;
$this->userId = $user->id;
$this->assets = $assets;
$this->campaignExportId = $campaignExport->id;
}

/**
Expand All @@ -49,6 +53,12 @@ public function __construct(Campaign $campaign, User $user, bool $assets = false
*/
public function handle()
{
$campaignExport = CampaignExport::find($this->campaignExportId);
if (!$campaignExport) {
return 0;
}
$campaignExport->update(['status' => CampaignExport::STATUS_RUNNING]);

/** @var Campaign|null $campaign */
$campaign = Campaign::find($this->campaignId);
if (!$campaign) {
Expand All @@ -63,7 +73,7 @@ public function handle()

/** @var ExportService $service */
$service = app()->make(ExportService::class);
$service
$filesize = $service
->user($user)
->campaign($campaign)
->assets($this->assets)
Expand All @@ -74,7 +84,7 @@ public function handle()
if ($queue !== 'sync') {
FileCleanup::dispatch($service->exportPath())->delay(now()->addMinutes(60));
}

$campaignExport->update(['status' => CampaignExport::STATUS_FINISHED, 'size' => $filesize]);
return 1;
}

Expand All @@ -83,6 +93,12 @@ public function handle()
*/
public function failed(Exception $exception)
{
$campaignExport = CampaignExport::find($this->campaignExportId);
if (!$campaignExport) {
return;
}
$campaignExport->update(['status' => CampaignExport::STATUS_FAILED]);

// Set the campaign export date to null so that the user can try again.
// If it failed once, trying again won't help, but this might motivate
// them to report the error.
Expand Down
47 changes: 47 additions & 0 deletions app/Models/CampaignExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\MassPrunable;
use Illuminate\Database\Eloquent\Model;

/**
* Class CampaignExport
* @package App\Models
*
* @property int $id
* @property int $campaign_id
* @property int $created_by
* @property int $status
*
*/
class CampaignExport extends Model
{
use MassPrunable;

public const TYPE_ENTITIES = 1;
public const TYPE_ASSETS = 2;

public const STATUS_SCHEDULED = 1;
public const STATUS_RUNNING = 2;
public const STATUS_FINISHED = 3;
public const STATUS_FAILED = 4;

public $fillable = [
'size',
'type',
'status',
'campaign_id',
'created_by'
];

/**
* Automatically prune old elements from the db
*/
public function prunable(): Builder
{
return static::where('updated_at', '<=', now()->subDays(90));
}

}
27 changes: 23 additions & 4 deletions app/Services/Campaign/ExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Facades\CampaignCache;
use App\Jobs\Campaigns\Export;
use App\Models\Image;
use App\Models\CampaignExport;
use App\Notifications\Header;
use App\Traits\CampaignAware;
use App\Traits\UserAware;
Expand All @@ -29,6 +30,7 @@ class ExportService
protected bool $assets = false;

protected int $files = 0;
protected int $filesize = 0;

public function exportPath(): string
{
Expand All @@ -46,13 +48,28 @@ public function queue(): self
$this->campaign->export_date = date('Y-m-d');
$this->campaign->saveQuietly();

Export::dispatch($this->campaign, $this->user, false);
Export::dispatch($this->campaign, $this->user, true);
$entitiesExport = CampaignExport::create([
'campaign_id' => $this->campaign->id,
'created_by' => $this->user->id,
'type' => CampaignExport::TYPE_ENTITIES,
'status' => CampaignExport::STATUS_SCHEDULED,
]);

Export::dispatch($this->campaign, $this->user, $entitiesExport, false);

$assetExport = CampaignExport::create([
'campaign_id' => $this->campaign->id,
'created_by' => $this->user->id,
'type' => CampaignExport::TYPE_ASSETS,
'status' => CampaignExport::STATUS_SCHEDULED,
]);

Export::dispatch($this->campaign, $this->user, $assetExport, true);

return $this;
}

public function export(): self
public function export(): int
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved
{
$this
->prepare()
Expand All @@ -63,7 +80,7 @@ public function export(): self
->notify()
;

return $this;
return $this->filesize;
}

protected function prepare(): self
Expand Down Expand Up @@ -212,6 +229,8 @@ protected function finish(): self
$saveFolder = storage_path() . '/exports/campaigns/';

$this->archive->saveTo($saveFolder);
$this->filesize = (int) floor(filesize($this->path) / pow(1024, 2));

} catch (Exception $e) {
// The export might fail if the zip is too big.
$this->files = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('campaign_exports', function (Blueprint $table) {

$table->increments('id');
$table->unsignedInteger('campaign_id');
$table->unsignedInteger('created_by')->nullable();
$table->integer('size');
$table->tinyInteger('type');
$table->tinyInteger('status');
$table->timestamps();
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved

$table->index(['status']);

$table->foreign('campaign_id')->references('id')->on('campaigns')->onDelete('cascade');
$table->foreign('created_by')->references('id')->on('users')->onDelete('set null');

});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('campaign_exports');
}
};