-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRuntimes.php
More file actions
129 lines (113 loc) · 3.41 KB
/
Runtimes.php
File metadata and controls
129 lines (113 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace OpenRuntimes\Executor\Runner\Repository;
use Iterator;
use OpenRuntimes\Executor\Runner\Runtime;
use Swoole\Table;
/**
* @implements Iterator<string, Runtime>
*/
final class Runtimes implements Iterator
{
private Table $runtimes;
/**
* Create a runtime repository.
* This class should be initialized in the main application process, before workers are forked.
*
* @param int $size The size of the table. Swoole tables must be preallocated.
*/
public function __construct(int $size = 4096)
{
$this->runtimes = new Table($size);
$this->runtimes->column('version', Table::TYPE_STRING, 32);
$this->runtimes->column('created', Table::TYPE_FLOAT);
$this->runtimes->column('updated', Table::TYPE_FLOAT);
$this->runtimes->column('name', Table::TYPE_STRING, 1024);
$this->runtimes->column('hostname', Table::TYPE_STRING, 1024);
$this->runtimes->column('status', Table::TYPE_STRING, 256);
$this->runtimes->column('key', Table::TYPE_STRING, 1024);
$this->runtimes->column('listening', Table::TYPE_INT, 1);
$this->runtimes->column('image', Table::TYPE_STRING, 1024);
$this->runtimes->column('initialised', Table::TYPE_INT, 0);
$this->runtimes->create();
}
/**
* Get a runtime by ID.
*
* @param string $id The ID of the runtime to retrieve.
* @return Runtime|null The runtime object or null if not found.
*/
public function get(string $id): ?Runtime
{
$runtime = $this->runtimes->get($id);
if ($runtime === false) {
return null;
}
return Runtime::fromArray($runtime);
}
/**
* Check if a runtime exists by ID.
*
* @param string $id The ID of the runtime to check.
* @return bool True if the runtime exists, false otherwise.
*/
public function exists(string $id): bool
{
return $this->runtimes->exists($id);
}
/**
* Set a runtime by ID. Existing runtime will be overwritten.
*
* @param string $id The ID of the runtime to update.
* @param Runtime $runtime The updated runtime object.
* @return void
*/
public function set(string $id, Runtime $runtime): void
{
$this->runtimes->set($id, $runtime->toArray());
}
/**
* Remove a runtime by ID.
*
* @param string $id The ID of the runtime to remove.
* @return bool True if the runtime was removed, false otherwise.
*/
public function remove(string $id): bool
{
return $this->runtimes->del($id);
}
/**
* List all runtimes.
*
* @return array<Runtime> An array of Runtime objects.
*/
public function list(): array
{
$runtimes = [];
foreach ($this->runtimes as $runtimeKey => $runtime) {
$runtimes[$runtimeKey] = Runtime::fromArray($runtime);
}
return $runtimes;
}
// Iterator traits
public function current(): Runtime
{
$runtime = $this->runtimes->current();
return Runtime::fromArray($runtime);
}
public function next(): void
{
$this->runtimes->next();
}
public function key(): string
{
return $this->runtimes->key();
}
public function valid(): bool
{
return $this->runtimes->valid();
}
public function rewind(): void
{
$this->runtimes->rewind();
}
}