'use strict';

/**
 * Basic qless client object for interacting with qless.
 */

const redis = require('redis');
const os = require('os');
const process = require('process');

const luaScript = require('./lua_script');
const queue = require('./queue');
const jobs = require('./jobs');

class Client {
  /**
   * Parameters are passed through to redis.createClient()
   */
  constructor() {
    this.redis = redis.createClient.apply(redis, arguments);
    this._lua = new luaScript.LuaScript('qless', this.redis);
    this.jobs = new jobs.Jobs(this);
    this.workerName = [os.hostname(), process.env.HOST, process.pid.toString()].join('-');
  }

  /**
   * Args: command, arg1, arg1, ..., cb
   */
  call() {
    // convert arguments to real array
    const args = Array.prototype.slice.call(arguments, 0);
    const cb = args.pop();

    // Add "time" between command and rest of args
    const time = new Date().getTime() / 1000.0;
    args.splice(1, 0, time);
    this._lua.call(args, cb);
  }

  /**
   * Get a Queue object for the given queue name
   */
  queue(name) {
    // TODO: still not sure if we want to make all workers under one process have the same workerName.
    // Seems like they would just step on each other as they are independent (e.g., two workers working
    // on the same job and renewing each other's lock/lease on the job). This seems to be what the
    // Python ForkingWorker does, but perhaps they are not so independent under that model? Still it seems
    // possible that two workers could be working on the same job and there is no check to see if this is
    // happenning?
    return new queue.Queue(name, this, this.workerName);
  }
}

module.exports = { Client };

@@ -1079,47 +1079,47 @@
  // 
-TODO: still not sure if we want to make
+Note: As with the Python ForkingWorker,
  all
@@ -1144,16 +1144,23 @@
  process
+%0A    //
  have th
@@ -1181,219 +1181,156 @@
 ame.
-%0A    // Seems like they would just step on each other as they are independent (e.g., two workers working%0A    // on the same job and renewing each other's lock/lease on the job). This seems to be what 
+ Dan says: %22They do occasionally step on each others'%0A    // toes (one process loses its lock on a job when the other pops it off, then bo
 th
-e
 %0A    // 
 Pyth
@@ -1329,181 +1329,160 @@
  // 
-Python ForkingWorker does, but perhaps they are not so independent under that model? Still it seems%0A    // possible that two workers could be working on the same job
+are notified that the lock is lost and second one drops it). The upside to this%0A    // design decision is that we have fewer unique worker names
  and the
 re i
@@ -1481,59 +1481,189 @@
  the
+y'
 re 
-is no check to see if this is%0A    // happenning?
+not%0A    // constantly changing. In practice, this happens infrequently and negligibly%0A    // impacts the delivery of useful work.%22 See https://github.com/seomoz/qless-js/pull/4
 %0A   

modify comment to explain design decision