Openstatus www.openstatus.dev

Add await (#1184)

* fix workflow

* fix workflow

* ci: apply automated fixes

* fix workflow

* ci: apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

authored by

Thibault Le Ouay
autofix-ci[bot]
and committed by
GitHub
60b2bd59 7884aea9

+61 -36
+60 -35
apps/workflows/src/cron/monitor.ts
··· 123 123 // Let's merge both results 124 124 const users = [...u, ...u1]; 125 125 // iterate over users 126 + 127 + const allResult = []; 128 + 126 129 for (const user of users) { 127 - console.log(`Starting workflow for ${user.userId}`); 128 - // Let's check if the user is in the workflow 129 - const isMember = await redis.sismember("workflow:users", user.userId); 130 - if (isMember) { 131 - continue; 132 - } 133 - // check if user has some running monitors 134 - const nbRunningMonitor = await db.$count( 135 - schema.monitor, 136 - and( 137 - eq(schema.monitor.workspaceId, user.workspaceId), 138 - eq(schema.monitor.active, true), 139 - isNull(schema.monitor.deletedAt), 140 - ), 141 - ); 142 - if (nbRunningMonitor > 0) { 143 - continue; 144 - } 145 - await CreateTask({ 146 - parent, 147 - client: client, 148 - step: "14days", 149 - userId: user.userId, 150 - initialRun: new Date().getTime(), 151 - }); 152 - // // Add our user to the list of users that have started the workflow 130 + const workflow = workflowInit({ user }); 131 + allResult.push(workflow); 132 + } 153 133 154 - await redis.sadd("workflow:users", user.userId); 155 - console.log(`user workflow started for ${user.userId}`); 134 + const allRequests = await Promise.allSettled(allResult); 135 + 136 + const success = allRequests.filter((r) => r.status === "fulfilled").length; 137 + const failed = allRequests.filter((r) => r.status === "rejected").length; 138 + 139 + console.log( 140 + `End cron with ${allResult.length} jobs with ${success} success and ${failed} failed`, 141 + ); 142 + } 143 + 144 + async function workflowInit({ 145 + user, 146 + }: { 147 + user: { 148 + userId: number; 149 + email: string | null; 150 + workspaceId: number; 151 + }; 152 + }) { 153 + console.log(`Starting workflow for ${user.userId}`); 154 + // Let's check if the user is in the workflow 155 + const isMember = await redis.sismember("workflow:users", user.userId); 156 + if (isMember) { 157 + return; 156 158 } 159 + // check if user has some running monitors 160 + const nbRunningMonitor = await db.$count( 161 + schema.monitor, 162 + and( 163 + eq(schema.monitor.workspaceId, user.workspaceId), 164 + eq(schema.monitor.active, true), 165 + isNull(schema.monitor.deletedAt), 166 + ), 167 + ); 168 + if (nbRunningMonitor > 0) { 169 + return; 170 + } 171 + await CreateTask({ 172 + parent, 173 + client: client, 174 + step: "14days", 175 + userId: user.userId, 176 + initialRun: new Date().getTime(), 177 + }); 178 + // // Add our user to the list of users that have started the workflow 179 + 180 + await redis.sadd("workflow:users", user.userId); 181 + console.log(`user workflow started for ${user.userId}`); 157 182 } 158 183 159 184 export async function Step14Days(userId: number) { ··· 165 190 // TODO: Send email 166 191 167 192 if (user.email) { 168 - sendWithRender({ 193 + await sendWithRender({ 169 194 to: [user.email], 170 195 subject: "Your OpenStatus monitors will be paused soon", 171 196 from: "Thibault From OpenStatus <thibault@notifications.openstatus.dev>", 172 - replyTo: "thibault@openstatus.dev", 197 + reply_to: "thibault@openstatus.dev", 173 198 174 199 react: MonitorDeactivationEmail({ 175 200 lastLogin: new Date(), ··· 195 220 const user = await getUser(userId); 196 221 197 222 if (user.email) { 198 - sendWithRender({ 223 + await sendWithRender({ 199 224 to: [user.email], 200 225 subject: "Your OpenStatus monitors will be paused in 3 days", 201 226 from: "Thibault From OpenStatus <thibault@notifications.openstatus.dev>", 202 - replyTo: "thibault@openstatus.dev", 227 + reply_to: "thibault@openstatus.dev", 203 228 204 229 react: MonitorDeactivationEmail({ 205 230 lastLogin: new Date(workFlowRunTimestamp), ··· 267 292 // Remove user for workflow 268 293 269 294 if (currentUser.email) { 270 - sendWithRender({ 295 + await sendWithRender({ 271 296 to: [currentUser.email], 272 297 subject: "Your monitors have been paused", 273 298 from: "Thibault From OpenStatus <thibault@notifications.openstatus.dev>", 274 - replyTo: "thibault@openstatus.dev", 299 + reply_to: "thibault@openstatus.dev", 275 300 react: MonitorPausedEmail(), 276 301 }); 277 302 } ··· 320 345 httpRequest: { 321 346 headers: { 322 347 "Content-Type": "application/json", // Set content type to ensure compatibility your application's request parsing 323 - Authorization: `Basic ${env().CRON_SECRET}`, 348 + Authorization: `${env().CRON_SECRET}`, 324 349 }, 325 350 httpMethod: "GET", 326 351 url,
+1 -1
packages/emails/src/send.ts
··· 11 11 subject: string; 12 12 to: string[]; 13 13 from: string; 14 - replyTo?: string; 14 + reply_to?: string; 15 15 } 16 16 17 17 export type EmailHtml = {