{"size": 1357, "ext": "js", "max_stars_repo_path": "src/worker.js", "max_stars_repo_name": "kritzware/nodeq", "max_stars_repo_stars_event_min_datetime": "2020-04-08T08:09:17.000Z", "max_stars_repo_stars_event_max_datetime": "2020-04-08T08:09:17.000Z", "max_issues_repo_path": "src/worker.js", "max_issues_repo_name": "kritzware/nodeq", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/worker.js", "max_forks_repo_name": "kritzware/nodeq", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 20.8769230769, "max_line_length": 60, "alphanum_fraction": 0.6035372144}
"use strict"

const crypto = require('crypto')
const events = require('events')
const redis = require('redis')

const Job = require('./job')

const Worker = class {

  constructor({
    host='127.0.0.1',
    port=6379
  }) {
    this.redis = { host, port }
    this.redis.client = this.createRedisClient(host, port)
    events.EventEmitter.call(this)
  }

  createRedisClient(host, port) {
    return redis.createClient({ host, port })
  }

  addJob({
    name,
    interval,
    timeout=6000
  }, method) {
    const job = new Job({ name, interval, method, timeout })
    const n = job.getNextOccurence()
    const milliseconds_to_next_run = n - new Date()
    console.log(n)
    console.log(milliseconds_to_next_run)
  }

  completedJob() {
    this.job = true
  }

  async start() {



    // console.time('jobs')
    // for(const job of this.jobs) {
    //   try {
    //     await job.method(this.completedJob.bind(job))
    //   } catch(err) {
    //     // console.log(err)
    //     this.emit('error', job, err)
    //     job.retry = true
    //   }
    // }
    // console.timeEnd('jobs')
    // console.time('remove_completed')
    // this.jobs = this.jobs.filter(j => j.retry)
    // console.timeEnd('remove_completed')
    // // console.log(this.jobs)
  }

}

Worker.prototype.__proto__ = events.EventEmitter.prototype

module.exports = Worker