Skip to content

Commit

Permalink
Merge pull request #4 from typov-software/fix/dequeue-schema
Browse files Browse the repository at this point in the history
Use recommendations from db lint
  • Loading branch information
davidglivar authored May 4, 2024
2 parents 930328b + b0b29dc commit 93f2c24
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
33 changes: 20 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ Supaworker is a job queue for Supabase projects.
## Usage

Supaworker is a job queue that is backed by your Supabase database.
Jobs are enqueued as rows in a `"supaworker"."jobs"` table where background workers can pick them up and process them.
Jobs are enqueued as rows in a `"supaworker"."jobs"` table where background workers can dequeue and process them.

A worker is a Supaworker client does the following:
A worker does the following:

1. Dequeues jobs that match the worker's `queue`
2. Processes jobs until there are none left
Expand All @@ -21,21 +21,29 @@ Supaworker is _**not**_ designed to be used with Edge Functions, but instead wit

### Setup

Create a new migration
The first integration step is to add the Supaworker schema to your Supabase project.

```bash
supabase migration new setup_supaworker
```

And add the following SQL from [here](./supabase/migrations/20240407025302_setup_supaworker.sql).
Carefully review and add the following SQL from [here](./supabase/migrations/20240407025302_setup_supaworker.sql).

Then run the migration:
Run the migration:

```bash
supabase migration up --local
```

Add Supaworker to your project:
Sync the schema to your Supabase project:

```bash
supabase db lint
supabase test db
supabase db push
```

Add [`supaworker-js`](https://www.npmjs.com/package/supaworker-js) to your project:

```bash
npm install --save supaworker-js
Expand All @@ -51,6 +59,7 @@ Create a new project
mkdir my-worker && cd my-worker
npm init -y
npm install --save supaworker-js
touch index.js
```

Edit package.json to use ESM modules:
Expand All @@ -73,7 +82,6 @@ const clientOptions = {

const workerOptions = {
queue: "example",
debug: true,
};

const { enqueue, worker } = createSupaworker(
Expand Down Expand Up @@ -104,7 +112,7 @@ await worker.stop();
Run the worker:
```sh
```bash
SUPABASE_URL="" \
SUPABASE_SERVICE_ROLE_KEY="" \
node index.js
Expand All @@ -116,7 +124,7 @@ Create a new project
```bash
mkdir my-worker && cd my-worker
bun init -y
bun init
bun add supaworker-js
```
Expand All @@ -130,13 +138,12 @@ import {
} from "supaworker-js";

const clientOptions: SupaworkerClientOptions = {
supabase_url: process.env.SUPABASE_URL ?? "",
supabase_service_role_key: process.env.SUPABASE_SERVICE_ROLE_KEY ?? "",
supabase_url: import.meta.env.SUPABASE_URL ?? "",
supabase_service_role_key: import.meta.env.SUPABASE_SERVICE_ROLE_KEY ?? "",
};

const workerOptions: SupaworkerOptions = {
queue: "example",
debug: true,
};

const { enqueue, worker } = createSupaworker<{ message: string }>(
Expand Down Expand Up @@ -167,7 +174,7 @@ await worker.stop();
Run the worker:
```sh
```bash
SUPABASE_URL="" \
SUPABASE_SERVICE_ROLE_KEY="" \
bun run index.ts
Expand Down
4 changes: 2 additions & 2 deletions supabase/migrations/20240407025302_setup_supaworker.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ CREATE TABLE IF NOT EXISTS "supaworker"."jobs" (
ALTER TABLE "supaworker"."jobs" OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "supaworker"."dequeue"("queue_name" character varying) RETURNS SETOF "supaworker"."jobs" LANGUAGE "plpgsql" AS $$ #variable_conflict use_variable
begin return query
delete from "jobs"
delete from "supaworker"."jobs"
where id = (
select id
from "jobs"
from "supaworker"."jobs"
where enabled = true
and queue = queue_name
order by created_at asc for
Expand Down

0 comments on commit 93f2c24

Please sign in to comment.