1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | Object.defineProperty(exports, "__esModule", { value: true });
|
24 | exports.onSchedule = exports.getOpts = void 0;
|
25 | const encoding_1 = require("../../common/encoding");
|
26 | const manifest_1 = require("../../runtime/manifest");
|
27 | const trace_1 = require("../trace");
|
28 | const logger = require("../../logger");
|
29 | const options = require("../options");
|
30 | const onInit_1 = require("../../common/onInit");
|
31 |
|
32 | function getOpts(args) {
|
33 | if (typeof args === "string") {
|
34 | return {
|
35 | schedule: args,
|
36 | opts: {},
|
37 | };
|
38 | }
|
39 | return {
|
40 | schedule: args.schedule,
|
41 | timeZone: args.timeZone,
|
42 | retryConfig: {
|
43 | retryCount: args.retryCount,
|
44 | maxRetrySeconds: args.maxRetrySeconds,
|
45 | minBackoffSeconds: args.minBackoffSeconds,
|
46 | maxBackoffSeconds: args.maxBackoffSeconds,
|
47 | maxDoublings: args.maxDoublings,
|
48 | },
|
49 | opts: args,
|
50 | };
|
51 | }
|
52 | exports.getOpts = getOpts;
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | function onSchedule(args, handler) {
|
61 | const separatedOpts = getOpts(args);
|
62 | const httpFunc = async (req, res) => {
|
63 | const event = {
|
64 | jobName: req.header("X-CloudScheduler-JobName") || undefined,
|
65 | scheduleTime: req.header("X-CloudScheduler-ScheduleTime") || new Date().toISOString(),
|
66 | };
|
67 | try {
|
68 | await handler(event);
|
69 | res.status(200).send();
|
70 | }
|
71 | catch (err) {
|
72 | logger.error(err.message);
|
73 | res.status(500).send();
|
74 | }
|
75 | };
|
76 | const func = (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(httpFunc));
|
77 | func.run = handler;
|
78 | const globalOpts = options.getGlobalOptions();
|
79 | const baseOptsEndpoint = options.optionsToEndpoint(globalOpts);
|
80 | const specificOptsEndpoint = options.optionsToEndpoint(separatedOpts.opts);
|
81 | const ep = {
|
82 | ...(0, manifest_1.initV2Endpoint)(globalOpts, separatedOpts.opts),
|
83 | platform: "gcfv2",
|
84 | ...baseOptsEndpoint,
|
85 | ...specificOptsEndpoint,
|
86 | labels: {
|
87 | ...baseOptsEndpoint === null || baseOptsEndpoint === void 0 ? void 0 : baseOptsEndpoint.labels,
|
88 | ...specificOptsEndpoint === null || specificOptsEndpoint === void 0 ? void 0 : specificOptsEndpoint.labels,
|
89 | },
|
90 | scheduleTrigger: (0, manifest_1.initV2ScheduleTrigger)(separatedOpts.schedule, globalOpts, separatedOpts.opts),
|
91 | };
|
92 | (0, encoding_1.copyIfPresent)(ep.scheduleTrigger, separatedOpts, "timeZone");
|
93 | (0, encoding_1.copyIfPresent)(ep.scheduleTrigger.retryConfig, separatedOpts.retryConfig, "retryCount", "maxRetrySeconds", "minBackoffSeconds", "maxBackoffSeconds", "maxDoublings");
|
94 | func.__endpoint = ep;
|
95 | func.__requiredAPIs = [
|
96 | {
|
97 | api: "cloudscheduler.googleapis.com",
|
98 | reason: "Needed for scheduled functions.",
|
99 | },
|
100 | ];
|
101 | return func;
|
102 | }
|
103 | exports.onSchedule = onSchedule;
|