Unverified Commit 99d302f3 authored by Hong Minhee's avatar Hong Minhee
Browse files

Add option to manually start task queue

parent cfc1e513
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -31,6 +31,13 @@ To be released.
     -  Added `createExponentialBackoffPolicy()` function.
     -  Added `CreateExponentialBackoffPolicyOptions` interface.

 -  `Federation` object now allows its task queue to be started manually.
    [[#53]]

     -  Added `manuallyStartQueue` option to `CreateFederationOptions`
        interface.
     -  Added `Federation.startQueue()` method.

 -  Added `ChatMessage` class to Activity Vocabulary API.  [[#85]]

 -  Improved multitenancy (virtual hosting) support.  [[#66]]
@@ -52,6 +59,7 @@ To be released.

     -  `["fedify", "federation", "queue"]`

[#53]: https://github.com/dahlia/fedify/issues/53
[#66]: https://github.com/dahlia/fedify/issues/66
[#70]: https://github.com/dahlia/fedify/issues/70
[#85]: https://github.com/dahlia/fedify/issues/85
+23 −0
Original line number Diff line number Diff line
@@ -112,6 +112,29 @@ a different message queue.[^1]

[`RedisMessageQueue`]: https://jsr.io/@fedify/redis/doc/mq/~/RedisMessageQueue

### `manuallyStartQueue`

*This API is available since Fedify 0.12.0.*

Whether to start the task queue manually or automatically.

If `true`, the task queue will not start automatically and you need to
manually start it by calling the `Federation.startQueue()` method.

If `false`, the task queue will start automatically as soon as the first
task is enqueued.

By default, the queue starts automatically.

> [!TIP]
> This option is useful when you want to separately deploy the web server
> and the task queue worker.  In this case, you can start the task queue
> in the worker process, and the web server process doesn't start the task
> queue, but only enqueues tasks.  Of course, in this case, you need to
> provide a `MessageQueue` backend that can be shared between the web server
> and the worker process (e.g., a Redis-backed message queue) as
> the [`queue`](#queue) option.

### `documentLoader`

A JSON-LD document loader function that the `Federation` object uses to
+32 −2
Original line number Diff line number Diff line
@@ -87,6 +87,21 @@ export interface CreateFederationOptions {
   */
  queue?: MessageQueue;

  /**
   * Whether to start the task queue manually or automatically.
   *
   * If `true`, the task queue will not start automatically and you need to
   * manually start it by calling the {@link Federation.startQueue} method.
   *
   * If `false`, the task queue will start automatically as soon as
   * the first task is enqueued.
   *
   * By default, the queue starts automatically.
   *
   * @since 0.12.0
   */
  manuallyStartQueue?: boolean;

  /**
   * A custom JSON-LD document loader.  By default, this uses the built-in
   * cache-backed loader that fetches remote documents over HTTP(S).
@@ -255,6 +270,7 @@ export class Federation<TContextData> {
  #kvPrefixes: FederationKvPrefixes;
  #queue?: MessageQueue;
  #queueStarted: boolean;
  #manuallyStartQueue: boolean;
  #router: Router;
  #nodeInfoDispatcher?: NodeInfoDispatcher<TContextData>;
  #actorCallbacks?: ActorCallbacks<TContextData>;
@@ -309,6 +325,7 @@ export class Federation<TContextData> {
    };
    this.#queue = options.queue;
    this.#queueStarted = false;
    this.#manuallyStartQueue = options.manuallyStartQueue ?? false;
    this.#router = new Router();
    this.#router.add("/.well-known/webfinger", "webfinger");
    this.#router.add("/.well-known/nodeinfo", "nodeInfoJrd");
@@ -557,6 +574,19 @@ export class Federation<TContextData> {
    );
  }

  /**
   * Manually start the task queue.
   *
   * This method is useful when you set the `manuallyStartQueue` option to
   * `true` in the {@link createFederation} function.
   * @param contextData The context data to pass to the context.
   * @since 0.12.0
   */
  startQueue(contextData: TContextData): Promise<void> {
    this.#startQueue(contextData);
    return Promise.resolve();
  }

  /**
   * Create a new context.
   * @param baseUrl The base URL of the server.  The `pathname` remains root,
@@ -1586,7 +1616,7 @@ export class Federation<TContextData> {
        "The activity to send must have at least one actor property.",
      );
    }
    this.#startQueue(contextData);
    if (!this.#manuallyStartQueue) this.#startQueue(contextData);
    if (activity.id == null) {
      activity = activity.clone({
        id: new URL(`urn:uuid:${crypto.randomUUID()}`),
@@ -1805,7 +1835,7 @@ export class Federation<TContextData> {
            });
          }
        }
        if (this.#queue != null) this.#startQueue(contextData);
        if (!this.#manuallyStartQueue) this.#startQueue(contextData);
        return await handleInbox(request, {
          handle: route.values.handle ?? null,
          context,