var http = require('http');
var URL = require('url');
module.exports = function(agenda) {


  agenda.define('fire callback url', function(job, done) {


    var postData = querystring.stringify(job);
    var cburl = URL.parse(job.url,true, true);
    console.log("cburl " + cburl);
    var options = {
      hostname: URL.parse(job.url).hostname,
      port: URL.parse(job.url).port,
      path: URL.parse(job.url).path,
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Content-Length': postData.length
      }
    };

    var req = http.request(options, (res) => {
      console.log(`STATUS: ${res.statusCode}`);
      console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
      res.setEncoding('utf8');
      res.on('data', (chunk) => {
        console.log(`BODY: ${chunk}`);
      });
      res.on('end', () => {
        console.log('No more data in response.')
      })
    });

    req.on('error', (e) => {
      console.log(`problem with request: ${e.message}`);
    });

// write data to request body
    req.write(postData);
    req.end();

  });
  agenda.start(job.attrs.newRunAt.toDate(),"fire callback url");
  process.on('message', function(msg) {console.log(msg)});
  // More email related jobs
}