Skip to content

Tutorial

Grant Carthew edited this page Aug 23, 2016 · 16 revisions

How do I process jobs?

To really understand how to use rethinkdb-job-queue we need to go through a real example. In this document we will see how to send emails using the Nodemailer SMTP client.

The example we are going through will be very similar to the one on the README document with a little more detail.

This example is broken down into the following parts.

Installation

Install RethinkDB

The rethinkdb-job-queue requires an installation of RethinkDB. Please follow the installation procedure for your operating system to get RethinkDB up and running.

Install Node.js / NPM

Once you have RethinkDB installed you will need an installation of Node.js. Follow the installation procedure for Node.

Now that you have Node.js installed you will also have the "Node Package Manager" or NPM package installed. NPM is bundled with the Node.js installation package. Using the npm command line tool you can continue the installation.

Install rethinkdb-job-queue

If you are adding rethinkdb-job-queue to an existing project then change directory to that projects root directory. If you are starting from scratch you may want to initialize a project using npm init. Either way get into the directory you would like to install rethinkdb-job-queue.

Finally we install rethinkdb-job-queue by typing the following into the command line.

npm install rethinkdb-job-queue --save

Install Nodemailer

Seeing as this example it using the Nodemailer package you will also need to install Nodemailer as follows.

npm install nodemailer --save

At this point we have all the programs and packages installed that we need to start coding.

Create send-mail.js File

To make this a complete tutorial we need to make our code modular. In this section we are going to make a node module that returns a function we can use to email a new user to confirm their email address.

To start with, create a new file in your project directory called 'send-mail.js'.

Require Modules

At the top of the send-mail.js file add the following code.

const Queue = require('rethinkdb-job-queue')
const nodemailer = require('nodemailer')

These two statements give us access to rethinkdb-job-queue and nodemailer modules.

Configure Nodemailer

Now under the require statements add the following to configure Nodemailer.

const transporter = nodemailer.createTransport({
  service: 'Mailgun',
  auth: {
    user: '[email protected]',
    pass: 'your-api-key-here'
  }
}, {
  // Default message fields
  from: '"Application" <[email protected]>'
})

// setup e-mail data with unicode symbols
const mailOptions = {
  subject: 'Email Address Confirmation',
  text: `
  Thankyou for registering for this great Application.
  Please click the link below to confirm your email address.

  url`
}

There are two configurations above. The first creates the Nodemailer transport object with service, authentication, and default fields setup. The second is creating an object holding the Nodemailer options for sending email.

Create the Queue

Next we are going to use the rethinkdb-job-queue to create the Queue object.

const options = {
  db: 'JobQueue', // The name of the database in RethinkDB
  name: 'RegistrationEmail' // The name of the table in the database
}

const emailQueue = new Queue(options)

The above statements do alot.

  • Connect to a RethinkDB instance operating on the localhost at port 28015.
  • Create a database in

This creates a new Queue object called emailQueue with a queue name of RegistrationEmail within a database called 'JobQueue'. Within the RethinkDB

emailQ.process((job, next) => {
  console.log('Job processing started')
  mailOptions.to = job.to
  mailOptions.text = mailOptions.text.replace('url', job.url)
  return transporter.sendMail(mailOptions).then((info) => {
    console.dir(info)
    return next(info)
  }).catch((err) => {
    console.error(err)
    return next(err)
  })
})

Main

How It Works

Contributing

API

Queue Methods

Queue Properties

Queue Events

Job Methods

Job Properties

Documentation

Clone this wiki locally