Skip to content

Commit 5257a44

Browse files
authored
Merge pull request #227 from firebase/@invertase/handler-namespace
refactor(firestore-counter): use handlers
2 parents 87cdc6a + 699d865 commit 5257a44

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

firestore-counter/functions/lib/index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ exports.controllerCore = functions.handler.pubsub.topic.onPublish(() => __awaite
5454
/**
5555
* Backwards compatible HTTPS function
5656
*/
57-
exports.controller = functions.https.onRequest((req, res) => __awaiter(void 0, void 0, void 0, function* () {
57+
exports.controller = functions.handler.https.onRequest((req, res) => __awaiter(void 0, void 0, void 0, function* () {
5858
if (!pubsub) {
5959
pubsub = new pubsub_1.PubSub();
6060
}
@@ -71,9 +71,7 @@ exports.controller = functions.https.onRequest((req, res) => __awaiter(void 0, v
7171
* ControllerCore is monitoring these metadata documents to detect overload that requires
7272
* resharding and to detect failed workers that need poking.
7373
*/
74-
exports.worker = functions.firestore
75-
.document(process.env.INTERNAL_STATE_PATH + WORKERS_COLLECTION_ID + "/{workerId}")
76-
.onWrite((change, context) => __awaiter(void 0, void 0, void 0, function* () {
74+
exports.worker = functions.handler.firestore.document.onWrite((change, context) => __awaiter(void 0, void 0, void 0, function* () {
7775
// stop worker if document got deleted
7876
if (!change.after.exists)
7977
return;
@@ -85,9 +83,7 @@ exports.worker = functions.firestore
8583
* limited to one concurrent run at the time. This helps reduce latency for workloads
8684
* that are below the threshold for workers.
8785
*/
88-
exports.onWrite = functions.firestore
89-
.document("/{collection}/{**}/_counter_shards_/{shardId}")
90-
.onWrite((change, context) => __awaiter(void 0, void 0, void 0, function* () {
86+
exports.onWrite = functions.handler.firestore.document.onWrite((change, context) => __awaiter(void 0, void 0, void 0, function* () {
9187
const metadocRef = firestore.doc(process.env.INTERNAL_STATE_PATH);
9288
const controller = new controller_1.ShardedCounterController(metadocRef, SHARDS_COLLECTION_ID);
9389
yield controller.aggregateContinuously({ start: "", end: "" }, 200, 60000);

firestore-counter/functions/src/index.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,17 @@ export const controllerCore = functions.handler.pubsub.topic.onPublish(
5656
/**
5757
* Backwards compatible HTTPS function
5858
*/
59-
export const controller = functions.https.onRequest(async (req, res) => {
60-
if (!pubsub) {
61-
pubsub = new PubSub();
59+
export const controller = functions.handler.https.onRequest(
60+
async (req, res) => {
61+
if (!pubsub) {
62+
pubsub = new PubSub();
63+
}
64+
await pubsub
65+
.topic(process.env.EXT_INSTANCE_ID)
66+
.publish(Buffer.from(JSON.stringify({})));
67+
res.status(200).send("Ok");
6268
}
63-
await pubsub
64-
.topic(process.env.EXT_INSTANCE_ID)
65-
.publish(Buffer.from(JSON.stringify({})));
66-
res.status(200).send("Ok");
67-
});
69+
);
6870

6971
/**
7072
* Worker is responsible for aggregation of a defined range of shards. It is controlled
@@ -74,30 +76,28 @@ export const controller = functions.https.onRequest(async (req, res) => {
7476
* ControllerCore is monitoring these metadata documents to detect overload that requires
7577
* resharding and to detect failed workers that need poking.
7678
*/
77-
export const worker = functions.firestore
78-
.document(
79-
process.env.INTERNAL_STATE_PATH + WORKERS_COLLECTION_ID + "/{workerId}"
80-
)
81-
.onWrite(async (change, context) => {
79+
export const worker = functions.handler.firestore.document.onWrite(
80+
async (change, context) => {
8281
// stop worker if document got deleted
8382
if (!change.after.exists) return;
8483

8584
const worker = new ShardedCounterWorker(change.after, SHARDS_COLLECTION_ID);
8685
await worker.run();
87-
});
86+
}
87+
);
8888

8989
/**
9090
* This is an additional function that is triggered for every shard write. It is
9191
* limited to one concurrent run at the time. This helps reduce latency for workloads
9292
* that are below the threshold for workers.
9393
*/
94-
export const onWrite = functions.firestore
95-
.document("/{collection}/{**}/_counter_shards_/{shardId}")
96-
.onWrite(async (change, context) => {
94+
export const onWrite = functions.handler.firestore.document.onWrite(
95+
async (change, context) => {
9796
const metadocRef = firestore.doc(process.env.INTERNAL_STATE_PATH);
9897
const controller = new ShardedCounterController(
9998
metadocRef,
10099
SHARDS_COLLECTION_ID
101100
);
102101
await controller.aggregateContinuously({ start: "", end: "" }, 200, 60000);
103-
});
102+
}
103+
);

0 commit comments

Comments
 (0)