Gravity
  • Welcome to Gravity
  • Getting Started
  • Stack
  • Updates
  • Rules For AI
  • Troubleshooting
  • Gravity Server
    • Introduction
    • Installation
      • Install Node.js
      • Database Setup
      • Stripe Setup
      • Mailgun Setup
      • Install Gravity
    • Application Structure
    • REST API
      • API Scopes
      • Webhooks
    • Authentication
      • Email Verification
      • Social Sign On
      • Two-Factor Authentication
    • Authorization
      • Feature Access and Plan Restrictions
      • Permissions (Roles)
    • Config
    • Environment Variables
    • Database Queries
    • Handling Errors
    • Logging
    • Localization
    • Push Notifications
    • Email Notifications
    • User Feedback
    • User Onboarding
    • File Uploads
    • Billing
      • Seat Billing
      • Usage Billing
    • Free Accounts
    • CLI Toolbelt
    • Testing
    • AI Tools
    • Background Jobs
    • Deployment
  • Gravity Web
    • Introduction
    • Tailwind & SCSS
    • Routing
    • Events
    • Authentication
    • Localization
    • Hooks
      • useAPI
      • usePlans
      • usePermissions
    • Components
      • Alert
      • Animate
      • Avatar
      • Badge
      • Breadcrumb
      • Button
      • Calendar
      • Card
      • Chart
      • Checklist
      • Credit Card
      • Detail
      • Dialog
      • Dropdown
      • Feedback
      • Form
      • Grid
      • Header
      • Helper
      • Icon
      • Image
      • Layout
      • Link
      • List
      • Loader
      • Logo
      • Nav
      • Onboarding
      • Pagination
      • Popover
      • Progress
      • Row
      • Search
      • Separator
      • Sheet
      • Social
      • Stat
      • Table
      • Tabs
      • Toast (Notification)
      • Tooltip
      • User
      • View
    • Views
    • Handling Errors
    • Deployment
  • Gravity Native
    • Introduction
    • Prerequisites
    • Installation
    • App Context
    • Authentication
    • Localisation
    • External Linking
    • Handling Errors
    • Navigation
    • Config
    • Events
    • Views
    • Components
      • Badge
      • Blankslate
      • Button
      • Card
      • Chart
      • Form
      • Global
      • Grid
      • Icon
      • List
      • Logo
      • Message
      • Modal
      • Nav
      • Notification
      • Progress Bar
      • Search
      • Separator
      • Social
      • Stat
      • View
    • Push Notifications
    • Payments
    • Building Your App
  • Mission Control
    • Introduction
    • Installation
    • User Management
    • Feedback
    • Events
    • Logs
  • Website Template
    • Introduction
    • Environment Variables
    • Styling
    • Components
      • Article
      • Feature List
      • Footer
      • Hero
      • Layout
      • Pricing
      • Meta Data
      • Nav
      • Testimonial
    • Build and Deploy
Powered by GitBook
On this page
  • 1. Add Your Redis URL
  • 2. Start The Worker
  • 3. Add a Job To The Queue
  • Updating the Job Progress
  • Updating The Job Data
  • Getting a Job's Status
  • Delete a Job

Was this helpful?

  1. Gravity Server

Background Jobs

PreviousAI ToolsNextDeployment

Last updated 1 year ago

Was this helpful?

Gravity supports background jobs with . These are used for offloading longer tasks to a separate process. There is a working example included that manages an .

For more information, .

1. Add Your Redis URL

You will need to set up a Redis database for tracking the job queue and then add the URL to the following environment variable:

REDIS_JOB_URL=

2. Start The Worker

Run the following command to start the background worker:

node worker

The worker will process jobs added to the queue.

If you're using Mongo, the setup wizard will inject mongo.connect() into the worker. If you bypassed the setup wizard, you need to add this manually.

There's also a Procfile included to start a separate worker dynos on automatically. Other platforms will vary in how you set this up.

The default worker has a setTimeout function in worker/index.js – you should replace this with your own job function.

3. Add a Job To The Queue

There are two ways to add a job to the queue in Gravity:

  1. Internally

  2. Using the API

To add a job on the server, use the following code:

const Queue = require('bull');
const jobQueue = new Queue('jobs', process.env.REDIS_JOB_URL);

async function addJob(){

 const job = await jobQueue.add({ /* your custom metadata here */ });
 
}

To add a job via the API, make a POST request to:

POST /api/job

Updating the Job Progress

It's helpful to set different statuses during the lifecycle of the job:

const job = await jobQueue.getJob(id);
job.progress('started');

Updating The Job Data

If you want to update the job metadata, make a PATCH request to:

PATCH /api/job/:id

The request body will be merged with the existing job data.

Getting a Job's Status

Once a job has begun, you'll want to check it's status and perform an action when it has completed. To do this, make a GET request to:

GET /api/job/:id

You can determine if a job has finished using the finishedOn key.

You can also fetch a job internally without using the API with:

const job = await jobQueue.getJob(id);

Delete a Job

If a job has already started, you can't delete it from the queue.

You can delete a job that hasn't been executed yet using the the API:

DELETE /api/job/:id

or anywhere in your internal app:

const job = await jobQueue.getJob(req.params.id);
await job.remove();
Bull.js
automated onboarding flow
please read the Bull docs
Heroku