🖍️
graphql-node-jobs
  • Graphql-Node-Jobs
  • What's a Job/Batch/Pipeline?
  • Running the server
  • Creating a worker
  • The GNJ API
  • Plugin the server to your express
  • Q&A
  • Contributing
Powered by GitBook
On this page
  • checkForJobs()
  • Facilities
  • Avoiding spamming your API
  • Identifying workers

Was this helpful?

Creating a worker

You can create your workers from scratch using the Graphql schema. Or you can use the few utilities functions available to quickly setup a worker.

checkForJobs()

worker.ts/js
// Create an ApolloClient that uses your endpoint url
// Here it is using the default url provided by the out-of-the-box server
const client = New ApolloClient({uri : 'http://localhost:8080/graphql'})

const job = await checkForJobs({
  typeList: ['myJobType'],
  client,
  processingFunction: async (job) => {
    const result = await myApiCall()
    return myProcessingFunction(result)
  }
})
  • typeList (Array<String>) is the type of jobs the worker will wait to execute.

  • client (ApolloClient) is a client with the URL of your GraphQL endpoint.

  • processingFunction (Function(job, facilities) => Promise<JsonObject>) is the function that executes the job.

  • workerId = undefined

  • looping = true

  • loopTime = 1000

When processingFunction return something, the job is considered as done. The returned Object is serialized and stored as the "output" of the job.

Easy debug without looping

With the looping option to false the worker will only check for a job once. Made for end-to-end tests or manual debugging.

worker.ts/js
const job = await checkForJobs({
  ...,
  processingFunction: ...,
  looping: false
})

Facilities

updateProcessingInfo()

worker.ts/js
const job = await checkForJobs({
  typeList: ['myJobType'],
  client,
  processingFunction: async (job, { updateProcessingInfo }) => {
    await updateProcessingInfo({ percent: 10 })
  },
  looping: false
})

Avoiding spamming your API

Some jobs runs very rarely, so you may want that your worker only check the queue from time to time.

worker.ts/js
const job = await checkForJobs({
  ...,
  processingFunction: ...,
  loopTime: 60 * 1000 // Every minute
})

Identifying workers

GNJ automatically generated uuid for your new workers to easily track what went wrong. But if you need to link a run to an id your want you can just specify it under the workerId property.

worker.ts/js
const job = await checkForJobs({
  ...,
  processingFunction: ...,
  workerId: 'manualCallPreMigration'
})

They all use the GraphQL api provided by the server. So even if it's really convenient to use those functions you can create your own.

PreviousRunning the serverNextThe GNJ API

Last updated 5 years ago

Was this helpful?