Skip to content

Commit f4021c2

Browse files
committed
Federation constructor no more holds the process
Fix #39
1 parent 71c5067 commit f4021c2

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ Version 0.5.2
1313

1414
To be released.
1515

16+
- Fixed a bug of `new Federation()` constructor that if it is once called
17+
the process will never exit. [[#39]]
18+
19+
[#39]: https://github.com/dahlia/fedify/issues/39
20+
1621

1722
Version 0.5.1
1823
-------------

federation/middleware.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export class Federation<TContextData> {
115115
#kv: KvStore;
116116
#kvPrefixes: FederationKvPrefixes;
117117
#queue?: MessageQueue;
118+
#queueStarted: boolean;
118119
#router: Router;
119120
#nodeInfoDispatcher?: NodeInfoDispatcher<TContextData>;
120121
#actorCallbacks?: ActorCallbacks<TContextData>;
@@ -155,6 +156,7 @@ export class Federation<TContextData> {
155156
...(kvPrefixes ?? {}),
156157
};
157158
this.#queue = queue;
159+
this.#queueStarted = false;
158160
this.#router = new Router();
159161
this.#router.add("/.well-known/webfinger", "webfinger");
160162
this.#router.add("/.well-known/nodeinfo", "nodeInfoJrd");
@@ -175,8 +177,13 @@ export class Federation<TContextData> {
175177
15 * 60_000,
176178
60 * 60_000,
177179
].map((ms) => Temporal.Duration.from({ milliseconds: ms }));
180+
}
178181

179-
queue?.listen(this.#listenQueue.bind(this));
182+
#startQueue() {
183+
if (this.#queue != null && !this.#queueStarted) {
184+
this.#queue?.listen(this.#listenQueue.bind(this));
185+
this.#queueStarted = true;
186+
}
180187
}
181188

182189
async #listenQueue(message: OutboxMessage): Promise<void> {
@@ -681,6 +688,7 @@ export class Federation<TContextData> {
681688
activity: Activity,
682689
{ preferSharedInbox, immediate }: SendActivityOptions = {},
683690
): Promise<void> {
691+
this.#startQueue();
684692
if (activity.id == null) {
685693
activity = activity.clone({
686694
id: new URL(`urn:uuid:${crypto.randomUUID()}`),
@@ -741,6 +749,7 @@ export class Federation<TContextData> {
741749
contextData,
742750
}: FederationHandlerParameters<TContextData>,
743751
): Promise<Response> {
752+
this.#startQueue();
744753
onNotFound ??= notFound;
745754
onNotAcceptable ??= notAcceptable;
746755
const url = new URL(request.url);

0 commit comments

Comments
 (0)