Queue API
Process jobs with async message queues and worker functions. This API is automatically available from the inside of any Codehook function.
API quick overview
Datastore.open()
Datastore.open() opens a connection to a datastore in the active project space.
import {Datastore} from 'codehooks-js'
async function myfunc() {
const conn = await Datastore.open();
// use conn to call API functions
...
}
Returns Promise / Datastore connection
enqueue(topic, payload, options)
Add a queued job in a datastore. Jobs in the queue are processed by your worker function.
Parameters
- topic: queue topic string
- payload: any json data
- options: NOT IN USE
Returns Promise / JobId
Code example
import {app, Datastore} from 'codehooks-js'
// your worker function
app.worker('sendEmail', (req, res) => {
console.debug('Sending email to', req.body.payload.email);
// send email with your favorite service, e.g. Mailgun or Sendgrid
res.end(); // done processing queue item
})
app.post('/welcome', async (req, res) => {
const conn = await Datastore.open();
const jobId = await conn.enqueue("sendEmail", req.body);
res.status(200).json({"message": "Thanks for the signup, you will receive an email soon :)"});
})
export default app.init(); // Bind functions to the serverless runtime
Running the example with curl
curl --location --request POST 'https://<YOUR-PROJECT-NAME>.api.codehooks.io/dev/welcome' \
--header 'x-apikey: <YOUR-API-TOKEN>' \
--header 'Content-Type: application/json' \
--data-raw '{"email": "[email protected]"}'
{"message":"Thanks for the signup, you will receive an email soon :)"}
Your project name and token(s) can be retrieved by running the command 'coho info' when you are inside the project folder.
enqueueFromQuery(collection, query, topic, options)
Add multiple queued jobs from the result of a database query in a datastore.
Each object from the collection in the query result will be processed by your worker function.
If your collection has 1000 items, a {}
query will add 1000 items to the queue and call your worker function 1000 times.
Parameters
- collection: datastore collection name
- query: JSON query object
- topic: queue topic string
- options (advanced optional settings for indexes and filtering)
- useIndex: indexed field name
- Indexes are created with CLI command
coho createindex
- Indexes are created with CLI command
- startIndex: jump into the index
- endIndex: where to end in the index
- limit: how many items to return
- offset: how many items to skip before returning any
- reverse: reverese scan of index, i.e. descending sort
- useIndex: indexed field name
Returns Promise / {JobId, count}
Code example
import {app, Datastore} from 'codehooks-js'
// Codehooks route hooks
app.post('/add', add);
app.worker('mailworker', mailWorker);
// add all items in a collection to the queue worker function
async function add(req, res) {
const conn = await Datastore.open();
const query = {emailConsent: true}; // all objects that matches query
const topic = 'mailworker';
const job = await conn.enqueueFromQuery('emaildata', query, topic);
console.log("Queue job", job);
res.end(job);
}
// worker function
async function mailWorker(req, res) {
let {payload} = req.body;
console.log("Mail worker", payload.email);
// fake that the job takes 100 ms
setTimeout(res.end, 100);
}
export default app.init(); // Bind functions to the serverless runtime
Running the example with curl
curl --location --request POST 'https://<YOUR-PROJECT-NAME>.api.codehooks.io/dev/add' \
--header 'x-apikey: <YOUR-API-TOKEN>'
{"count":2018,"jobId":"7c29faa2-d075-4fe6-af54-60dce6024f04"}