@resolveio/server-lib 20.12.43 → 20.12.44

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/fixtures/init.js CHANGED
@@ -52,6 +52,7 @@ var email_history_collection_1 = require("../collections/email-history.collectio
52
52
  var flag_collection_1 = require("../collections/flag.collection");
53
53
  var report_builder_report_collection_1 = require("../collections/report-builder-report.collection");
54
54
  var resolveio_server_app_1 = require("../resolveio-server-app");
55
+ var chrome_cleaner_1 = require("../util/chrome-cleaner");
55
56
  function loadServerInit() {
56
57
  return __awaiter(this, void 0, void 0, function () {
57
58
  var enableDebugFlag, enableDebugFlagPDF;
@@ -82,8 +83,14 @@ function loadServerInit() {
82
83
  case 8:
83
84
  _a.sent();
84
85
  _a.label = 9;
85
- case 9: return [4 /*yield*/, backfillReportBuilderLayouts()];
86
+ case 9:
87
+ if (!(resolveio_server_app_1.ResolveIOServer.getServerConfig()['ROOT_URL'] !== 'http://localhost:4200')) return [3 /*break*/, 11];
88
+ return [4 /*yield*/, (0, chrome_cleaner_1.cleanupExistingChromeProcesses)('server-init')];
86
89
  case 10:
90
+ _a.sent();
91
+ _a.label = 11;
92
+ case 11: return [4 /*yield*/, backfillReportBuilderLayouts()];
93
+ case 12:
87
94
  _a.sent();
88
95
  return [2 /*return*/];
89
96
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/fixtures/init.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAKA,wCAmBC;AAxBD,oFAAyE;AACzE,kEAAuD;AACvD,oGAAuF;AACvF,gEAA0D;AAE1D,SAAsB,cAAc;;;;;wBACnC,qBAAM,yCAAc,CAAC,UAAU,CAAC,EAAC,MAAM,EAAE,YAAY,EAAC,EAAE,EAAC,IAAI,EAAE,EAAC,MAAM,EAAE,SAAS,EAAC,EAAC,CAAC,EAAA;;oBAApF,SAAoF,CAAC;oBAE/D,qBAAM,uBAA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M,KAAK,GAAG,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;QACxC,IAAI,KAAK,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;YACpC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAClB,CAAC;aACI,IAAI,IAAI,CAAC,UAAU,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;YAC7D,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QAC5B,CAAC;IACF,CAAC,CAAC,CAAC;IAEH,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;AACtC,CAAC","file":"init.js","sourcesContent":["import { EmailHistories } from '../collections/email-history.collection';\nimport { Flags } from '../collections/flag.collection';\nimport { ReportBuilderReports } from '../collections/report-builder-report.collection';\nimport { ResolveIOServer } from '../resolveio-server-app';\n\nexport async function loadServerInit() {\n\tawait EmailHistories.updateMany({status: 'processing'}, {$set: {status: 'pending'}});\n\n\tlet enableDebugFlag = await Flags.findOne({type: 'Enable Debug'});\n\n\tif (!enableDebugFlag) {\n\t\tawait Flags.insertOne({type: 'Enable Debug', value: false});\n\t}\n\n\tlet enableDebugFlagPDF = await Flags.findOne({type: 'Enable Debug PDF'});\n\n\tif (!enableDebugFlagPDF) {\n\t\tawait Flags.insertOne({type: 'Enable Debug PDF', value: false});\n\t}\n\telse if (enableDebugFlagPDF.value && ResolveIOServer.getServerConfig()['ROOT_URL'] !== 'http://localhost:4200') {\n\t\tawait Flags.updateOne({_id: enableDebugFlagPDF._id}, {$set: {value: false}});\n\t}\n\n\tawait backfillReportBuilderLayouts();\n}\n\nasync function backfillReportBuilderLayouts() {\n\tif (!ReportBuilderReports) {\n\t\treturn;\n\t}\n\n\tconst reports = await ReportBuilderReports.find({\n\t\t$or: [\n\t\t\t{fields_layout: {$exists: false}},\n\t\t\t{fields_layout: {$size: 0}}\n\t\t]\n\t});\n\n\tconst ops = [];\n\n\tfor (const report of reports) {\n\t\tconst fieldsSelected = report.fields_selected || [];\n\t\tconst collectionKeys = buildCollectionKeys(report);\n\n\t\tconst layout = fieldsSelected.map((field, index) => {\n\t\t\tconst header = field.columnName || field.fieldName || `Column ${index + 1}`;\n\n\t\t\tconst mappings = collectionKeys.map(key => {\n\t\t\t\tconst matchesKey = key === report.collection_root\n\t\t\t\t\t? (field.collection_name === report.collection_root)\n\t\t\t\t\t: (key === (field.lookup_as || '').trim()) || key === field.collection_name;\n\n\t\t\t\tif (matchesKey) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcollection: key,\n\t\t\t\t\t\tmode: 'variable',\n\t\t\t\t\t\tfieldPath: field.fieldPath || '',\n\t\t\t\t\t\tfieldId: field.id || `f_layout_${index}`,\n\t\t\t\t\t\ttext: '',\n\t\t\t\t\t\tleafValueType: field.leafValueType || 'Value',\n\t\t\t\t\t\tleafFormatType: field.leafFormatType || ''\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\treturn {\n\t\t\t\t\tcollection: key,\n\t\t\t\t\tmode: 'text',\n\t\t\t\t\tfieldPath: '',\n\t\t\t\t\tfieldId: '',\n\t\t\t\t\ttext: '',\n\t\t\t\t\tleafValueType: 'Value',\n\t\t\t\t\tleafFormatType: ''\n\t\t\t\t};\n\t\t\t});\n\n\t\t\treturn {\n\t\t\t\tid: field.id ? `layout_${field.id}` : `layout_${index}`,\n\t\t\t\theader,\n\t\t\t\tmappings\n\t\t\t};\n\t\t});\n\n\t\tops.push({\n\t\t\tupdateOne: {\n\t\t\t\tfilter: {_id: report._id},\n\t\t\t\tupdate: {\n\t\t\t\t\t$set: {\n\t\t\t\t\t\tcollection_joins: report.collection_joins || [],\n\t\t\t\t\t\tfields_layout: layout\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\t}\n\n\tconsole.log(`Backfilled ${ops.length} report layouts`);\n\n\tif (ops.length) {\n\t\tawait ReportBuilderReports.bulkWrite(ops, {ordered: false}, false, true, true, true);\n\t}\n}\n\nfunction buildCollectionKeys(report: any): string[] {\n\tconst keys: string[] = [];\n\n\tif (report.collection_root) {\n\t\tkeys.push(report.collection_root);\n\t}\n\n\t(report.collection_joins || []).forEach(join => {\n\t\tif (!join) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst alias = (join.alias || '').trim();\n\t\tif (alias && !keys.includes(alias)) {\n\t\t\tkeys.push(alias);\n\t\t}\n\t\telse if (join.collection && !keys.includes(join.collection)) {\n\t\t\tkeys.push(join.collection);\n\t\t}\n\t});\n\n\treturn keys.length ? keys : ['root'];\n}\n"]}
1
+ {"version":3,"sources":["../../src/fixtures/init.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,wCAwBC;AA9BD,oFAAyE;AACzE,kEAAuD;AACvD,oGAAuF;AACvF,gEAA0D;AAC1D,yDAAwE;AAExE,SAAsB,cAAc;;;;;wBACnC,qBAAM,yCAAc,CAAC,UAAU,CAAC,EAAC,MAAM,EAAE,YAAY,EAAC,EAAE,EAAC,IAAI,EAAE,EAAC,MAAM,EAAE,SAAS,EAAC,EAAC,CAAC,EAAA;;oBAApF,SAAoF,CAAC;oBAE/D,qBAAM,uBAAK,CAAC,OAAO,CAAC,EAAC,IAAI,EAAE,cAAc,EAAC,CAAC,EAAA;;oBAA7D,eAAe,GAAG,SAA2C;yBAE7D,CAAC,eAAe,EAAhB,wBAAgB;oBACnB,qBAAM,uBAAK,CAAC,SAAS,CAAC,EAAC,IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,KAAK,EAAC,CAAC,EAAA;;oBAA3D,SAA2D,CAAC;;wBAGpC,qBAAM,uBAAK,CAAC,OAAO,CAAC,EAAC,IAAI,EAAE,kBAAkB,EAAC,CAAC,EAAA;;oBA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AC,IAAI,CAAC,KAAK,CAAC,CAAC;QAClB,CAAC;aACI,IAAI,IAAI,CAAC,UAAU,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;YAC7D,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QAC5B,CAAC;IACF,CAAC,CAAC,CAAC;IAEH,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;AACtC,CAAC","file":"init.js","sourcesContent":["import { EmailHistories } from '../collections/email-history.collection';\nimport { Flags } from '../collections/flag.collection';\nimport { ReportBuilderReports } from '../collections/report-builder-report.collection';\nimport { ResolveIOServer } from '../resolveio-server-app';\nimport { cleanupExistingChromeProcesses } from '../util/chrome-cleaner';\n\nexport async function loadServerInit() {\n\tawait EmailHistories.updateMany({status: 'processing'}, {$set: {status: 'pending'}});\n\n\tlet enableDebugFlag = await Flags.findOne({type: 'Enable Debug'});\n\n\tif (!enableDebugFlag) {\n\t\tawait Flags.insertOne({type: 'Enable Debug', value: false});\n\t}\n\n\tlet enableDebugFlagPDF = await Flags.findOne({type: 'Enable Debug PDF'});\n\n\tif (!enableDebugFlagPDF) {\n\t\tawait Flags.insertOne({type: 'Enable Debug PDF', value: false});\n\t}\n\telse if (enableDebugFlagPDF.value && ResolveIOServer.getServerConfig()['ROOT_URL'] !== 'http://localhost:4200') {\n\t\tawait Flags.updateOne({_id: enableDebugFlagPDF._id}, {$set: {value: false}});\n\t}\n\n\t// Clean up any orphaned headless Chrome processes left from prior runs (production only).\n\tif (ResolveIOServer.getServerConfig()['ROOT_URL'] !== 'http://localhost:4200') {\n\t\tawait cleanupExistingChromeProcesses('server-init');\n\t}\n\n\tawait backfillReportBuilderLayouts();\n}\n\nasync function backfillReportBuilderLayouts() {\n\tif (!ReportBuilderReports) {\n\t\treturn;\n\t}\n\n\tconst reports = await ReportBuilderReports.find({\n\t\t$or: [\n\t\t\t{fields_layout: {$exists: false}},\n\t\t\t{fields_layout: {$size: 0}}\n\t\t]\n\t});\n\n\tconst ops = [];\n\n\tfor (const report of reports) {\n\t\tconst fieldsSelected = report.fields_selected || [];\n\t\tconst collectionKeys = buildCollectionKeys(report);\n\n\t\tconst layout = fieldsSelected.map((field, index) => {\n\t\t\tconst header = field.columnName || field.fieldName || `Column ${index + 1}`;\n\n\t\t\tconst mappings = collectionKeys.map(key => {\n\t\t\t\tconst matchesKey = key === report.collection_root\n\t\t\t\t\t? (field.collection_name === report.collection_root)\n\t\t\t\t\t: (key === (field.lookup_as || '').trim()) || key === field.collection_name;\n\n\t\t\t\tif (matchesKey) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcollection: key,\n\t\t\t\t\t\tmode: 'variable',\n\t\t\t\t\t\tfieldPath: field.fieldPath || '',\n\t\t\t\t\t\tfieldId: field.id || `f_layout_${index}`,\n\t\t\t\t\t\ttext: '',\n\t\t\t\t\t\tleafValueType: field.leafValueType || 'Value',\n\t\t\t\t\t\tleafFormatType: field.leafFormatType || ''\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\treturn {\n\t\t\t\t\tcollection: key,\n\t\t\t\t\tmode: 'text',\n\t\t\t\t\tfieldPath: '',\n\t\t\t\t\tfieldId: '',\n\t\t\t\t\ttext: '',\n\t\t\t\t\tleafValueType: 'Value',\n\t\t\t\t\tleafFormatType: ''\n\t\t\t\t};\n\t\t\t});\n\n\t\t\treturn {\n\t\t\t\tid: field.id ? `layout_${field.id}` : `layout_${index}`,\n\t\t\t\theader,\n\t\t\t\tmappings\n\t\t\t};\n\t\t});\n\n\t\tops.push({\n\t\t\tupdateOne: {\n\t\t\t\tfilter: {_id: report._id},\n\t\t\t\tupdate: {\n\t\t\t\t\t$set: {\n\t\t\t\t\t\tcollection_joins: report.collection_joins || [],\n\t\t\t\t\t\tfields_layout: layout\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\t}\n\n\tconsole.log(`Backfilled ${ops.length} report layouts`);\n\n\tif (ops.length) {\n\t\tawait ReportBuilderReports.bulkWrite(ops, {ordered: false}, false, true, true, true);\n\t}\n}\n\nfunction buildCollectionKeys(report: any): string[] {\n\tconst keys: string[] = [];\n\n\tif (report.collection_root) {\n\t\tkeys.push(report.collection_root);\n\t}\n\n\t(report.collection_joins || []).forEach(join => {\n\t\tif (!join) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst alias = (join.alias || '').trim();\n\t\tif (alias && !keys.includes(alias)) {\n\t\t\tkeys.push(alias);\n\t\t}\n\t\telse if (join.collection && !keys.includes(join.collection)) {\n\t\t\tkeys.push(join.collection);\n\t\t}\n\t});\n\n\treturn keys.length ? keys : ['root'];\n}\n"]}
package/methods/pdf.js CHANGED
@@ -81,6 +81,7 @@ var puppeteer = require("puppeteer");
81
81
  var simpl_schema_1 = require("simpl-schema");
82
82
  var flag_collection_1 = require("../collections/flag.collection");
83
83
  var common_1 = require("../util/common");
84
+ var chrome_cleaner_1 = require("../util/chrome-cleaner");
84
85
  var PUPPETEER_ARGS = [
85
86
  '--disable-dev-shm-usage',
86
87
  '--no-sandbox',
@@ -159,7 +160,7 @@ function applyLowResourceLaunchTweaks(options) {
159
160
  });
160
161
  return __assign(__assign({}, options), { args: mergedArgs, ignoreDefaultArgs: undefined, env: sanitizeLaunchEnv(options.env) });
161
162
  }
162
- function safeCloseBrowser(browser) {
163
+ function safeCloseBrowser(browser, browserPid, meta) {
163
164
  return __awaiter(this, void 0, void 0, function () {
164
165
  var proc, closeTimedOut, closeTimeout, errClose_1;
165
166
  return __generator(this, function (_a) {
@@ -175,6 +176,9 @@ function safeCloseBrowser(browser) {
175
176
  catch (_b) {
176
177
  proc = null;
177
178
  }
179
+ if (!browserPid && proc && proc.pid) {
180
+ browserPid = proc.pid;
181
+ }
178
182
  closeTimedOut = false;
179
183
  closeTimeout = setTimeout(function () {
180
184
  closeTimedOut = true;
@@ -188,17 +192,19 @@ function safeCloseBrowser(browser) {
188
192
  return [3 /*break*/, 5];
189
193
  case 3:
190
194
  errClose_1 = _a.sent();
191
- console.error(new Date(), 'Puppeteer close failed, forcing kill', errClose_1 && errClose_1.message ? errClose_1.message : errClose_1);
195
+ console.error(new Date(), 'Puppeteer close failed, forcing kill', { jobId: meta && meta.jobId, attempt: meta && meta.attempt, pid: browserPid }, errClose_1 && errClose_1.message ? errClose_1.message : errClose_1);
192
196
  return [3 /*break*/, 5];
193
197
  case 4:
194
198
  clearTimeout(closeTimeout);
195
- if ((closeTimedOut || (proc && !proc.killed)) && proc) {
196
- try {
197
- proc.kill('SIGKILL');
198
- }
199
- catch (errKill) {
200
- console.error(new Date(), 'Failed to kill Puppeteer process', errKill);
201
- }
199
+ if (browserPid) {
200
+ (0, chrome_cleaner_1.killChromePid)(browserPid, closeTimedOut ? 'browser-close-timeout' : 'browser-close');
201
+ console.log(new Date(), 'Puppeteer browser closed', {
202
+ jobId: meta && meta.jobId,
203
+ attempt: meta && meta.attempt,
204
+ pid: browserPid,
205
+ closeTimedOut: closeTimedOut,
206
+ durationMs: meta && meta.startedAt ? Date.now() - meta.startedAt : undefined
207
+ });
202
208
  }
203
209
  return [7 /*endfinally*/];
204
210
  case 5: return [2 /*return*/];
@@ -208,7 +214,7 @@ function safeCloseBrowser(browser) {
208
214
  }
209
215
  function launchPDFBrowser(headless, devtools) {
210
216
  return __awaiter(this, void 0, void 0, function () {
211
- var envExecutablePath, launchOptions, errLaunch_1, fallbackExecutablePath, retryOptions;
217
+ var envExecutablePath, launchOptions, browser, proc, pid, errLaunch_1, fallbackExecutablePath, retryOptions, browser, proc, pid;
212
218
  return __generator(this, function (_a) {
213
219
  switch (_a.label) {
214
220
  case 0:
@@ -230,7 +236,12 @@ function launchPDFBrowser(headless, devtools) {
230
236
  case 1:
231
237
  _a.trys.push([1, 3, , 5]);
232
238
  return [4 /*yield*/, puppeteer.launch(launchOptions)];
233
- case 2: return [2 /*return*/, _a.sent()];
239
+ case 2:
240
+ browser = _a.sent();
241
+ proc = typeof browser.process === 'function' ? browser.process() : null;
242
+ pid = proc && proc.pid ? proc.pid : undefined;
243
+ (0, chrome_cleaner_1.trackChromePid)(pid);
244
+ return [2 /*return*/, { browser: browser, pid: pid }];
234
245
  case 3:
235
246
  errLaunch_1 = _a.sent();
236
247
  if (!shouldRetryBrowserLaunch(errLaunch_1)) {
@@ -243,7 +254,12 @@ function launchPDFBrowser(headless, devtools) {
243
254
  }
244
255
  console.warn(new Date(), 'Retrying Puppeteer launch with low-resource args');
245
256
  return [4 /*yield*/, puppeteer.launch(retryOptions)];
246
- case 4: return [2 /*return*/, _a.sent()];
257
+ case 4:
258
+ browser = _a.sent();
259
+ proc = typeof browser.process === 'function' ? browser.process() : null;
260
+ pid = proc && proc.pid ? proc.pid : undefined;
261
+ (0, chrome_cleaner_1.trackChromePid)(pid);
262
+ return [2 /*return*/, { browser: browser, pid: pid }];
247
263
  case 5: return [2 /*return*/];
248
264
  }
249
265
  });
@@ -283,7 +299,7 @@ function loadPDFMethods(methodManager) {
283
299
  }),
284
300
  function: function (html_1, fileName_1) {
285
301
  return __awaiter(this, arguments, void 0, function (html, fileName, orientation, fontSize, upload, uploadFileOrder, uploadFileType) {
286
- var enableDebugFlag, enableDebug, maxAttempts, lastError, attempt, browser, page, pathToFont, dejavuserif_woff, dejavuserifBold_woff, dejavuserifItalic_woff, dejavuserifBoldItalic_woff, content, tmpFileName, data, fileData, err_1;
302
+ var enableDebugFlag, enableDebug, maxAttempts, lastError, jobId, attempt, browser, browserPid, attemptStarted, launchResult, page, pathToFont, dejavuserif_woff, dejavuserifBold_woff, dejavuserifItalic_woff, dejavuserifBoldItalic_woff, content, tmpFileName, data, fileData, err_1;
287
303
  if (orientation === void 0) { orientation = 'portrait'; }
288
304
  if (fontSize === void 0) { fontSize = '12px'; }
289
305
  if (upload === void 0) { upload = false; }
@@ -297,17 +313,24 @@ function loadPDFMethods(methodManager) {
297
313
  enableDebug = !!(enableDebugFlag && enableDebugFlag.value);
298
314
  maxAttempts = 2;
299
315
  lastError = null;
316
+ jobId = (0, common_1.objectIdHexString)();
300
317
  attempt = 1;
301
318
  _a.label = 2;
302
319
  case 2:
303
- if (!(attempt <= maxAttempts)) return [3 /*break*/, 19];
320
+ if (!(attempt <= maxAttempts)) return [3 /*break*/, 20];
304
321
  browser = null;
322
+ browserPid = null;
323
+ attemptStarted = Date.now();
305
324
  _a.label = 3;
306
325
  case 3:
307
- _a.trys.push([3, 13, 16, 18]);
326
+ _a.trys.push([3, 14, 17, 19]);
327
+ console.log(new Date(), 'PDF launch start', { jobId: jobId, attempt: attempt, headless: !enableDebug });
308
328
  return [4 /*yield*/, launchPDFBrowser(!enableDebug, enableDebug)];
309
329
  case 4:
310
- browser = _a.sent();
330
+ launchResult = _a.sent();
331
+ browser = launchResult.browser;
332
+ browserPid = launchResult.pid;
333
+ console.log(new Date(), 'PDF launch success', { jobId: jobId, attempt: attempt, pid: browserPid });
311
334
  return [4 /*yield*/, browser.newPage()];
312
335
  case 5:
313
336
  page = _a.sent();
@@ -354,17 +377,20 @@ function loadPDFMethods(methodManager) {
354
377
  })];
355
378
  case 8:
356
379
  _a.sent();
357
- if (!(enableDebugFlag && enableDebugFlag.value)) return [3 /*break*/, 12];
358
- if (!(orientation === 'landscape')) return [3 /*break*/, 10];
359
- return [4 /*yield*/, page.setViewport({ width: 1100, height: 850 })];
380
+ return [4 /*yield*/, page.close()];
360
381
  case 9:
361
382
  _a.sent();
362
- return [3 /*break*/, 12];
363
- case 10: return [4 /*yield*/, page.setViewport({ width: 850, height: 1100 })];
364
- case 11:
383
+ if (!(enableDebugFlag && enableDebugFlag.value)) return [3 /*break*/, 13];
384
+ if (!(orientation === 'landscape')) return [3 /*break*/, 11];
385
+ return [4 /*yield*/, page.setViewport({ width: 1100, height: 850 })];
386
+ case 10:
365
387
  _a.sent();
366
- _a.label = 12;
388
+ return [3 /*break*/, 13];
389
+ case 11: return [4 /*yield*/, page.setViewport({ width: 850, height: 1100 })];
367
390
  case 12:
391
+ _a.sent();
392
+ _a.label = 13;
393
+ case 13:
368
394
  if (fs.existsSync(path.join(__dirname, (tmpFileName)))) {
369
395
  try {
370
396
  data = fs.readFileSync(path.join(__dirname, (tmpFileName)));
@@ -386,32 +412,34 @@ function loadPDFMethods(methodManager) {
386
412
  console.log('No tmp file ', tmpFileName);
387
413
  throw new Error('Error in Generate PDF: No tmp file');
388
414
  }
389
- return [3 /*break*/, 18];
390
- case 13:
415
+ return [3 /*break*/, 19];
416
+ case 14:
391
417
  err_1 = _a.sent();
392
418
  lastError = err_1;
393
- if (!browser) return [3 /*break*/, 15];
419
+ console.error(new Date(), 'generatePDF attempt error', { jobId: jobId, attempt: attempt, pid: browserPid }, err_1 && err_1.message ? err_1.message : err_1);
420
+ if (!browser) return [3 /*break*/, 16];
394
421
  // eslint-disable-next-line no-restricted-syntax
395
422
  return [4 /*yield*/, browser.close().catch(function () { })];
396
- case 14:
423
+ case 15:
397
424
  // eslint-disable-next-line no-restricted-syntax
398
425
  _a.sent();
399
- _a.label = 15;
400
- case 15:
426
+ _a.label = 16;
427
+ case 16:
401
428
  if (err_1 && err_1.message && err_1.message.includes('Navigating frame was detached') && attempt < maxAttempts) {
402
429
  console.warn(new Date(), 'Retry generatePDF after detached frame', { attempt: attempt });
403
- return [3 /*break*/, 18];
430
+ return [3 /*break*/, 19];
404
431
  }
405
432
  throw err_1;
406
- case 16: return [4 /*yield*/, safeCloseBrowser(browser)];
407
- case 17:
433
+ case 17: return [4 /*yield*/, safeCloseBrowser(browser, browserPid, { jobId: jobId, attempt: attempt, startedAt: attemptStarted })];
434
+ case 18:
408
435
  _a.sent();
409
436
  browser = null;
437
+ browserPid = null;
410
438
  return [7 /*endfinally*/];
411
- case 18:
439
+ case 19:
412
440
  attempt++;
413
441
  return [3 /*break*/, 2];
414
- case 19: throw lastError;
442
+ case 20: throw lastError;
415
443
  }
416
444
  });
417
445
  });
@@ -451,7 +479,7 @@ function loadPDFMethods(methodManager) {
451
479
  }),
452
480
  function: function (html_1, fileName_1) {
453
481
  return __awaiter(this, arguments, void 0, function (html, fileName, orientation, fontSize, upload, uploadFileOrder, uploadFileType) {
454
- var maxAttempts, lastError, attempt, browser, page, pathToFont, dejavuserif_woff, dejavuserifBold_woff, dejavuserifItalic_woff, dejavuserifBoldItalic_woff, content, tmpFileName, data, fileData, err_2;
482
+ var maxAttempts, lastError, jobId, attempt, browser, browserPid, attemptStarted, launchResult, page, pathToFont, dejavuserif_woff, dejavuserifBold_woff, dejavuserifItalic_woff, dejavuserifBoldItalic_woff, content, tmpFileName, data, fileData, err_2;
455
483
  if (orientation === void 0) { orientation = 'portrait'; }
456
484
  if (fontSize === void 0) { fontSize = '12px'; }
457
485
  if (upload === void 0) { upload = false; }
@@ -462,17 +490,24 @@ function loadPDFMethods(methodManager) {
462
490
  case 0:
463
491
  maxAttempts = 2;
464
492
  lastError = null;
493
+ jobId = (0, common_1.objectIdHexString)();
465
494
  attempt = 1;
466
495
  _a.label = 1;
467
496
  case 1:
468
- if (!(attempt <= maxAttempts)) return [3 /*break*/, 14];
497
+ if (!(attempt <= maxAttempts)) return [3 /*break*/, 15];
469
498
  browser = null;
499
+ browserPid = null;
500
+ attemptStarted = Date.now();
470
501
  _a.label = 2;
471
502
  case 2:
472
- _a.trys.push([2, 8, 11, 13]);
503
+ _a.trys.push([2, 9, 12, 14]);
504
+ console.log(new Date(), 'PDF launch start', { jobId: jobId, attempt: attempt, headless: true });
473
505
  return [4 /*yield*/, launchPDFBrowser(true, false)];
474
506
  case 3:
475
- browser = _a.sent();
507
+ launchResult = _a.sent();
508
+ browser = launchResult.browser;
509
+ browserPid = launchResult.pid;
510
+ console.log(new Date(), 'PDF launch success', { jobId: jobId, attempt: attempt, pid: browserPid });
476
511
  return [4 /*yield*/, browser.newPage()];
477
512
  case 4:
478
513
  page = _a.sent();
@@ -518,6 +553,9 @@ function loadPDFMethods(methodManager) {
518
553
  timeout: 0
519
554
  })];
520
555
  case 7:
556
+ _a.sent();
557
+ return [4 /*yield*/, page.close()];
558
+ case 8:
521
559
  _a.sent();
522
560
  if (fs.existsSync(path.join(__dirname, (tmpFileName)))) {
523
561
  try {
@@ -540,32 +578,34 @@ function loadPDFMethods(methodManager) {
540
578
  console.log('No tmp file ', tmpFileName);
541
579
  throw new Error('Error in Generate PDF No Page Number: No tmp file');
542
580
  }
543
- return [3 /*break*/, 13];
544
- case 8:
581
+ return [3 /*break*/, 14];
582
+ case 9:
545
583
  err_2 = _a.sent();
546
584
  lastError = err_2;
547
- if (!browser) return [3 /*break*/, 10];
585
+ console.error(new Date(), 'generatePDFNoPageNumber attempt error', { jobId: jobId, attempt: attempt, pid: browserPid }, err_2 && err_2.message ? err_2.message : err_2);
586
+ if (!browser) return [3 /*break*/, 11];
548
587
  // eslint-disable-next-line no-restricted-syntax
549
588
  return [4 /*yield*/, browser.close().catch(function () { })];
550
- case 9:
589
+ case 10:
551
590
  // eslint-disable-next-line no-restricted-syntax
552
591
  _a.sent();
553
- _a.label = 10;
554
- case 10:
592
+ _a.label = 11;
593
+ case 11:
555
594
  if (err_2 && err_2.message && err_2.message.includes('Navigating frame was detached') && attempt < maxAttempts) {
556
595
  console.warn(new Date(), 'Retry generatePDFNoPageNumber after detached frame', { attempt: attempt });
557
- return [3 /*break*/, 13];
596
+ return [3 /*break*/, 14];
558
597
  }
559
598
  throw err_2;
560
- case 11: return [4 /*yield*/, safeCloseBrowser(browser)];
561
- case 12:
599
+ case 12: return [4 /*yield*/, safeCloseBrowser(browser, browserPid, { jobId: jobId, attempt: attempt, startedAt: attemptStarted })];
600
+ case 13:
562
601
  _a.sent();
563
602
  browser = null;
603
+ browserPid = null;
564
604
  return [7 /*endfinally*/];
565
- case 13:
605
+ case 14:
566
606
  attempt++;
567
607
  return [3 /*break*/, 1];
568
- case 14: throw lastError;
608
+ case 15: throw lastError;
569
609
  }
570
610
  });
571
611
  });