Supaqueue is a job queue for Supabase projects.
Note: This is a work in progress and is not yet ready for production use.
Supaqueue is a job queue that is backed by your Supabase database.
Jobs are enqueued as rows in a supaqueue_jobs
table where background workers can pick them up and process them.
A worker is a Supaqueue client does the following:
- Dequeues jobs that match the worker's
queue
- Processes jobs until there are none left
- Listens for new jobs using Supabase's Realtime feature
Timeouts, delayed retries, and scale are left to the developer.
Create a new migration
supabase migration new setup_supaqueue
And add the following SQL from here.
CREATE TABLE IF NOT EXISTS "public"."supaqueue_jobs" (
"id" bigint NOT NULL,
"created_at" timestamp with time zone DEFAULT "now"() NOT NULL,
"queue" "text" NOT NULL,
"enabled" boolean DEFAULT true NOT NULL,
"attempts" smallint DEFAULT '0'::smallint NOT NULL,
"options" "jsonb",
"payload" "jsonb"
);
ALTER TABLE "public"."supaqueue_jobs" OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."dequeue"("queue_name" character varying) RETURNS SETOF "public"."supaqueue_jobs" LANGUAGE "plpgsql" AS $$ #variable_conflict use_variable
begin return query
delete from "supaqueue_jobs"
where id = (
select id
from "supaqueue_jobs"
where enabled = true
and queue = queue_name
order by created_at asc for
update skip locked
limit 1
)
returning *;
end;
$$;
ALTER FUNCTION "public"."dequeue"("queue_name" character varying) OWNER TO "postgres";
ALTER TABLE "public"."supaqueue_jobs"
ALTER COLUMN "id"
ADD GENERATED BY DEFAULT AS IDENTITY (
SEQUENCE NAME "public"."supaqueue_jobs_id_seq" START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1
);
CREATE TABLE IF NOT EXISTS "public"."supaqueue_logs" (
"id" "uuid" DEFAULT "gen_random_uuid"() NOT NULL,
"created_at" timestamp with time zone DEFAULT "now"() NOT NULL,
"job" "jsonb" NOT NULL,
"status" character varying NOT NULL
);
ALTER TABLE "public"."supaqueue_logs" OWNER TO "postgres";
ALTER TABLE ONLY "public"."supaqueue_jobs"
ADD CONSTRAINT "supaqueue_jobs_pkey" PRIMARY KEY ("id");
ALTER TABLE ONLY "public"."supaqueue_logs"
ADD CONSTRAINT "supaqueue_logs_pkey" PRIMARY KEY ("id");
ALTER TABLE "public"."supaqueue_jobs" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "public"."supaqueue_logs" ENABLE ROW LEVEL SECURITY;
Then run the migration:
supabase migration up --local
From Supabase studio, head to Database -> Replication. In the row named "supabase_realtime", click the "x table(s)" button under the "Source" column and toggle the "supaqueue_jobs" table to enabled.
Add Supaqueue to your project:
npm install --save supaqueue-js