@alchemy.run/effect-cluster 0.0.0 → 2.0.0-beta.86

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (233) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +101 -4
  3. package/lib/AWS/Database.d.ts +54 -0
  4. package/lib/AWS/Database.d.ts.map +1 -0
  5. package/lib/AWS/Database.js +96 -0
  6. package/lib/AWS/Database.js.map +1 -0
  7. package/lib/AWS/EcsPool.d.ts +56 -0
  8. package/lib/AWS/EcsPool.d.ts.map +1 -0
  9. package/lib/AWS/EcsPool.js +87 -0
  10. package/lib/AWS/EcsPool.js.map +1 -0
  11. package/lib/AWS/index.d.ts +7 -0
  12. package/lib/AWS/index.d.ts.map +1 -0
  13. package/lib/AWS/index.js +7 -0
  14. package/lib/AWS/index.js.map +1 -0
  15. package/lib/AWS/internal/Gateway.d.ts +23 -0
  16. package/lib/AWS/internal/Gateway.d.ts.map +1 -0
  17. package/lib/AWS/internal/Gateway.js +70 -0
  18. package/lib/AWS/internal/Gateway.js.map +1 -0
  19. package/lib/Cloudflare/DurableObjectPool.d.ts +42 -0
  20. package/lib/Cloudflare/DurableObjectPool.d.ts.map +1 -0
  21. package/lib/Cloudflare/DurableObjectPool.js +136 -0
  22. package/lib/Cloudflare/DurableObjectPool.js.map +1 -0
  23. package/lib/Cloudflare/PoolWorker.d.ts +186 -0
  24. package/lib/Cloudflare/PoolWorker.d.ts.map +1 -0
  25. package/lib/Cloudflare/PoolWorker.js +75 -0
  26. package/lib/Cloudflare/PoolWorker.js.map +1 -0
  27. package/lib/Cloudflare/index.d.ts +7 -0
  28. package/lib/Cloudflare/index.d.ts.map +1 -0
  29. package/lib/Cloudflare/index.js +7 -0
  30. package/lib/Cloudflare/index.js.map +1 -0
  31. package/lib/Cloudflare/internal/Objects.d.ts +38 -0
  32. package/lib/Cloudflare/internal/Objects.d.ts.map +1 -0
  33. package/lib/Cloudflare/internal/Objects.js +102 -0
  34. package/lib/Cloudflare/internal/Objects.js.map +1 -0
  35. package/lib/Cron.d.ts +143 -0
  36. package/lib/Cron.d.ts.map +1 -0
  37. package/lib/Cron.js +83 -0
  38. package/lib/Cron.js.map +1 -0
  39. package/lib/Database.d.ts +95 -0
  40. package/lib/Database.d.ts.map +1 -0
  41. package/lib/Database.js +38 -0
  42. package/lib/Database.js.map +1 -0
  43. package/lib/Docker/Database.d.ts +42 -0
  44. package/lib/Docker/Database.d.ts.map +1 -0
  45. package/lib/Docker/Database.js +53 -0
  46. package/lib/Docker/Database.js.map +1 -0
  47. package/lib/Docker/SwarmPool.d.ts +44 -0
  48. package/lib/Docker/SwarmPool.d.ts.map +1 -0
  49. package/lib/Docker/SwarmPool.js +56 -0
  50. package/lib/Docker/SwarmPool.js.map +1 -0
  51. package/lib/Docker/index.d.ts +7 -0
  52. package/lib/Docker/index.d.ts.map +1 -0
  53. package/lib/Docker/index.js +7 -0
  54. package/lib/Docker/index.js.map +1 -0
  55. package/lib/Entity.d.ts +348 -0
  56. package/lib/Entity.d.ts.map +1 -0
  57. package/lib/Entity.js +308 -0
  58. package/lib/Entity.js.map +1 -0
  59. package/lib/Fly/Database.d.ts +50 -0
  60. package/lib/Fly/Database.d.ts.map +1 -0
  61. package/lib/Fly/Database.js +56 -0
  62. package/lib/Fly/Database.js.map +1 -0
  63. package/lib/Fly/MachinePool.d.ts +39 -0
  64. package/lib/Fly/MachinePool.d.ts.map +1 -0
  65. package/lib/Fly/MachinePool.js +51 -0
  66. package/lib/Fly/MachinePool.js.map +1 -0
  67. package/lib/Fly/index.d.ts +7 -0
  68. package/lib/Fly/index.d.ts.map +1 -0
  69. package/lib/Fly/index.js +7 -0
  70. package/lib/Fly/index.js.map +1 -0
  71. package/lib/GCP/ComputeEnginePool.d.ts +41 -0
  72. package/lib/GCP/ComputeEnginePool.d.ts.map +1 -0
  73. package/lib/GCP/ComputeEnginePool.js +172 -0
  74. package/lib/GCP/ComputeEnginePool.js.map +1 -0
  75. package/lib/GCP/Database.d.ts +48 -0
  76. package/lib/GCP/Database.d.ts.map +1 -0
  77. package/lib/GCP/Database.js +115 -0
  78. package/lib/GCP/Database.js.map +1 -0
  79. package/lib/GCP/index.d.ts +7 -0
  80. package/lib/GCP/index.d.ts.map +1 -0
  81. package/lib/GCP/index.js +7 -0
  82. package/lib/GCP/index.js.map +1 -0
  83. package/lib/Hetzner/Database.d.ts +48 -0
  84. package/lib/Hetzner/Database.d.ts.map +1 -0
  85. package/lib/Hetzner/Database.js +91 -0
  86. package/lib/Hetzner/Database.js.map +1 -0
  87. package/lib/Hetzner/ServerPool.d.ts +35 -0
  88. package/lib/Hetzner/ServerPool.d.ts.map +1 -0
  89. package/lib/Hetzner/ServerPool.js +98 -0
  90. package/lib/Hetzner/ServerPool.js.map +1 -0
  91. package/lib/Hetzner/index.d.ts +7 -0
  92. package/lib/Hetzner/index.d.ts.map +1 -0
  93. package/lib/Hetzner/index.js +7 -0
  94. package/lib/Hetzner/index.js.map +1 -0
  95. package/lib/Host.d.ts +60 -0
  96. package/lib/Host.d.ts.map +1 -0
  97. package/lib/Host.js +31 -0
  98. package/lib/Host.js.map +1 -0
  99. package/lib/Kubernetes/Database.d.ts +40 -0
  100. package/lib/Kubernetes/Database.d.ts.map +1 -0
  101. package/lib/Kubernetes/Database.js +56 -0
  102. package/lib/Kubernetes/Database.js.map +1 -0
  103. package/lib/Kubernetes/DeploymentPool.d.ts +54 -0
  104. package/lib/Kubernetes/DeploymentPool.d.ts.map +1 -0
  105. package/lib/Kubernetes/DeploymentPool.js +49 -0
  106. package/lib/Kubernetes/DeploymentPool.js.map +1 -0
  107. package/lib/Kubernetes/index.d.ts +7 -0
  108. package/lib/Kubernetes/index.d.ts.map +1 -0
  109. package/lib/Kubernetes/index.js +7 -0
  110. package/lib/Kubernetes/index.js.map +1 -0
  111. package/lib/Pool.d.ts +66 -0
  112. package/lib/Pool.d.ts.map +1 -0
  113. package/lib/Pool.js +47 -0
  114. package/lib/Pool.js.map +1 -0
  115. package/lib/Providers.d.ts +19 -0
  116. package/lib/Providers.d.ts.map +1 -0
  117. package/lib/Providers.js +31 -0
  118. package/lib/Providers.js.map +1 -0
  119. package/lib/ShardGroups.d.ts +24 -0
  120. package/lib/ShardGroups.d.ts.map +1 -0
  121. package/lib/ShardGroups.js +19 -0
  122. package/lib/ShardGroups.js.map +1 -0
  123. package/lib/Singleton.d.ts +110 -0
  124. package/lib/Singleton.d.ts.map +1 -0
  125. package/lib/Singleton.js +60 -0
  126. package/lib/Singleton.js.map +1 -0
  127. package/lib/Workflow.d.ts +251 -0
  128. package/lib/Workflow.d.ts.map +1 -0
  129. package/lib/Workflow.js +288 -0
  130. package/lib/Workflow.js.map +1 -0
  131. package/lib/index.d.ts +12 -0
  132. package/lib/index.d.ts.map +1 -0
  133. package/lib/index.js +12 -0
  134. package/lib/index.js.map +1 -0
  135. package/lib/internal/Bootstrap.d.ts +28 -0
  136. package/lib/internal/Bootstrap.d.ts.map +1 -0
  137. package/lib/internal/Bootstrap.js +257 -0
  138. package/lib/internal/Bootstrap.js.map +1 -0
  139. package/lib/internal/EntityRuntime.d.ts +40 -0
  140. package/lib/internal/EntityRuntime.d.ts.map +1 -0
  141. package/lib/internal/EntityRuntime.js +698 -0
  142. package/lib/internal/EntityRuntime.js.map +1 -0
  143. package/lib/internal/Gateway.d.ts +82 -0
  144. package/lib/internal/Gateway.d.ts.map +1 -0
  145. package/lib/internal/Gateway.js +218 -0
  146. package/lib/internal/Gateway.js.map +1 -0
  147. package/lib/internal/HostedPlatform.d.ts +36 -0
  148. package/lib/internal/HostedPlatform.d.ts.map +1 -0
  149. package/lib/internal/HostedPlatform.js +36 -0
  150. package/lib/internal/HostedPlatform.js.map +1 -0
  151. package/lib/internal/HostedRuntime.d.ts +58 -0
  152. package/lib/internal/HostedRuntime.d.ts.map +1 -0
  153. package/lib/internal/HostedRuntime.js +72 -0
  154. package/lib/internal/HostedRuntime.js.map +1 -0
  155. package/lib/internal/Naming.d.ts +12 -0
  156. package/lib/internal/Naming.d.ts.map +1 -0
  157. package/lib/internal/Naming.js +40 -0
  158. package/lib/internal/Naming.js.map +1 -0
  159. package/lib/internal/ObjectSharding.d.ts +141 -0
  160. package/lib/internal/ObjectSharding.d.ts.map +1 -0
  161. package/lib/internal/ObjectSharding.js +337 -0
  162. package/lib/internal/ObjectSharding.js.map +1 -0
  163. package/lib/internal/PoolHost.d.ts +80 -0
  164. package/lib/internal/PoolHost.d.ts.map +1 -0
  165. package/lib/internal/PoolHost.js +203 -0
  166. package/lib/internal/PoolHost.js.map +1 -0
  167. package/lib/internal/PoolHostService.d.ts +25 -0
  168. package/lib/internal/PoolHostService.d.ts.map +1 -0
  169. package/lib/internal/PoolHostService.js +10 -0
  170. package/lib/internal/PoolHostService.js.map +1 -0
  171. package/lib/internal/Wire.d.ts +60 -0
  172. package/lib/internal/Wire.d.ts.map +1 -0
  173. package/lib/internal/Wire.js +36 -0
  174. package/lib/internal/Wire.js.map +1 -0
  175. package/lib/internal/WorkflowGateway.d.ts +36 -0
  176. package/lib/internal/WorkflowGateway.d.ts.map +1 -0
  177. package/lib/internal/WorkflowGateway.js +124 -0
  178. package/lib/internal/WorkflowGateway.js.map +1 -0
  179. package/lib/internal/external-runner.d.ts +2 -0
  180. package/lib/internal/external-runner.d.ts.map +1 -0
  181. package/lib/internal/external-runner.js +7 -0
  182. package/lib/internal/external-runner.js.map +1 -0
  183. package/lib/internal/runner.d.ts +2 -0
  184. package/lib/internal/runner.d.ts.map +1 -0
  185. package/lib/internal/runner.js +5 -0
  186. package/lib/internal/runner.js.map +1 -0
  187. package/package.json +128 -3
  188. package/src/AWS/Database.ts +139 -0
  189. package/src/AWS/EcsPool.ts +118 -0
  190. package/src/AWS/index.ts +6 -0
  191. package/src/AWS/internal/Gateway.ts +82 -0
  192. package/src/Cloudflare/DurableObjectPool.ts +198 -0
  193. package/src/Cloudflare/PoolWorker.ts +124 -0
  194. package/src/Cloudflare/index.ts +6 -0
  195. package/src/Cloudflare/internal/Objects.ts +160 -0
  196. package/src/Cron.ts +202 -0
  197. package/src/Database.ts +116 -0
  198. package/src/Docker/Database.ts +94 -0
  199. package/src/Docker/SwarmPool.ts +83 -0
  200. package/src/Docker/index.ts +6 -0
  201. package/src/Entity.ts +621 -0
  202. package/src/Fly/Database.ts +102 -0
  203. package/src/Fly/MachinePool.ts +72 -0
  204. package/src/Fly/index.ts +6 -0
  205. package/src/GCP/ComputeEnginePool.ts +203 -0
  206. package/src/GCP/Database.ts +168 -0
  207. package/src/GCP/index.ts +6 -0
  208. package/src/Hetzner/Database.ts +142 -0
  209. package/src/Hetzner/ServerPool.ts +116 -0
  210. package/src/Hetzner/index.ts +6 -0
  211. package/src/Host.ts +68 -0
  212. package/src/Kubernetes/Database.ts +89 -0
  213. package/src/Kubernetes/DeploymentPool.ts +77 -0
  214. package/src/Kubernetes/index.ts +6 -0
  215. package/src/Pool.ts +104 -0
  216. package/src/Providers.ts +49 -0
  217. package/src/ShardGroups.ts +41 -0
  218. package/src/Singleton.ts +153 -0
  219. package/src/Workflow.ts +556 -0
  220. package/src/index.ts +11 -0
  221. package/src/internal/Bootstrap.ts +368 -0
  222. package/src/internal/EntityRuntime.ts +996 -0
  223. package/src/internal/Gateway.ts +366 -0
  224. package/src/internal/HostedPlatform.ts +83 -0
  225. package/src/internal/HostedRuntime.ts +118 -0
  226. package/src/internal/Naming.ts +44 -0
  227. package/src/internal/ObjectSharding.ts +578 -0
  228. package/src/internal/PoolHost.ts +330 -0
  229. package/src/internal/PoolHostService.ts +25 -0
  230. package/src/internal/Wire.ts +83 -0
  231. package/src/internal/WorkflowGateway.ts +188 -0
  232. package/src/internal/external-runner.ts +10 -0
  233. package/src/internal/runner.ts +5 -0
@@ -0,0 +1,41 @@
1
+ import * as Provider from "alchemy/Provider";
2
+ import { Resource } from "alchemy/Resource";
3
+ import * as Effect from "effect/Effect";
4
+ import type { Providers } from "./Providers.ts";
5
+
6
+ /** The shard groups one pool serves, bound onto its database's {@link ShardGroups}. */
7
+ export interface ShardGroupsBindingContract {
8
+ groups?: string[];
9
+ }
10
+
11
+ export interface ShardGroupsAttributes {
12
+ /** Every shard group in the cluster, sorted. */
13
+ groups: string[];
14
+ }
15
+
16
+ export interface ShardGroups extends Resource<
17
+ "Cluster.ShardGroups",
18
+ {},
19
+ ShardGroupsAttributes,
20
+ ShardGroupsBindingContract,
21
+ Providers
22
+ > {}
23
+
24
+ /**
25
+ * Every shard group in one cluster, collected from the pools that share
26
+ * its database. Every runner and gateway is configured with the whole list:
27
+ * Effect Cluster numbers each shard's lock by its group's place in it, so
28
+ * they must all agree. It owns nothing in the cloud.
29
+ *
30
+ * @internal
31
+ */
32
+ export const ShardGroups = Resource<ShardGroups>("Cluster.ShardGroups");
33
+
34
+ export const ShardGroupsProvider = () =>
35
+ Provider.succeed(ShardGroups, {
36
+ reconcile: ({ bindings }) =>
37
+ Effect.sync(() => ({
38
+ groups: [...new Set(bindings.flatMap(({ data }) => data.groups ?? []))].sort(),
39
+ })),
40
+ delete: () => Effect.void,
41
+ });
@@ -0,0 +1,153 @@
1
+ import type { InputProps } from "alchemy/Input";
2
+ import type { Named } from "alchemy/Named";
3
+ import type { PlatformIdentity, PlatformServices } from "alchemy/Platform";
4
+ import type { RuntimeContext } from "alchemy/RuntimeContext";
5
+ import type { Self } from "alchemy/Self";
6
+ import type * as ConfigError from "effect/Config";
7
+ import type * as Context from "effect/Context";
8
+ import type * as Effect from "effect/Effect";
9
+ import type * as Layer from "effect/Layer";
10
+ import type { Scope } from "effect/Scope";
11
+ import {
12
+ type HostedProps,
13
+ type HostedResource,
14
+ makeHostedPlatform,
15
+ } from "./internal/HostedPlatform.ts";
16
+ import type { Providers } from "./Providers.ts";
17
+
18
+ export const SingletonTypeId = "Cluster.Singleton";
19
+ export type SingletonTypeId = typeof SingletonTypeId;
20
+
21
+ export type SingletonProps = HostedProps;
22
+
23
+ export interface ClusterSingleton extends HostedResource<SingletonTypeId> {}
24
+
25
+ /**
26
+ * A singleton as a runner runs it: the fiber its Construction phase
27
+ * returned, and the services that phase ran with.
28
+ * @internal
29
+ */
30
+ export interface HostedSingleton {
31
+ readonly kind: "singleton";
32
+ readonly name: string;
33
+ readonly run: Effect.Effect<void, unknown, Scope>;
34
+ readonly context: Context.Context<never>;
35
+ }
36
+
37
+ type SingletonServices =
38
+ | ClusterSingleton
39
+ | Providers
40
+ | RuntimeContext
41
+ | Self
42
+ | Self<ClusterSingleton>;
43
+
44
+ const { platform, provider, declare } = makeHostedPlatform(
45
+ SingletonTypeId,
46
+ (id, run, context): HostedSingleton => ({
47
+ kind: "singleton",
48
+ name: id,
49
+ run: run as HostedSingleton["run"],
50
+ context,
51
+ }),
52
+ );
53
+
54
+ /** The resource type behind every singleton, which the providers register. @internal */
55
+ export const SingletonResource = platform;
56
+ export const SingletonProvider = provider;
57
+
58
+ /** A declared singleton, implemented by `.make(props, impl)`. */
59
+ export type SingletonTag<Self, Name extends string> = Named<Name> &
60
+ PlatformIdentity<Name> & {
61
+ /**
62
+ * The singleton's implementation, as a Layer: an Effect that resolves
63
+ * bindings once per runner and returns the fiber to run. Provide a
64
+ * pool Layer to `impl` to deploy it; provide the returned Layer to
65
+ * the Stack.
66
+ */
67
+ make<InitReq = never>(
68
+ props: InputProps<SingletonProps>,
69
+ impl: Effect.Effect<Effect.Effect<void, unknown, Scope>, ConfigError.ConfigError, InitReq>,
70
+ ): Layer.Layer<Self, never, Providers | Exclude<InitReq, SingletonServices | PlatformServices>>;
71
+ new (_: never): {};
72
+ };
73
+
74
+ /** A singleton declared with its implementation: the class is what deploys it. */
75
+ export type SingletonInline<Name extends string, Req> = Effect.Effect<
76
+ ClusterSingleton,
77
+ never,
78
+ Req
79
+ > &
80
+ Named<Name> &
81
+ PlatformIdentity<Name> & {
82
+ new (_: never): {};
83
+ };
84
+
85
+ /** The class surface of {@link Singleton}. */
86
+ export interface SingletonClass {
87
+ // `Self` names the class (`extends Cluster.Singleton<Reaper>()`).
88
+ <Self>(): {
89
+ /**
90
+ * Declared with its implementation: an Effect that resolves bindings
91
+ * once per runner and returns the fiber. Provide a pool Layer to
92
+ * `impl` to deploy it; `yield*` the class in the Stack.
93
+ */
94
+ <const Name extends string, InitReq = never>(
95
+ name: Name,
96
+ props: InputProps<SingletonProps>,
97
+ impl: Effect.Effect<Effect.Effect<void, unknown, Scope>, ConfigError.ConfigError, InitReq>,
98
+ ): SingletonInline<Name, Providers | Exclude<InitReq, SingletonServices | PlatformServices>>;
99
+ /** A bare declaration, implemented by `.make(props, impl)`. */
100
+ <const Name extends string>(name: Name): SingletonTag<Self, Name>;
101
+ };
102
+ }
103
+
104
+ /**
105
+ * A **Singleton** on an Effect Cluster: one fiber, running on exactly one
106
+ * of its pool. If that runner stops, another starts it.
107
+ *
108
+ * Declare it by name, with its implementation: an Effect that resolves
109
+ * bindings once per runner and returns the fiber, which only the runner
110
+ * that owns the singleton runs. Providing a pool Layer to the
111
+ * implementation is what deploys it. Or declare it bare and implement it
112
+ * with `.make(props, impl)`, as a Layer.
113
+ *
114
+ * ### Declaring a singleton
115
+ * **Example:** A loop that runs once across the cluster
116
+ * ```typescript
117
+ * // src/Reaper.ts
118
+ * export default class Reaper extends Cluster.Singleton<Reaper>()(
119
+ * "Reaper",
120
+ * { main: import.meta.url },
121
+ * Effect.gen(function* () {
122
+ * const counters = yield* Counter;
123
+ * return Effect.gen(function* () {
124
+ * const counter = yield* counters.getByName("reaped");
125
+ * yield* counter.Increment({ amount: 1 });
126
+ * }).pipe(Effect.repeat(Schedule.spaced("1 minute")), Effect.orDie);
127
+ * }).pipe(Effect.provide(Default)),
128
+ * ) {}
129
+ * ```
130
+ *
131
+ * **Example:** Declared bare, and implemented as a Layer
132
+ * ```typescript
133
+ * // src/Reaper.ts
134
+ * export class Reaper extends Cluster.Singleton<Reaper>()("Reaper") {}
135
+ *
136
+ * // src/ReaperLive.ts
137
+ * export default Reaper.make(
138
+ * { main: import.meta.url },
139
+ * Effect.gen(function* () {
140
+ * const counters = yield* Counter;
141
+ * return Effect.gen(function* () {
142
+ * const counter = yield* counters.getByName("reaped");
143
+ * yield* counter.Increment({ amount: 1 });
144
+ * }).pipe(Effect.repeat(Schedule.spaced("1 minute")), Effect.orDie);
145
+ * }).pipe(Effect.provide(Default)),
146
+ * );
147
+ * ```
148
+ *
149
+ * @resource
150
+ */
151
+ export const Singleton: SingletonClass = (() =>
152
+ (name: string, props?: InputProps<SingletonProps>, impl?: unknown) =>
153
+ declare(name, {}, props, impl)) as unknown as SingletonClass;