Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/Signal.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Workflow\Middleware\WithoutOverlappingMiddleware;
use Workflow\Models\StoredWorkflow;

final class Signal implements ShouldBeEncrypted, ShouldQueue
class Signal implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
Expand Down
25 changes: 25 additions & 0 deletions src/Timer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Workflow;

use Illuminate\Contracts\Queue\ShouldBeUnique;
use Workflow\Models\StoredWorkflow;

final class Timer extends Signal implements ShouldBeUnique
{
public function __construct(
public StoredWorkflow $storedWorkflow,
public int $index,
$connection = null,
$queue = null
) {
parent::__construct($storedWorkflow, $connection, $queue);
}

public function uniqueId()
{
return $this->storedWorkflow->id . ':' . $this->index;
}
}
11 changes: 8 additions & 3 deletions src/Traits/Timers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use React\Promise\PromiseInterface;
use function React\Promise\resolve;
use Workflow\Serializers\Serializer;
use Workflow\Signal;
use Workflow\Timer;

trait Timers
{
Expand Down Expand Up @@ -66,7 +66,7 @@ public static function timer(int|string|CarbonInterval $seconds): PromiseInterfa
->create([
'index' => self::$context->index,
'now' => self::$context->now,
'class' => Signal::class,
'class' => Timer::class,
'result' => Serializer::serialize(true),
]);
} catch (\Illuminate\Database\UniqueConstraintViolationException $exception) {
Expand All @@ -90,7 +90,12 @@ public static function timer(int|string|CarbonInterval $seconds): PromiseInterfa
}
}

Signal::dispatch(self::$context->storedWorkflow, self::connection(), self::queue())->delay($delay);
Timer::dispatch(
self::$context->storedWorkflow,
self::$context->index,
self::connection(),
self::queue()
)->delay($delay);
}

++self::$context->index;
Expand Down
12 changes: 6 additions & 6 deletions tests/Unit/Traits/TimersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use Tests\TestCase;
use Workflow\Models\StoredWorkflow;
use Workflow\Serializers\Serializer;
use Workflow\Signal;
use Workflow\States\WorkflowPendingStatus;
use Workflow\Timer;
use Workflow\WorkflowStub;

final class TimersTest extends TestCase
Expand Down Expand Up @@ -109,7 +109,7 @@ public function testStoresResult(): void
$this->assertDatabaseHas('workflow_logs', [
'stored_workflow_id' => $workflow->id(),
'index' => 0,
'class' => Signal::class,
'class' => Timer::class,
]);
$this->assertSame(true, Serializer::unserialize($workflow->logs()->firstWhere('index', 0)->result));
}
Expand All @@ -127,7 +127,7 @@ public function testLoadsStoredResult(): void
->create([
'index' => 0,
'now' => now(),
'class' => Signal::class,
'class' => Timer::class,
'result' => Serializer::serialize(true),
]);

Expand All @@ -141,7 +141,7 @@ public function testLoadsStoredResult(): void
$this->assertDatabaseHas('workflow_logs', [
'stored_workflow_id' => $workflow->id(),
'index' => 0,
'class' => Signal::class,
'class' => Timer::class,
]);
$this->assertSame(true, Serializer::unserialize($workflow->logs()->firstWhere('index', 0)->result));
}
Expand All @@ -159,7 +159,7 @@ public function testHandlesDuplicateLogInsertionProperly(): void
->create([
'index' => 0,
'now' => now(),
'class' => Signal::class,
'class' => Timer::class,
'result' => Serializer::serialize(true),
]);

Expand Down Expand Up @@ -296,7 +296,7 @@ public function testTimerCapsDelayForSqsDriver(): void

$this->assertNull($result);

Bus::assertDispatched(Signal::class, function ($job) use ($now) {
Bus::assertDispatched(Timer::class, function ($job) use ($now) {
$delaySeconds = $job->delay->diffInSeconds($now);
$this->assertLessThanOrEqual(900, $delaySeconds);
$this->assertGreaterThanOrEqual(899, $delaySeconds);
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/WorkflowStubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Workflow\States\WorkflowCreatedStatus;
use Workflow\States\WorkflowPendingStatus;
use Workflow\States\WorkflowWaitingStatus;
use Workflow\Timer;
use Workflow\WorkflowStub;

final class WorkflowStubTest extends TestCase
Expand Down Expand Up @@ -204,7 +205,7 @@ public function testAwaitWithTimeoutTimedout(): void
$this->assertDatabaseHas('workflow_logs', [
'stored_workflow_id' => $workflow->id(),
'index' => 1,
'class' => Signal::class,
'class' => Timer::class,
]);
$this->assertTrue(Serializer::unserialize($workflow->logs()->firstWhere('index', 1)->result));
}
Expand Down