-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
cronTasks.ts
202 lines (182 loc) · 4.38 KB
/
cronTasks.ts
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import * as cron from "cron";
import { prisma } from "./server/db";
import * as ztController from "~/utils/ztApi";
import { syncMemberPeersAndStatus } from "./server/api/services/memberService";
type FakeContext = {
session: {
user: {
id: string;
};
};
};
export const CheckExpiredUsers = async () => {
new cron.CronJob(
// "*/10 * * * * *", // every 10 seconds ( testing )
"0 0 0 * * *", // 12:00:00 AM (midnight) every day
async () => {
const expUsers = await prisma.user.findMany({
where: {
expiresAt: {
lt: new Date(),
},
isActive: true,
NOT: {
role: "ADMIN",
},
},
select: {
network: true,
id: true,
role: true,
},
});
// if no users return
if (expUsers.length === 0) return;
for (const userObj of expUsers) {
if (userObj.role === "ADMIN") continue;
const context: FakeContext = {
session: {
user: {
id: userObj.id,
},
},
};
for (const network of userObj.network) {
const members = await ztController.network_members(
// @ts-ignore
context,
network.nwid,
false,
);
for (const member in members) {
const ctx = {
session: {
user: {
id: userObj.id,
},
},
};
await ztController.member_update({
// @ts-ignore
ctx,
nwid: network.nwid,
central: false,
memberId: member,
updateParams: {
authorized: false,
},
});
}
}
// update user isActive to false
await prisma.user.update({
where: {
id: userObj.id,
},
data: {
isActive: false,
},
});
}
},
null,
true,
"America/Los_Angeles",
);
};
/**
* Updates the peers for all active users and their networks.
* This function is scheduled to run periodically using a cron job.
*
* Run every 5 minutes and if user is offline. There is no reason to update if the user is online.
* https://github.com/sinamics/ztnet/issues/313
*/
export const updatePeers = async () => {
new cron.CronJob(
// updates every 5 minutes
// "*/10 * * * * *", // every 10 seconds ( testing )
"*/5 * * * *", // every 5min
async () => {
try {
// fetch all users
const users = await prisma.user.findMany({
where: {
isActive: true,
},
select: {
id: true,
lastseen: true,
memberOfOrgs: {
select: {
networks: true,
},
},
},
});
// if no users return
if (users.length === 0) return;
// Get all users that have been inactive for 5 minutes
const now = new Date();
const fiveMinutesAgo = new Date(now.getTime() - 5 * 60000);
const inactiveUsers = users.filter((user) => {
return user?.lastseen && new Date(user.lastseen) < fiveMinutesAgo;
});
// keep track of processed networks
const processedNetworks = new Set();
// fetch all networks for each user
for (const user of inactiveUsers) {
const networks = await prisma.network.findMany({
where: {
authorId: user.id,
},
select: {
nwid: true,
},
});
// include get organization networks
const organizationNetworks = user.memberOfOrgs?.flatMap((org) =>
org.networks.map((network) => ({
nwid: network.nwid,
})),
);
// merge user and organization networks
const allNetworks = [...networks, ...organizationNetworks];
// if no networks return
if (allNetworks.length === 0) return;
// fetch all members for each network
for (const network of allNetworks) {
if (network && !processedNetworks.has(network.nwid)) {
processedNetworks.add(network.nwid);
const context: FakeContext = {
session: {
user: {
id: user.id,
},
},
};
// get members from the zt controller
const ztControllerResponse = await ztController.local_network_detail(
// @ts-expect-error
context,
network.nwid,
false,
);
if (!ztControllerResponse || !("members" in ztControllerResponse)) return;
await syncMemberPeersAndStatus(
// @ts-expect-error
context,
network?.nwid,
ztControllerResponse.members,
);
}
}
}
} catch (error) {
console.error("cron task updatePeers:", error);
}
},
null,
true,
"America/Los_Angeles",
);
};