跳至正文
Wener Notes

graphile-worker

约 2 分钟阅读
    • MIT, TS
    • 支持 cron
    • PostgreSQL
      • LISTEN/NOTIFY
        • pgbouncer 需要 connection 模式
      • 默认 schema graphile_worker
bash
npm add graphile-worker
依赖包
bash
docker run \
  --init \
  --rm -it \
  --network=host \
  -v "$PWD/tasks":/worker/tasks \
  graphile/worker \
  -c "postgres://postgres:postgres@localhost:5432/postgres"
  • GRAPHILE_ENABLE_DANGEROUS_LOGS
  • GRAPHILE_LOGGER_DEBUG

Library

TypeScript
// worker
import { run, quickAddJob } from 'graphile-worker';
import type { Task, WorkerUtils } from 'graphile-worker';

const runner = await run({
  connectionString: 'postgres:///my_db',
  concurrency: 5,
  // Install signal handlers for graceful shutdown on SIGINT, SIGTERM, etc
  noHandleSignals: false,
  pollInterval: 1000,
  // you can set the taskList or taskDirectory but not both
  taskList: {
    hello: async (payload, helpers) => {
      const { name } = payload;
      helpers.logger.info(`Hello, ${name}`);
    },
  },
  // or:
  //   taskDirectory: `${__dirname}/tasks`,
});

await runner.promise;

// 任务定义
export interface TaskSpec {
  // 一个 queue 里会顺序运行
  queueName?: string;
  // 未来的运行时间 - 默认 now
  runAt?: Date;
  // 任务按优先级的数字升序执行(数字较小的优先级的任务先运行)。(默认值:0)
  priority?: number;
  // 这个任务应该尝试几次?最小值为1,此时任务只会尝试一次,不会重试。 (默认值:25)
  maxAttempts?: number;
  // 任务的唯一标识符,如果需要,可以用来稍后更新或删除它。 (默认值:null)
  jobKey?: string;
  // 修改`jobKey`的行为;当为'replace'时,所有属性都会被更新,当为'preserve_run_at'时,除'run_at'外的所有属性都会被更新,当为'unsafe_dedupe'时,只有在不存在具有匹配任务键的现有任务(包括锁定的任务和永久失败的任务)时,才会添加新任务。 (默认值:'replace')
  jobKeyMode?: 'replace' | 'preserve_run_at' | 'unsafe_dedupe';
  // 任务的标志,可以用来动态过滤哪些任务可以在运行时运行,哪些任务不能运行。 (默认值:null)
  flags?: string[];
}
text
┌───────────── UTC minute (0 - 59)
│ ┌───────────── UTC hour (0 - 23)
│ │ ┌───────────── UTC day of the month (1 - 31)
│ │ │ ┌───────────── UTC month (1 - 12)
│ │ │ │ ┌───────────── UTC day of the week (0 - 6) (Sunday to Saturday)
│ │ │ │ │ ┌───────────── task (identifier) to schedule
│ │ │ │ │ │    ┌────────── optional scheduling options
│ │ │ │ │ │    │     ┌────── optional payload to merge
│ │ │ │ │ │    │     │
│ │ │ │ │ │    │     │
* * * * * task ?opts {payload}
  • parsedCronItems
  • graphile.config.ts
graphile.config.ts
import type {} from "graphile-config";
import type {} from "graphile-worker";

const preset: GraphileConfig.Preset = {
  worker: {
    connectionString: process.env.DATABASE_URL,
    maxPoolSize: 10,
    pollInterval: 2000,
    preparedStatements: true,
    schema: "graphile_worker",
    crontabFile: "crontab",
    concurrentJobs: 1,
    fileExtensions: [".js", ".cjs", ".mjs"],
  },
};

export default preset;

关联信息

反向链接、本文链接的其他页面和外部资料。

反向链接

  • Nodejs Awesome
    笔记 · graphile-worker

    最好选择 TypeScript 开发的或支持 TypeScript 的 · TypeScript 的 decorator 比 Java 的 Annotation 弱得多 · 不支持获取字段实际类型信息 - 因为不存在这样的信息 · 最近一两年 (2020-2021),可能是因为 Go 和 Rust 的盛行,导致 Nodejs 的后端开发弱化了,很多项目...

参考资料

GitHub1 条
最近更新commit e15cc1a编辑