backend-manager 3.2.113 → 3.2.114

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backend-manager",
3
- "version": "3.2.113",
3
+ "version": "3.2.114",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -1,31 +1,20 @@
1
- const _ = require('lodash');
1
+ // Libraries
2
2
  const fetch = require('wonderful-fetch');
3
3
 
4
4
  function Module() {
5
5
 
6
6
  }
7
7
 
8
- Module.prototype.init = function (Manager, data) {
8
+ Module.prototype.main = function (assistant, context) {
9
9
  const self = this;
10
- self.Manager = Manager;
11
10
 
12
- self.libraries = Manager.libraries;
13
- self.assistant = Manager.Assistant();
14
-
15
- self.storage = Manager.storage({name: 'usage', temporary: true, clear: false, log: false});
16
-
17
- self.context = data.context;
18
- return self;
19
- }
20
-
21
- Module.prototype.main = function() {
22
- const self = this;
23
- const Manager = self.Manager;
24
- const libraries = self.libraries;
25
- const assistant = self.assistant;
26
- const context = self.context;
11
+ // Shortcuts
12
+ const Manager = assistant.Manager;
13
+ const libraries = Manager.libraries;
27
14
 
28
15
  return new Promise(async function(resolve, reject) {
16
+ self.storage = Manager.storage({name: 'usage', temporary: true, clear: false, log: false});
17
+
29
18
  assistant.log(`cron/daily/reset-usage(): Starting main...`);
30
19
 
31
20
  // Clear local
@@ -40,6 +29,8 @@ Module.prototype.main = function() {
40
29
 
41
30
  Module.prototype.clearLocal = function() {
42
31
  const self = this;
32
+
33
+ // Shortcuts
43
34
  const Manager = self.Manager;
44
35
  const libraries = self.libraries;
45
36
  const assistant = self.assistant;
@@ -127,7 +118,7 @@ Module.prototype.clearFirestore = function() {
127
118
  data.usage[metric].total = data.usage[metric].total || 0;
128
119
  data.usage[metric].last = data.usage[metric].last || {};
129
120
 
130
- // Bail if its 0
121
+ // Yeet if its 0
131
122
  if (data.usage[metric].period <= 0) {
132
123
  continue;
133
124
  }
@@ -8,6 +8,7 @@ function Module() {
8
8
  Module.prototype.init = function (Manager, data) {
9
9
  const self = this;
10
10
 
11
+ // Shortcuts
11
12
  self.Manager = Manager;
12
13
  self.libraries = Manager.libraries;
13
14
  self.assistant = Manager.Assistant();
@@ -19,42 +20,88 @@ Module.prototype.init = function (Manager, data) {
19
20
 
20
21
  Module.prototype.main = function() {
21
22
  const self = this;
23
+
24
+ // Shortcuts
22
25
  const Manager = self.Manager;
23
26
  const libraries = self.libraries;
24
27
  const assistant = self.assistant;
25
28
  const context = self.context;
26
29
 
27
30
  return new Promise(async function(resolve, reject) {
28
- assistant.log(`cron/daily(): Starting...`);
31
+ // Set log prefix
32
+ assistant.setLogPrefix('cron/daily()');
33
+
34
+ // Log
35
+ assistant.log(`Starting...`);
36
+
37
+ // Setup error
38
+ let error;
39
+
40
+ // Load BEM jobs
41
+ await loadAndExecuteJobs(`${__dirname}/daily`, Manager, context).catch((e) => error = e);
42
+
43
+ // Load custom jobs
44
+ await loadAndExecuteJobs(`${process.cwd()}/hooks/cron/daily`, Manager, context).catch((e) => error = e);
45
+
46
+ // If there was an error, reject
47
+ if (error) {
48
+ return reject(error);
49
+ }
50
+
51
+ // Return
52
+ return resolve();
53
+ });
54
+ }
29
55
 
30
- const jobsPath = `${__dirname}/daily`;
31
- const jobs = jetpack.list(jobsPath);
56
+ function loadAndExecuteJobs(jobsPath, Manager, context) {
57
+ const assistant = Manager.assistant;
58
+
59
+ return new Promise(async function(resolve, reject) {
60
+ const jobs = jetpack.list(jobsPath) || [];
32
61
  let caught;
33
62
 
34
- // For of loop for jobs, load the job, execute it, and log the result
63
+ // Log
64
+ assistant.log(`Located ${jobs.length} jobs @ ${jobsPath}...`);
65
+
35
66
  for (let i = 0; i < jobs.length; i++) {
67
+ // Create new assistant for each job
68
+ const assistant = Manager.Assistant();
69
+
70
+ // Load job
36
71
  const job = jobs[i];
37
72
  const jobName = job.replace('.js', '');
38
73
 
74
+ // Set log prefix
75
+ assistant.setLogPrefix(`cron/daily/${jobName}()`);
76
+
39
77
  // Log
40
- assistant.log(`cron/daily(): Job ${jobName} starting...`);
78
+ assistant.log(`Starting...`);
41
79
 
42
- // Load the job
80
+ // Load job
43
81
  const Job = require(`${jobsPath}/${job}`);
44
82
  const jobInstance = new Job();
45
- jobInstance.init(Manager, { context: context, });
46
-
47
- // Execute the job
48
- await jobInstance.main()
49
- .then(res => {
50
- assistant.log(`cron/daily(): Job ${jobName} completed...`);
51
- })
52
- .catch(e => {
53
- assistant.errorify(`Error executing ${jobName}: ${e}`, {code: 500, sentry: true});
54
- caught = e;
55
- })
83
+
84
+ // Setup
85
+ jobInstance.Manager = Manager;
86
+ jobInstance.assistant = assistant;
87
+ jobInstance.context = context;
88
+ jobInstance.libraries = Manager.libraries;
89
+
90
+ // Execute job
91
+ await jobInstance.main(assistant, context)
92
+ .then(res => {
93
+ assistant.log(`Completed!`);
94
+ })
95
+ .catch(e => {
96
+ assistant.errorify(`Error executing: ${e}`, {
97
+ code: 500,
98
+ sentry: true
99
+ });
100
+ caught = e;
101
+ })
56
102
  }
57
103
 
104
+ // If there was an error, reject
58
105
  if (caught) {
59
106
  return reject(caught);
60
107
  }
@@ -81,6 +81,9 @@ BackendAssistant.prototype.init = function (ref, options) {
81
81
  // Set tag
82
82
  self.tag = `${self.meta.name}/${self.id}`;
83
83
 
84
+ // Set logger prefix
85
+ self.logPrefix = '';
86
+
84
87
  // Set stuff about request
85
88
  self.request = {};
86
89
  self.request.referrer = (self.ref.req.headers || {}).referrer || (self.ref.req.headers || {}).referer || '';
@@ -255,8 +258,15 @@ BackendAssistant.prototype._log = function () {
255
258
  // 1. Convert args to a normal array
256
259
  const logs = [...Array.prototype.slice.call(arguments)];
257
260
 
261
+ // Add log prefix
262
+ const prefix = self.logPrefix
263
+ ? ` ${self.logPrefix}:`
264
+ : ':';
265
+
258
266
  // 2. Prepend log prefix log string
259
- logs.unshift(`[${self.tag} @ ${new Date().toISOString()}]:`);
267
+ logs.unshift(
268
+ `[${self.tag} @ ${new Date().toISOString()}]${prefix}`
269
+ );
260
270
 
261
271
  // 3. Pass along arguments to console.log
262
272
  if (logs[1] === 'error') {
@@ -309,6 +319,30 @@ BackendAssistant.prototype._log = function () {
309
319
  }
310
320
  }
311
321
 
322
+ BackendAssistant.prototype.setLogPrefix = function (s) {
323
+ const self = this;
324
+
325
+ // Set logger prefix
326
+ self.logPrefix = s
327
+
328
+ return self;
329
+ };
330
+
331
+ BackendAssistant.prototype.clearLogPrefix = function () {
332
+ const self = this;
333
+
334
+ // Set logger prefix
335
+ self.logPrefix = '';
336
+
337
+ return self;
338
+ };
339
+
340
+ BackendAssistant.prototype.getLogPrefix = function () {
341
+ const self = this;
342
+
343
+ return self.logPrefix;
344
+ };
345
+
312
346
  BackendAssistant.prototype.getUser = function () {
313
347
  const self = this;
314
348
 
@@ -826,7 +826,7 @@ Manager.prototype.setupFunctions = function (exporter, options) {
826
826
  // Setup cron jobs
827
827
  exporter.bm_cronDaily =
828
828
  self.libraries.functions
829
- .runWith({ memory: '256MB', timeoutSeconds: 120 })
829
+ .runWith({ memory: '256MB', timeoutSeconds: 190 })
830
830
  .pubsub.schedule('every 24 hours')
831
831
  .onRun(async (context) => self._process((new (require(`${core}/cron/daily.js`))()).init(self, { context: context, })));
832
832
  };