'use strict';

// Load dependencies
const Logger = require('./logger');
const JobQueue = require('./job-queue');

/**
 * Creates a job in the job queue with the given information.  Logs if the job
 * creation fails.  Returns nothing but an indicator that the job was created in
 * the job queue.
 * 
 * @param {string} name - The name of the job to create in the queue
 * @param {object} data - The payload to pass to the job processor
 * @param {string} priority - How important the job is in the grand scheme of things
 * @param {boolean} removeOnComplete - Whether the job is deleted after completion
 * @param {number} [attempts] - Number of attempts before failure
 * @param {number} [delay] - Milliseconds to delay between attempts
 * 
 * @return {void}
 */
function createJob(name, data, priority, removeOnComplete, attempts = 10, delay = 2000) {
  JobQueue.create(name, data)
    .priority(priority)
    .attempts(attempts)
    .backoff({ delay, type: 'fixed' })
    .removeOnComplete(removeOnComplete)
    .save(error => {
      if (error) {
        // Could not save to the queue.  Maybe Redis is down or the library has
        // a bug or we messed up.  Whatever the case log what happened.
        Logger.error('dispatch.createJob: %s, Job name: %s, Job data:', error, name, data);
      }
    });
}

/**
 * Dispatches a write job to the job queue.
 * 
 * @param {object} data - Contains schema information and the values to write
 * @return {void}
 */
exports.dispatchWriteJob = function dispatchWriteJob(data) {
  return createJob('database_writer', data, 'normal', true);
};

/**
 * Dispatches a crawl job based on the namespace provided.  Has knowledge of how
 * to handle the different crawl jobs that the application is capable of handling.
 * Creates a job in the job queue and returns when the job is created.
 * 
 * @param {object} data - Data to use for the job.  This data is structured on a
 *   job-by-job basis.  There is no specific format in general except that each
 *   data object will contain a
 * @param {string} data.namespace - The name of the crawl job that should be created
 * 
 * @return {void}
 */
exports.dispatchCrawlJob = function dispatchCrawlJob(job) {
  return createJob(job.namespace, job.data, 'high', false);
};
@@ -815,16 +815,26 @@
 omplete,
+ callback,
  attempt
@@ -838,17 +838,16 @@
 mpts = 1
-0
 , delay 
@@ -858,16 +858,25 @@
 00) %7B%0A  
+  return 
 JobQueue
@@ -895,16 +895,20 @@
 , data)%0A
+    
     .pri
@@ -927,16 +927,20 @@
 ty)%0A    
+   
+ 
 .attempt
@@ -951,16 +951,20 @@
 tempts)%0A
+    
     .bac
@@ -998,16 +998,20 @@
  %7D)%0A    
+   
+ 
 .removeO
@@ -1038,16 +1038,20 @@
 mplete)%0A
+    
     .sav
@@ -1056,298 +1056,130 @@
 ave(
-error =%3E %7B%0A      if (error) %7B%0A        // Could not save to the queue.  Maybe Redis is down or the library has%0A        // a bug or we messed up.  Whatever the case log what happened.%0A        Logger.error('dispatch.createJob: %25s, Job name: %25s, Job data:', error, name, data
+callback);%0A%7D%0A%0Afunction errorLogger(error) %7B%0A    if (error) %7B%0A        Logger.error('dispatch.createJob: %25s', error
 );%0A
-  
     %7D%0A
-    %7D);%0A
 %7D%0A%0A/
@@ -1386,24 +1386,26 @@
 Job(data) %7B%0A
+  
   return cre
@@ -1450,16 +1450,29 @@
 l', true
+, errorLogger
 );%0A%7D;%0A%0A/
@@ -2073,20 +2073,32 @@
 lJob(job
+, callback
 ) %7B%0A
+  
   return
@@ -2146,13 +2146,346 @@
 ', false
-);
+, callback %7C%7C errorLogger);%0A%7D;%0A%0A%0A%0Aexports.dispatchCrawlJobPromise = function dispatchCrawlJob(job) %7B%0A%0A    return new Promise( (resolve, reject) =%3E %7B%0A%0A        dispatchCrawlJob(job, (error) =%3E %7B%0A            %0A            if (error) %7B%0A                return reject(error);%0A            %7D%0A%0A            return resolve();%0A        %7D);%0A%0A    %7D);%0A
 %0A%7D;

Return Promise from Job Dispatcher