Repository navigation
Expand file tree
/
Copy pathlambda.js
More file actions
129 lines (111 loc) · 4.02 KB
/
Copy pathlambda.js
File metadata and controls
129 lines (111 loc) · 4.02 KB
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
import { configure } from "@codegenie/serverless-express";
import { init } from "./dist/App.js";
import { TranscodeHelper } from "./dist/helpers/TranscodeHelper.js";
import { Environment } from "./dist/helpers/Environment.js";
import { ZipHelper } from "./dist/helpers/ZipHelper.js";
import { GeoHelper } from "./dist/helpers/GeoHelper.js";
import { MigrationRunner } from "./dist/helpers/MigrationRunner.js";
let serverlessExpress;
const checkInit = async () => {
if (!Environment.connectionString) {
await Environment.init(process.env.APP_ENV);
}
};
export const universal = async (event, context) => {
try {
await checkInit();
if (!serverlessExpress) {
const app = await init();
serverlessExpress = configure({
app,
binarySettings: { contentTypes: ["application/octet-stream", "font/*", "image/*", "application/pdf"] },
stripBasePath: false,
resolutionMode: "PROMISE"
});
}
return serverlessExpress(event, context);
} catch (error) {
return {
statusCode: 500,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type,Authorization",
"Access-Control-Allow-Methods": "GET,POST,PUT,DELETE,OPTIONS"
},
body: JSON.stringify({
error: "Internal server error",
message: error.message,
timestamp: new Date().toISOString()
})
};
}
};
export const mauticSync = async (event, _context) => {
await checkInit();
const { MauticHelper } = await import("./dist/helpers/MauticHelper.js");
await MauticHelper.logLessonDownload(event.churchId, event.lessonId, event.au);
};
export const videoPingback = async (event, _context) => {
await checkInit();
await TranscodeHelper.handlePingback(event);
};
export const zipBundles = async (event, context) => {
const startTime = Date.now();
console.debug("Lambda zipBundles invoked", {
requestId: context.awsRequestId,
timestamp: new Date().toISOString()
});
try {
await checkInit();
const { Repositories } = await import("./dist/repositories/Repositories.js");
const initialPending = await Repositories.getCurrent().bundle.loadPendingUpdate(50);
console.debug(`Initial queue depth: ${initialPending.length}`);
await ZipHelper.zipPendingBundles();
await GeoHelper.lookupMissing();
const finalPending = await Repositories.getCurrent().bundle.loadPendingUpdate(50);
const processingTime = Date.now() - startTime;
const metrics = {
initialQueueDepth: initialPending.length,
finalQueueDepth: finalPending.length,
processed: initialPending.length - finalPending.length,
processingTimeMs: processingTime,
requestId: context.awsRequestId,
timestamp: new Date().toISOString()
};
(metrics.initialQueueDepth > 0 ? console.info : console.debug)("Lambda zipBundles completed successfully", metrics);
return {
statusCode: 200,
body: JSON.stringify({
success: true,
...metrics
})
};
} catch (error) {
const processingTime = Date.now() - startTime;
const errorDetails = {
error: error.message,
stack: error.stack,
processingTimeMs: processingTime,
requestId: context.awsRequestId,
timestamp: new Date().toISOString()
};
console.error("Lambda zipBundles failed", errorDetails);
return {
statusCode: 500,
body: JSON.stringify({
success: false,
...errorDetails
})
};
}
};
// Applies pending database migrations. The deploy workflows invoke this after `serverless deploy`;
// the prod database is VPC-only, so the GitHub runner can't run `yarn migrate` itself.
export const migrate = async () => {
await checkInit();
const result = await MigrationRunner.run();
if (result.applied.length) console.log(`[migrate] applied ${result.applied.join(", ")}`);
else if (result.ok) console.log("[migrate] up to date");
if (result.error) console.error(`[migrate] ${result.failed ? `${result.failed} failed - ` : ""}${result.error}`);
return result;
};