-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjob.js
76 lines (66 loc) · 1.57 KB
/
job.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
const config = require('./backend/utils/config')
console.log(config);
const mongoose = require('mongoose')
const Pool = require('pg').Pool
const db = new Pool({
user: config.PG_USER,
host: config.PG_HOST,
database: config.PG_DB,
password: config.PG_PASSWORD,
port: config.PG_PORT,
})
// CRON COMMAND
// RUN EVERY HOUR
// 0 * * * * /usr/local/bin/node /path/to/email.js
const raindropSchema = new mongoose.Schema({
bucket_path: {
type: String,
required: true,
},
headers: {
type: Object,
required: true,
},
payload: {
type: Object,
required: true,
}
})
const Raindrop = mongoose.model('Raindrop', raindropSchema);
mongoose.set('strictQuery', false);
mongoose.connect(config.TEST_MONGODB_URI);
const getExpiredBuckets = async () => {
try {
let result = await db.query(
"SELECT bucket_path FROM buckets WHERE creation_date < now()-'1 day'::interval"
);
return result.rows;
} catch (error) {
throw error;
}
}
async function deleteRaindropPayload(bucketPath) {
try {
return await Raindrop.deleteMany({bucket_path: bucketPath});
} catch (error) {
throw error;
}
}
const deleteBucket = async (bucketPath, res) => {
try {
await db.query(
'DELETE FROM buckets WHERE bucket_path = $1',
[bucketPath]
);
} catch (error) {
throw error;
}
}
async function deleteExpiredRaindropPayloads() {
let expiredBuckets = await getExpiredBuckets();
expiredBuckets.forEach((row) => {
deleteRaindropPayload(row.bucket_path);
deleteBucket(row.bucket_path);
})
}
deleteExpiredRaindropPayloads();