-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatest_work.js
92 lines (69 loc) · 2.16 KB
/
latest_work.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//Installed node modules: jquery underscore request express jade shelljs passport http sys lodash async mocha chai sinon sinon-chai moment connect validator restify ejs ws co when helmet wrench brain mustache should backbone forever debug
var _ = require('underscore')
, q = require('q')
, amqp = require('amqplib/callback_api');
// 'use strict';
class WorkQueue {
constructor(config) {
this.setEntireConfig(config);
this.connection = null;
}
getConfig(key = null) {
if (key) {
return this._config[key];
}
return this._config;
}
setConfig(key, value) {
this._config[key] = value;
return this._config[key];
}
setEntireConfig: (config) {
this._config = config;
return this._config;
}
clearConfig() {
this._config = [];
}
connect() {
return q((this.connection) ? this.connection : this.getConnection());
}
channel() {
if (!this.connection) {
throw new Error('You must define a connection object to get a channel object.');
}
return q((this.channel) ? this.channel : this.getChannel(this.connection));
}
getConnection() {
var deferred = q.defer()
, _this = this;
amqp.connect('amqp://localhost', (error, connection) => {
if (error) {
return deferred.reject(error);
}
this.connection = connection;
deferred.resolve(connection);
})
return deferred.promise;
}
getChannel(connection) {
var deferred = q.defer();
connection.createChannel('amqp://localhost', (error, channel) => {
if(error) {
return deferred.reject(error);
}
this.channel = channel;
deferred.resolve(channel);
})
return deferred.promise;
}
}
let wq = new WorkQueue({});
wq.connect()
.then(function (connection) {
return wq.channel(connection);
})
.then(function (channel) {
})
.catch(function (error) {
})