npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

seam-graphile-worker

v2.0.0

Published

This is a wrapper for Seam's opinionated usage of Graphile Worker.

Downloads

13

Readme

Seam Graphile Worker

This is a wrapper for Seam's opinionated usage of Graphile Worker.

Usage

seam-graphile-worker is made for usage with server projects. It will automatically connect to a database, run migrations, configure crontabs etc.

  1. Install via npm add seam-graphile-worker
  2. Run seam-graphile worker init to create a start:worker script, a seam-graphile-worker.config.ts file, and some other boilerplate files in the src/worker directory and src/tasks directory.
  3. To run your worker, run npm run start:worker

Features

  • Automatically parse environment database connection params via pg-connection-from-env
  • Workers log errors to Sentry
  • Workers have a health endpoint
  • See all your healthy workers by polling seam_graphile_worker.worker_health
  • Automatically restart jobs that were taken by an unhealthy worker
  • Task index is linted to include all tasks with predictable names
  • Define your cron jobs in a simple typed file format
  • Automatically validate input payloads against zod schemas
  • Prevent business-logic errors from retrying automatically
  • Run jobs or crons in tests easily

Defining A New Task

Tasks

// e.g. src/tasks/my_task.ts
import { withTaskSpec } from "worker/with-task-spec"

export default withTaskSpec({
  task_name: "my_task",
})(async (payload, opts) => {
  // ...
})

Customizing Worker Task Middleware

To customize worker middleware, edit the src/worker/with-task-spec.ts file.

import {
  withSentry,
  withDatabasePool,
  createWithTaskSpec,
} from "seam-graphile-worker"

export const withTaskSpec = createWithTaskSpec({
  middlewares: [withSentry, withDatabasePool],
})

Defining New Task Middleware

You can define new Task Middleware by creating files in src/worker/middlewares/with-*.ts

// e.g. src/worker/middlewares/with-workspace.ts
import { TaskMiddleware } from "seam-graphile-worker"

export const withWorkspace: TaskMiddleware<{
  opts_output: {
    workspace: { name: string; created_at: Date }
  }
  opts_dependencies: {
    db: DatabaseConnection
  }
  payload_dependencies: {
    workspace_id: number
  }
}> = (next) => async (payload, opts) => {
  opts.workspace = await opts.db.one("SELECT * FROM workspace WHERE id = $1", [
    payload.workspace_id,
  ])

  return next(payload, opts)
}

Defining Cron Jobs

To define cronjobs, edit crontabs property of seam-graphile-worker.config.ts

import * as tasks from "worker/tasks"

export default {
  tasks,
  crontabs: [
    {
      task: "example_task",
      frequency: "1h",
      payload: { some_param: "some_value" }
      options: { priority: 1 }
    }
  ]
}

Deploying to Fly

Technical Details

seam-graphile-worker maintains two schemas:

  • graphile_worker - this is the graphile worker schema, this is not changed so that we have the same schema structure as upstream graphile worker
  • seam_graphile_worker - this schema adds additional information to the context for graphile_worker

Stuff seam-graphile-worker doesn't do that you should do

  • Introduce logging middleware that captures logs from your job, the payload, and other information then inserts it to a log table or sends to a logging platform.