var kue = require('kue');
var trip = require('./trip');
// var socket = require('socket')

queue = kue.createQueue();

queue.process('driver_movement', function(job, done) {
  job_driver_movement(job.data.id_driver, job.data.step_index, job.data.position, done);
});

var job_driver_movement = function(id_driver, step_index, position, done) {
  // socket.emit('movement', id_driver, position);
  console.log(Date.now() + ' Driver ' + id_driver + ' make move ' + step_index + ' to ' + JSON.stringify(position));
  done();
};

queue.process('driver_new_trip', function(job, done) {
  job_driver_new_trip(job.data.id_driver, job.data.origin, done);
});

var job_driver_new_trip = function(id_driver, origin, done) {
  var trip = require('./trip');
  var Bot = require('../models/bot');

  Bot.find({id_driver})
  .then(function(bot) {
    if (bot.active) {
      return trip.generate_trip(bot, origin);
    } else {
      return false;
    }
  })
  .then(function(steps) {

    var current_delay = bot.delay;
    var last_step_index = steps.length - 1;

    steps.forEach(function(step, index) {
      queue.create('driver_movement', {
        id_driver: id_driver,
        position: step,
        step_index: index
      })
      .delay(current_delay)
      .removeOnComplete(true)
      .save();

      if(index === last_step_index) {
        queue.create('driver_new_course', {
          id_driver: id_driver,
          position: step
        })
        .delay(current_delay)
        .removeOnComplete(true)
        .save();
      }

      current_delay += delay;
    });

  });

};

job = {};

job.driver_new_trip = function(id_driver) {
  queue.create('driver_new_trip', {
    id_driver: id_driver
  })
  .delay(current_delay)
  .removeOnComplete(true)
  .save();
}

module.exports = job;

@@ -777,17 +777,16 @@
 /bot');%0A
-%0A
   Bot.fi
@@ -791,20 +791,44 @@
 find
-(%7Bid_driver%7D
+One(%7B drivers: id_driver %7D)%0A  .exec(
 )%0A  
@@ -855,17 +855,16 @@
 %7B%0A    if
- 
 (bot.act
@@ -880,100 +880,81 @@
     
-return trip.generate_trip(bot, origin);%0A    %7D else %7B%0A      return false;%0A    %7D%0A  %7D)%0A  .then(
+var current_delay = bot.precision;%0A      trip.generate_trip(bot, origin, 
 func
@@ -971,44 +971,12 @@
 ) %7B%0A
-%0A
     
-var current_delay = bot.delay;%0A
     
@@ -1016,16 +1016,20 @@
 h - 1;%0A%0A
+    
     step
@@ -1064,24 +1064,28 @@
 ex) %7B%0A      
+ 
+   
 queue.create
@@ -1106,32 +1106,36 @@
 ent', %7B%0A        
+   
+ 
 id_driver: id_dr
@@ -1140,32 +1140,36 @@
 driver,%0A        
+    
 position: step,%0A
@@ -1172,24 +1172,28 @@
 ep,%0A        
+ 
+   
 step_index: 
@@ -1200,27 +1200,35 @@
 index%0A      
+   
+ 
 %7D)%0A
+    
       .delay
@@ -1245,24 +1245,28 @@
 elay)%0A      
+ 
+   
 .removeOnCom
@@ -1279,24 +1279,28 @@
 true)%0A      
+   
+ 
 .save();%0A%0A  
@@ -1297,16 +1297,20 @@
 ave();%0A%0A
+    
       if
@@ -1331,32 +1331,80 @@
 t_step_index) %7B%0A
+            console.log('driver_new_trip');%0A    
         queue.cr
@@ -1424,14 +1424,12 @@
 new_
-course
+trip
 ', %7B
@@ -1431,32 +1431,36 @@
 p', %7B%0A          
+    
 id_driver: id_dr
@@ -1467,32 +1467,36 @@
 iver,%0A          
+   
+ 
 position: step%0A 
@@ -1494,32 +1494,36 @@
 n: step%0A        
+    
 %7D)%0A        .dela
@@ -1509,32 +1509,36 @@
      %7D)%0A        
+ 
+   
 .delay(current_d
@@ -1543,32 +1543,39 @@
 _delay)%0A        
+   
+ // 
 .removeOnComplet
@@ -1582,32 +1582,36 @@
 e(true)%0A        
+  
+  
 .save();%0A      %7D
@@ -1603,16 +1603,20 @@
 save();%0A
+    
       %7D%0A
@@ -1622,16 +1622,20 @@
 %0A%0A      
+    
 current_
@@ -1647,24 +1647,127 @@
  += 
-delay
+bot.precision;%0A        %7D);%0A      %7D);%0A    %7D%0A    done();%0A  %7D)%0A  .catch(function(err) %7B%0A    console.log(err)
 ;%0A    
-%7D
+done(
 );%0A
-%0A
   %7D)
@@ -1898,32 +1898,8 @@
  %7D)%0A
-  .delay(current_delay)%0A
   .r

Fix job creation