pangea-server 3.3.0 → 3.3.2

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.
@@ -1,15 +1,28 @@
1
1
  import { Db } from '../database';
2
- type TaskFn = (db: Db) => Promise<void>;
3
- type Timezone = 'UTC' | 'America/New_York' | 'America/Los_Angeles' | 'America/Argentina/Buenos_Aires' | 'America/Sao_Paulo' | 'Europe/London' | 'Europe/Madrid' | 'Africa/Johannesburg' | 'Asia/Tokyo' | 'Australia/Sydney';
2
+ type Now = {
3
+ year: number;
4
+ yearStr: string;
5
+ month: number;
6
+ monthStr: string;
7
+ day: number;
8
+ dayStr: string;
9
+ hour: number;
10
+ hourStr: string;
11
+ minute: number;
12
+ minuteStr: string;
13
+ };
14
+ type Timezone = 'UTC' | 'America/Argentina/Buenos_Aires';
15
+ type Task = (db: Db, now: Now) => Promise<void>;
4
16
  type ScheduleConfig = {
5
17
  expression: string;
6
- taskFn: TaskFn;
7
- timezone?: Timezone;
18
+ timezone: Timezone;
19
+ task: Task;
8
20
  runOnInit?: boolean;
9
21
  };
10
22
  export declare abstract class Job {
11
23
  static Schedule(config: ScheduleConfig): void;
12
24
  private static __Run;
25
+ private static __GetNow;
13
26
  }
14
27
  export interface JobCtor {
15
28
  Init(): void;
@@ -8,14 +8,15 @@ const node_cron_1 = __importDefault(require("node-cron"));
8
8
  const database_1 = require("../database");
9
9
  class Job {
10
10
  static Schedule(config) {
11
- const { expression, taskFn, timezone = 'UTC', runOnInit = false } = config;
12
- node_cron_1.default.schedule(expression, () => this.__Run(taskFn), { timezone, runOnInit });
11
+ const { expression, timezone, task, runOnInit = false } = config;
12
+ node_cron_1.default.schedule(expression, () => this.__Run(timezone, task), { timezone, runOnInit });
13
13
  }
14
- static async __Run(taskFn) {
14
+ static async __Run(timezone, task) {
15
15
  const tx = await (0, database_1.getDbClient)().transaction();
16
16
  const db = new database_1.Db(tx);
17
+ const now = this.__GetNow(timezone);
17
18
  try {
18
- await taskFn(db);
19
+ await task(db, now);
19
20
  await tx.commit();
20
21
  }
21
22
  catch (err) {
@@ -23,5 +24,36 @@ class Job {
23
24
  throw err;
24
25
  }
25
26
  }
27
+ static __GetNow(timezone) {
28
+ const today = new Date();
29
+ const parts = new Intl.DateTimeFormat(undefined, {
30
+ timeZone: timezone,
31
+ year: 'numeric',
32
+ month: 'numeric',
33
+ day: 'numeric',
34
+ hour: 'numeric',
35
+ minute: 'numeric',
36
+ hour12: false,
37
+ }).formatToParts(today);
38
+ const getNum = (type) => Number(parts.find((p) => p.type === type).value);
39
+ const pad = (num) => String(num).padStart(2, '0');
40
+ const year = getNum('year');
41
+ const month = getNum('month');
42
+ const day = getNum('day');
43
+ const hour = getNum('hour');
44
+ const minute = getNum('minute');
45
+ return {
46
+ year,
47
+ yearStr: String(year),
48
+ month,
49
+ monthStr: pad(month),
50
+ day,
51
+ dayStr: pad(day),
52
+ hour,
53
+ hourStr: pad(hour),
54
+ minute,
55
+ minuteStr: pad(minute),
56
+ };
57
+ }
26
58
  }
27
59
  exports.Job = Job;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pangea-server",
3
3
  "description": "",
4
- "version": "3.3.0",
4
+ "version": "3.3.2",
5
5
  "files": [
6
6
  "dist"
7
7
  ],