AFFiNE/packages/common/infra/src/sync/job
2024-10-22 06:41:07 +00:00
..
__tests__ feat(infra): job system (#7212) 2024-07-02 09:17:40 +00:00
impl/indexeddb feat(infra): opti indexer performance (#8557) 2024-10-22 06:41:07 +00:00
index.ts feat(infra): job system (#7212) 2024-07-02 09:17:40 +00:00
queue.ts feat(infra): job system (#7212) 2024-07-02 09:17:40 +00:00
README.md feat(infra): job system (#7212) 2024-07-02 09:17:40 +00:00
runner.ts feat(core): run indexer in worker (#7418) 2024-07-04 15:37:26 +08:00

job

Job system abstraction for AFFiNE. Currently, only IndexedDBJobQueue is implemented; more backends will be implemented in the future.

Run background jobs in browser & distributed environment. runners can consume tasks simultaneously without additional communication.

Basic Usage

const queue = new IndexedDBJobQueue('my-queue');

await queue.enqueue([
  {
    batchKey: '1',
    payload: { a: 'hello' },
  },
  {
    batchKey: '2',
    payload: { a: 'world' },
  },
]);

const runner = new JobRunner(queue, job => {
  console.log(job);
});

runner.start();

// Output:
// { batchKey: '1', payload: { a: 'hello' } }
// { batchKey: '2', payload: { a: 'world' } }

batchKey

Each job has a batchKey, and jobs with the same batchKey are handed over to one runner for execution at once. Additionally, if there are ongoing jobs with the same batchKey, other runners will not take on jobs with this batchKey, ensuring exclusive resource locking.

In the future, batchKey will be used to implement priority.

timeout

If the job execution time exceeds 30 seconds, it will be considered a timeout and reassigned to another runner.

Error Handling

If an error is thrown during job execution, will log an error, but the job will be considered complete.