UNPKG

4.33 kBJavaScriptView Raw
1"use strict";
2// The MIT License (MIT)
3//
4// Copyright (c) 2022 Firebase
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12//
13// The above copyright notice and this permission notice shall be included in all
14// copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22// SOFTWARE.
23Object.defineProperty(exports, "__esModule", { value: true });
24exports.onSchedule = exports.getOpts = void 0;
25const encoding_1 = require("../../common/encoding");
26const manifest_1 = require("../../runtime/manifest");
27const trace_1 = require("../trace");
28const logger = require("../../logger");
29const options = require("../options");
30const onInit_1 = require("../../common/onInit");
31/** @internal */
32function 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}
52exports.getOpts = getOpts;
53/**
54 * Handler for scheduled functions. Triggered whenever the associated
55 * scheduler job sends a http request.
56 * @param args - Either a schedule or an object containing function options.
57 * @param handler - A function to execute when triggered.
58 * @returns A function that you can export and deploy.
59 */
60function 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}
103exports.onSchedule = onSchedule;