- 
                Notifications
    
You must be signed in to change notification settings  - Fork 16
 
Tutorial
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.
The rethinkdb-job-queue requires an installation of RethinkDB. Please follow the installation procedure for your operating system to get RethinkDB up and running.
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.
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 --saveSeeing as this example it using the Nodemailer package you will also need to install Nodemailer as follows.
npm install nodemailer --saveAt this point we have all the programs and packages installed that we need to start coding.
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'.
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.
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.
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 
localhostat port28015. - 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)
  })
})- Introduction
 - Tutorial
 - Queue Constructor
 - Queue Connection
 - Queue Options
 - Queue PubSub
 - Queue Master
 - Queue Events
 - State Document
 - Job Processing
 - Job Options
 - Job Status
 - Job Retry
 - Job Repeat
 - Job Logging
 - Job Editing
 - Job Schema
 - Job Name
 - Complex Job
 - Delayed Job
 - Cancel Job
 - Error Handling
 
- Queue.createJob
 - Queue.addJob
 - Queue.getJob
 - Queue.findJob
 - Queue.findJobByName
 - Queue.containsJobByName
 - Queue.cancelJob
 - Queue.reanimateJob
 - Queue.removeJob
 - Queue.process
 - Queue.review
 - Queue.summary
 - Queue.ready
 - Queue.pause
 - Queue.resume
 - Queue.reset
 - Queue.stop
 - Queue.drop
 - Queue.Job
 
- Queue.host
 - Queue.port
 - Queue.db
 - Queue.name
 - Queue.r
 - Queue.id
 - Queue.jobOptions [R/W]
 - Queue.changeFeed
 - Queue.master
 - Queue.masterInterval
 - Queue.removeFinishedJobs
 - Queue.running
 - Queue.concurrency [R/W]
 - Queue.paused
 - Queue.idle
 
- Event.ready
 - Event.added
 - Event.updated
 - Event.active
 - Event.processing
 - Event.progress
 - Event.log
 - Event.pausing
 - Event.paused
 - Event.resumed
 - Event.completed
 - Event.cancelled
 - Event.failed
 - Event.terminated
 - Event.reanimated
 - Event.removed
 - Event.idle
 - Event.reset
 - Event.error
 - Event.reviewed
 - Event.detached
 - Event.stopping
 - Event.stopped
 - Event.dropped
 
- Job.setName
 - Job.setPriority
 - Job.setTimeout
 - Job.setDateEnable
 - Job.setRetryMax
 - Job.setRetryDelay
 - Job.setRepeat
 - Job.setRepeatDelay
 - Job.updateProgress
 - Job.update
 - Job.getCleanCopy
 - Job.addLog
 - Job.getLastLog