Skip to content

Commit 9b76313

Browse files
authored
feat(core)!: worker mode support (#2172)
1 parent 0e26fdc commit 9b76313

46 files changed

Lines changed: 1311 additions & 39 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/auth/src/Authentication/SessionAuthenticator.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
namespace Tempest\Auth\Authentication;
66

7-
use Tempest\Container\Resettable;
87
use Tempest\Http\Session\Session;
98
use Tempest\Http\Session\SessionManager;
109

11-
final class SessionAuthenticator implements Authenticator, Resettable
10+
final class SessionAuthenticator implements Authenticator
1211
{
1312
public const string AUTHENTICATABLE_KEY = '#authenticatable:id';
1413

@@ -77,12 +76,7 @@ public function current(): ?Authenticatable
7776
return $this->current;
7877
}
7978

80-
public function reset(): void
81-
{
82-
$this->clearCurrent();
83-
}
84-
85-
private function clearCurrent(): void
79+
public function clearCurrent(): void
8680
{
8781
$this->currentId = null;
8882
$this->currentClass = null;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Tempest\Auth\Authentication;
4+
5+
use Tempest\Container\Resettable;
6+
7+
final readonly class SessionAuthenticatorReset implements Resettable
8+
{
9+
public function __construct(
10+
private SessionAuthenticator $sessionAuthenticator,
11+
) {}
12+
13+
public function reset(): void
14+
{
15+
$this->sessionAuthenticator->clearCurrent();
16+
}
17+
}

packages/auth/tests/SessionAuthenticatorTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Tempest\Auth\Authentication\Authenticatable;
1010
use Tempest\Auth\Authentication\AuthenticatableResolver;
1111
use Tempest\Auth\Authentication\SessionAuthenticator;
12-
use Tempest\Container\Resettable;
12+
use Tempest\Auth\Authentication\SessionAuthenticatorReset;
1313
use Tempest\DateTime\DateTime;
1414
use Tempest\Http\Session\Session;
1515
use Tempest\Http\Session\SessionId;
@@ -100,10 +100,9 @@ public function reset_clears_the_cached_current_authenticatable(): void
100100
authenticatableResolver: $resolver,
101101
);
102102

103-
$this->assertInstanceOf(Resettable::class, $authenticator);
104103
$this->assertSame($authenticatable, $authenticator->current());
105104

106-
$authenticator->reset();
105+
new SessionAuthenticatorReset($authenticator)->reset();
107106

108107
$this->assertSame($authenticatable, $authenticator->current());
109108
$this->assertSame(2, $resolver->resolveCalls);

packages/console/src/ConsoleApplication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static function boot(
4848
return $container->get(ConsoleApplication::class);
4949
}
5050

51-
public function run(): never
51+
public function run(): void
5252
{
5353
$exitCode = $this->container->get(ExecuteConsoleCommand::class)($this->argumentBag->getCommandName());
5454

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Tempest\Core;
4+
5+
use Tempest\Container\Container;
6+
use Tempest\Container\Resettable;
7+
8+
final readonly class DeferredTasksReset implements Resettable
9+
{
10+
public function __construct(
11+
private Container $container,
12+
) {}
13+
14+
public function reset(): void
15+
{
16+
$this->container->unregister(DeferredTasks::class);
17+
}
18+
}

packages/core/src/FrameworkKernel.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ final class FrameworkKernel implements Kernel
3535
public function __construct(
3636
public string $root,
3737
/** @var DiscoveryLocation[] */
38-
private array $discoveryLocations = [],
38+
private readonly array $discoveryLocations = [],
3939
?Container $container = null,
4040
?string $internalStorage = null,
41+
private readonly bool $longRunning = false,
4142
) {
4243
$this->container = $container ?? $this->createContainer();
4344

@@ -51,6 +52,7 @@ public static function boot(
5152
array $discoveryLocations = [],
5253
?Container $container = null,
5354
?string $internalStorage = null,
55+
bool $longRunning = false,
5456
): self {
5557
if (! defined('TEMPEST_START')) {
5658
define('TEMPEST_START', value: hrtime(as_number: true));
@@ -61,6 +63,7 @@ public static function boot(
6163
discoveryLocations: $discoveryLocations,
6264
container: $container,
6365
internalStorage: $internalStorage,
66+
longRunning: $longRunning,
6467
)
6568
->registerKernel()
6669
->validateRoot()
@@ -98,12 +101,23 @@ public function validateRoot(): self
98101
return $this;
99102
}
100103

101-
public function shutdown(int|string $status = ''): never
104+
public function shutdown(int|string $status = ''): void
102105
{
103-
$this->finishDeferredTasks()
104-
->event(KernelEvent::SHUTDOWN);
106+
$this->event(KernelEvent::SHUTTING_DOWN)
107+
->finishDeferredTasks();
108+
109+
if ($this->longRunning) {
110+
$this
111+
->event(KernelEvent::RESETTING)
112+
->resetContainer()
113+
->event(KernelEvent::RESET);
114+
}
115+
116+
$this->event(KernelEvent::SHUTDOWN);
105117

106-
exit($status);
118+
if (! $this->longRunning) {
119+
exit($status);
120+
}
107121
}
108122

109123
public function loadComposer(): self
@@ -236,6 +250,13 @@ public function finishDeferredTasks(): self
236250
return $this;
237251
}
238252

253+
public function resetContainer(): self
254+
{
255+
$this->container->reset();
256+
257+
return $this;
258+
}
259+
239260
public function event(object $event): self
240261
{
241262
if (interface_exists(EventBus::class)) {

packages/core/src/Kernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ public static function boot(
2323
?string $internalStorage = null,
2424
): self;
2525

26-
public function shutdown(int|string $status = ''): never;
26+
public function shutdown(int|string $status = ''): void;
2727
}

packages/core/src/KernelEvent.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
enum KernelEvent
88
{
9+
case SHUTTING_DOWN;
10+
case RESETTING;
11+
case RESET;
912
case BOOTED;
1013
case SHUTDOWN;
1114
}

packages/core/src/Tempest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@
99
final readonly class Tempest
1010
{
1111
/** @param \Tempest\Discovery\DiscoveryLocation[] $discoveryLocations */
12-
public static function boot(?string $root = null, array $discoveryLocations = [], ?string $internalStorage = null): Container
13-
{
12+
public static function boot(
13+
?string $root = null,
14+
array $discoveryLocations = [],
15+
?string $internalStorage = null,
16+
bool $longRunning = false,
17+
): Container {
1418
$kernel = FrameworkKernel::boot(
1519
root: $root ?? getcwd(),
1620
discoveryLocations: $discoveryLocations,
1721
internalStorage: $internalStorage,
22+
longRunning: $longRunning,
1823
);
1924

2025
return $kernel->container;

packages/database/src/Connection/Connection.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ interface Connection
1010
{
1111
public function beginTransaction(): bool;
1212

13+
public function inTransaction(): bool;
14+
1315
public function commit(): bool;
1416

1517
public function rollback(): bool;
@@ -21,4 +23,8 @@ public function prepare(string $sql): PDOStatement;
2123
public function close(): void;
2224

2325
public function connect(): void;
26+
27+
public function reconnect(): void;
28+
29+
public function ping(): bool;
2430
}

0 commit comments

Comments
 (0)