Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions src/Deprecator/DeprecatorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
namespace CraftCms\Cms\Deprecator;

use CraftCms\Cms\Deprecator\Commands\ClearDeprecationsCommand;
use Illuminate\Queue\Events\JobExceptionOccurred;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Jobs\SyncJob;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;

/**
Expand All @@ -17,6 +21,13 @@ public function boot(): void
// Resolved when the request ends, as the deprecator is request-scoped, and the app can outlive a request (such as under Laravel Octane).
$this->app->terminating(fn () => $this->app->make(Deprecator::class)->storeLogs());

// A queue worker only terminates when it stops, and it forgets scoped instances before each job.
Event::listen([JobProcessed::class, JobExceptionOccurred::class], function (JobProcessed|JobExceptionOccurred $event): void {
if (! $event->job instanceof SyncJob) {
$this->app->make(Deprecator::class)->storeLogs();
}
});

$this->commands([
ClearDeprecationsCommand::class,
]);
Expand Down
10 changes: 10 additions & 0 deletions src/Element/ElementServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
use CraftCms\Cms\Element\Commands\SyncEloquentElementsCommand;
use CraftCms\Cms\Element\Eloquent\EloquentElements;
use CraftCms\Cms\Element\Listeners\BroadcastElementChanges;
use Illuminate\Queue\Events\JobExceptionOccurred;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Jobs\SyncJob;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;

Expand All @@ -44,6 +47,13 @@ public function boot(): void

app()->terminating(fn () => app(BulkOpDeferrals::class)->persistPending());

// A queue worker only terminates when it stops, and a bulk op can end in a job on another worker.
Event::listen([JobProcessed::class, JobExceptionOccurred::class], function (JobProcessed|JobExceptionOccurred $event): void {
if (! $event->job instanceof SyncJob) {
app(BulkOpDeferrals::class)->persistPending();
}
});

if (! $this->app->runningInConsole()) {
return;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Element/Operations/ElementCanonicalChanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ public function updateCanonicalElement(ElementInterface $element, array $newAttr

$updatedCanonical = $this->elementDuplicates->duplicateElement($element, $newAttributes);

app()->terminating(function () use (
// Written after the transaction commits, so the upserts don't hold locks during the save, and are skipped if it
// rolls back.
DB::afterCommit(function () use (
$canonical,
$updatedCanonical,
$changedAttributes,
Expand Down
6 changes: 5 additions & 1 deletion src/Mcp/StdioEventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace CraftCms\Cms\Mcp;

use CraftCms\Cms\Activity\ActivityEventRecorder;
use CraftCms\Cms\Deprecator\Deprecator;
use CraftCms\Cms\Element\BulkOp\BulkOpDeferrals;
use CraftCms\Cms\Http\Listeners\PrepareCraftForNextOperation;
use CraftCms\Cms\ProjectConfig\ProjectConfig;
use Illuminate\Support\Facades\Context;
Expand All @@ -15,7 +17,7 @@

/**
* Handles each stdio request in a fresh request scope, with Craft's state reset as for an Octane request, and saves
* project config changes before each response is sent.
* project config changes, pending bulk op events and deprecation warnings before each response is sent.
*
* Over HTTP, each request gets its own scope, and changes are saved as it ends. The stdio server is one long-running
* command, so without this, requests would share state, and changes would only be saved when it stops, with nothing
Expand All @@ -36,6 +38,8 @@ public function dispatch(object $event): object

if ($event instanceof ResponseEvent || $event instanceof ErrorEvent) {
app(ProjectConfig::class)->flushChanges();
app(BulkOpDeferrals::class)->persistPending();
app(Deprecator::class)->storeLogs();
}

return $event;
Expand Down
15 changes: 15 additions & 0 deletions tests/Feature/Deprecator/DeprecatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use CraftCms\Cms\Support\Facades\Deprecator as DeprecatorFacade;
use CraftCms\Cms\View\TemplateManager;
use CraftCms\Cms\View\TemplateMode;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Jobs\SyncJob;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;

Expand All @@ -27,6 +30,18 @@
expect(DeprecatorFacade::getTotalLogs())->toBe(0);
});

it('stores logs when a queued job finishes, but not a sync one', function () {
$this->deprecator->log('foo', 'A deprecation message');

event(new JobProcessed('sync', new SyncJob(app(), '{}', 'sync', 'default')));

expect(DeprecationError::count())->toBe(0);

event(new JobProcessed('database', Mockery::mock(Job::class, ['getQueue' => 'default', 'payload' => []])));

expect(DeprecationError::count())->toBe(1);
});

it('can log deprecations to the log', function () {
Deprecator::$logTarget = 'logs';

Expand Down
18 changes: 18 additions & 0 deletions tests/Feature/Element/BulkOp/BulkOpDeferralsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use CraftCms\Cms\Element\BulkOp\BulkOps;
use CraftCms\Cms\Element\BulkOp\Events\DeferredBulkOpReplayed;
use CraftCms\Cms\Entry\Elements\Entry;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Jobs\SyncJob;
use Illuminate\Support\Facades\DB;

class TestDeferredBulkEvent
Expand Down Expand Up @@ -145,6 +148,21 @@ public function __construct(
->count())->toBe(1);
});

it('persists pending triggers when a queued job finishes, but not a sync one', function () {
$this->deferrals->defer(TestDeferredBulkEvent::class, function () {});

$key = $this->bulkOps->start();

event(new TestDeferredBulkEvent('inside'));
event(new JobProcessed('sync', new SyncJob(app(), '{}', 'sync', 'default')));

expect($this->bulkOpConnection->table(Table::BULKOPEVENTS)->where('key', $key)->count())->toBe(0);

event(new JobProcessed('database', Mockery::mock(Job::class, ['getQueue' => 'default', 'payload' => []])));

expect($this->bulkOpConnection->table(Table::BULKOPEVENTS)->where('key', $key)->count())->toBe(1);
});

it('replays persisted rows and deletes them', function () {
$replays = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
expect($state->duplicateCall)->toBeNull();
});

it('prepares duplicate attributes and defers canonical change tracking updates for drafts', function () {
it('prepares duplicate attributes and writes canonical change tracking for drafts once the transaction commits', function () {
$result = EntryModel::factory()
->withField('testField', PlainText::class)
->createElementWithFields(save: false);
Expand Down Expand Up @@ -166,6 +166,14 @@

[$action, $state] = createActionSpy($updatedCanonical);

DB::beginTransaction();
$action->updateCanonicalElement($draft, ['custom' => 'value']);
DB::rollBack();

expect(DB::table(Table::CHANGEDATTRIBUTES)->where('elementId', $canonical->id)->count())->toBe(0)
->and(DB::table(Table::CHANGEDFIELDS)->where('elementId', $canonical->id)->count())->toBe(0);

DB::beginTransaction();
expect($action->updateCanonicalElement($draft, ['custom' => 'value']))->toBe($updatedCanonical);

expect($state->duplicateCall)->not->toBeNull();
Expand Down Expand Up @@ -198,7 +206,7 @@
expect(DB::table(Table::CHANGEDATTRIBUTES)->where('elementId', $canonical->id)->count())->toBe(0)
->and(DB::table(Table::CHANGEDFIELDS)->where('elementId', $canonical->id)->count())->toBe(0);

app()->terminate();
DB::commit();

expect(DB::table(Table::CHANGEDATTRIBUTES)
->where('elementId', $canonical->id)
Expand Down
Loading