forked from bradleyboy/standbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (127 loc) · 3.27 KB
/
index.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import fs from 'fs';
import client from './lib/client';
import parser from './lib/parser';
import { User, Room, Schedule, Standup } from './lib/db';
import { INTERVAL } from './constants';
import { isPaused } from './config';
import {
closeStandup,
startScheduledStandup,
warnStandup,
threatenStandup,
setChannelActiveStatus,
} from './actions';
process.on('unhandledRejection', (err) => {
if (process.env.DEBUG) {
console.trace(err);
throw err;
} else {
console.log('unhandledRejection:', err);
}
});
let timeoutId;
async function migrateUsers() {
const data = JSON.parse(
fs.readFileSync(process.env.USER_MIGRATIONS_FILE, 'utf8')
);
let total = 0;
let migrated = 0;
for (let i = 0; i < data.length; i++) {
const { oldId, newId } = data[i];
const user = await User.findOne({ where: { userId: oldId } });
if (user) {
migrated++;
user.userId = newId;
await user.save();
}
total++;
}
console.log(`User migration complete: ${migrated} migrated out of ${total}`);
}
let loopStart = Date.now();
async function runLoop() {
console.log('Loop begin, time since last run: ', Date.now() - loopStart);
loopStart = Date.now();
let errors = 0;
const schedules = await Schedule.scope('shouldStart').findAll({
limit: 5,
});
if (!isPaused) {
for (let i = 0; i < schedules.length; i++) {
try {
await startScheduledStandup(schedules[i]);
} catch (e) {
errors++;
console.log('unexpected error when starting scheduled standup', e);
}
}
}
const nagWarn = await Standup.scope('shouldWarn').findAll({
include: [Room],
limit: 5,
});
if (!isPaused) {
for (let i = 0; i < nagWarn.length; i++) {
try {
await warnStandup(nagWarn[i]);
} catch (e) {
errors++;
console.log('unexpected error when reminding standup', e);
}
}
}
const nagThreat = await Standup.scope('shouldThreat').findAll({
include: [Room],
limit: 5,
});
if (!isPaused) {
for (let i = 0; i < nagThreat.length; i++) {
try {
await threatenStandup(nagThreat[i]);
} catch (e) {
errors++;
console.log('unexpected error when warning standup', e);
}
}
}
const standups = await Standup.scope('shouldClose').findAll({
include: [Room],
limit: 1,
});
if (!isPaused) {
for (let i = 0; i < standups.length; i++) {
try {
await closeStandup(standups[i]);
} catch (e) {
errors++;
console.log('unexpected error when closing standup', e);
}
}
}
console.log(
'Loop end, time spent:',
Date.now() - loopStart,
`stats: ${schedules.length} started, ${nagWarn.length} reminded, ${nagThreat.length} warned, ${standups.length} closed. errors: ${errors}`
);
setTimeout(runLoop, INTERVAL);
}
function start() {
client.joined((channel) => {
setChannelActiveStatus(channel.id, true);
});
client.left((channelId) => {
setChannelActiveStatus(channelId, false);
});
client.listen(parser);
setTimeout(runLoop, INTERVAL);
}
process.on('SIGTERM', () => {
console.log('SIGTERM received.');
client.stop();
clearTimeout(timeoutId);
});
if (process.env.USER_MIGRATIONS_FILE) {
migrateUsers().then(() => start());
} else {
start();
}