-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathworker.js
117 lines (111 loc) · 3.46 KB
/
worker.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var pg = require('pg-promise')();
var client = pg(process.env.DATABASE_URL);
var AWS = require('aws-sdk-promise');
var s3 = new AWS.S3();
var amqplib = require('amqplib');
async function createConnection() {
const connection = await amqplib.connect(process.env.CLOUDAMQP_URL + '?heartbeat=10');
connection.on('error', function(err) {
console.error('Connection was closed!')
console.error(err);
// The connection was closed by the server, open a new one.
createConnection();
});
createChannel(connection);
}
async function createChannel(connection) {
const channel = await connection.createChannel();
channel.on('error', function(err) {
console.error('Channel was closed!')
console.error(err);
// The channel was closed by the server, open a new one.
createChannel(connection);
});
await channel.assertQueue('transactions');
channel.consume('transactions', async function(message) {
if(message === null) {
console.error('Consumer was canceled');
process.exit(1);
return;
}
var action = message.properties.type;
var metadata = message.properties.headers;
switch(action) {
case 'commit':
await channel.assertQueue(metadata.queue);
(function _getMessage() {
getMessage(channel, metadata.queue, async function(done) {
if(done) {
await channel.ack(message);
await channel.deleteQueue(metadata.queue, {ifEmpty: true}); // To be safe, but it should always be empty.
return;
}
_getMessage();
}, function getanother() {
getMessage(channel, metadata.queue, function() {}, getanother);
});
})();
break;
default:
console.error('Unknown message type: ' + action);
channel.nack(message);
break;
}
});
}
createConnection();
function getMessage(channel, queue, callback, getanother) {
channel.get(queue).then(async function(message) {
if(message === false) {
callback(true);
return;
}
var action = message.properties.type;
var metadata = message.properties.headers;
var buffer = message.content;
switch(action) {
case 'putObject':
s3.putObject({
Bucket: process.env.S3_BUCKET_NAME,
Key: metadata.S3Prefix + '/' + metadata.name,
ContentLength: metadata.size,
ACL: metadata.ACL,
Body: buffer
}).promise().then(function() {
if(metadata.authenticated) {
return client.query(`
INSERT INTO objects ("userId", name, size, "ACL", authkey) VALUES ($1, $2, $3, $4, $5)
ON CONFLICT ("userId", name) DO UPDATE
SET (size, "ACL", authkey, "lastUpdated") = ($3, $4, $5, $6)
`, [metadata.userID, metadata.name, metadata.size, metadata.ACL, metadata.objectAuthkey, Date.now()]);
} else {
return client.query(`
INSERT INTO objects ("userId", name, size) VALUES ((SELECT id FROM users WHERE "S3Prefix" = $1), $2, $3)
ON CONFLICT ("userId", name) DO UPDATE
SET (size, "lastUpdated") = ($3, $4)
`, [metadata.S3Prefix, metadata.name, metadata.size, Date.now()]);
}
}).then(async function() {
await channel.ack(message);
callback();
}, function(err) {
console.log(metadata);
console.error(err);
setTimeout(function() {
channel.nack(message);
}, (err.retryDelay || 1) * 1000);
callback();
});
break;
default:
console.error('Unknown message type: ' + action);
await channel.nack(message);
callback();
break;
}
getanother();
}).catch(function(error) {
console.error('channel.get error', error);
throw error;
});
}