Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- fix(v1): Call onInit for schedule.onRun functions (#1801)
28 changes: 27 additions & 1 deletion spec/v1/providers/pubsub.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import { expect } from "chai";

import { LegacyEvent, RESET_VALUE } from "../../../src/v1";
import { LegacyEvent, RESET_VALUE, onInit } from "../../../src/v1";
import { MINIMAL_V1_ENDPOINT } from "../../fixtures";
import { MINIMAL_SCHEDULE_TRIGGER } from "./fixtures";
import * as functions from "../../../src/v1";
Expand Down Expand Up @@ -256,6 +256,32 @@ describe("Pubsub Functions", () => {
}
);

it("should call onInit before executing scheduled function", async () => {
const context = {
eventId: "00000",
timestamp: "2016-11-04T21:29:03.496Z",
eventType: "google.pubsub.topic.publish",
resource: {
service: "pubsub.googleapis.com",
name: "projects/project-id/topics/topic-name",
},
};

let initCalled = false;
onInit(() => {
initCalled = true;
});

const scheduledFunc = pubsub.schedule("every 5 minutes").onRun(() => {
expect(initCalled).to.be.true;
return null;
});

expect(initCalled).to.be.false;
await scheduledFunc(null, context);
expect(initCalled).to.be.true;
});

it("should return an appropriate trigger/endpoint when called with region and options", () => {
const result = functions
.region("us-east1")
Expand Down
3 changes: 2 additions & 1 deletion src/v1/cloud-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ export function makeCloudFunction<EventData>({
triggerResource,
}: MakeCloudFunctionArgs<EventData>): CloudFunction<EventData> {
handler = withInit(handler ?? contextOnlyHandler);
const wrappedContextOnlyHandler = contextOnlyHandler ? withInit(contextOnlyHandler) : undefined;
const cloudFunction: any = (data: any, context: any) => {
if (legacyEventType && context.eventType === legacyEventType) {
/*
Expand Down Expand Up @@ -433,7 +434,7 @@ export function makeCloudFunction<EventData>({
let promise;
if (labels && labels["deployment-scheduled"]) {
// Scheduled function do not have meaningful data, so exclude it
promise = contextOnlyHandler(context);
promise = wrappedContextOnlyHandler(context);
} else {
const dataOrChange = dataConstructor(event);
promise = handler(dataOrChange, context);
Expand Down
Loading