Skip to content

Commit 94af942

Browse files
authored
Merge pull request #59 from Space48/support-queues
Add support for Queues
2 parents 403996f + 27e6146 commit 94af942

File tree

7 files changed

+101
-17
lines changed

7 files changed

+101
-17
lines changed

README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,18 @@ export const runtimeConfig: GcpConfig = {
7575

7676
```typescript
7777
import { CloudEventFunction } from "@google-cloud/functions-framework";
78-
import { GcpConfig } from "@space48/cloud-seed";
78+
import type { GcpConfig } from "@space48/cloud-seed";
7979

8080
const fn: CloudEventFunction (data) => {
8181
console.log("This is a event triggered function", data);
8282
};
8383

84+
export default fn;
85+
8486
export const runtimeConfig: GcpConfig = {
8587
cloud: "gcp",
8688
type: "event",
87-
// By defining the topicName it will create to the topic for you.
89+
// By defining the topicName it will create the topic for you.
8890
topicName: "hello-world",
8991
};
9092
```
@@ -93,7 +95,7 @@ export const runtimeConfig: GcpConfig = {
9395

9496
```typescript
9597
import { CloudEventFunction } from "@google-cloud/functions-framework";
96-
import { GcpConfig } from "@space48/cloud-seed";
98+
import type { GcpConfig } from "@space48/cloud-seed";
9799

98100
const fn: CloudEventFunction = (data) => {
99101
console.log("This is a scheduled triggered function", data);
@@ -108,11 +110,30 @@ export const runtimeConfig: GcpConfig = {
108110
};
109111
```
110112

113+
### Queues:
114+
115+
```typescript
116+
import { HttpFunction } from "@google-cloud/functions-framework";
117+
import type { GcpConfig } from "@space48/cloud-seed";
118+
119+
const fn: HttpFunction = (req, res) => {
120+
console.log(req.body);
121+
return res.sendStatus(200);
122+
};
123+
124+
export default fn;
125+
126+
export const runtimeConfig: GcpConfig = {
127+
cloud: "gcp",
128+
type: "queue",
129+
};
130+
```
131+
111132
### Firestore document triggers:
112133

113134
```typescript
114135
import { CloudEventFunction } from "@google-cloud/functions-framework";
115-
import { GcpConfig } from "@space48/cloud-seed";
136+
import type { GcpConfig } from "@space48/cloud-seed";
116137

117138
const fn: CloudEventFunction = (data) => {
118139
console.log("This is a firestore triggered function", data);
@@ -133,7 +154,7 @@ export const runtimeConfig: GcpConfig = {
133154

134155
```typescript
135156
import { CloudEventFunction } from "@google-cloud/functions-framework";
136-
import { GcpConfig } from "@space48/cloud-seed";
157+
import type { GcpConfig } from "@space48/cloud-seed";
137158

138159
const fn: CloudEventFunction = (data) => {
139160
console.log("This is a cloud storage triggered function", data);

build/esbuild/bundle.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,10 @@ function generateFunctionName(file: string) {
5050
.replace(/(src|index|functions|function|\.ts)/g, "")
5151
.replace(/^\//, "")
5252
.replace(/\/$/, "")
53-
.replace(/[\/]{2,}/g, "/")
54-
.replace(/\//g, "-")
55-
.replace(/[-_]{2,}/g, "-")
56-
.replace(/^[-_]+/, "")
57-
.replace(/[-_]+$/, "");
53+
.replace(/[\/_]/g, "-")
54+
.replace(/[-]{2,}/g, "-")
55+
.replace(/^[-]+/, "")
56+
.replace(/[-]+$/, "");
5857
}
5958

6059
function detectRuntimeConfig(node: ts.Node) {

docs/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ More information, see:
5555
- [Creating HTTP Functions](./http.md)
5656
- [Creating PubSub Topics and Subscriptions](./pubsub.md)
5757
- [Creating Scheduled Functions](./scheduled.md)
58+
- [Creating Queue Functions](./queue.md)
5859
- [Creating Firestore triggered Functions](./firestore.md)
5960
- [Creating Cloud Storage triggered Functions](./storage.md)
60-
- [Creating Secrets](./secrets.md)
61+
- [Creating Secrets](./secrets.md)

docs/pubsub.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ If you want your function to be subscribed to a pubsub topic, you can use the `e
55
## Code sample
66

77
```typescript
8-
import { EventFunction } from "@google-cloud/functions-framework/build/src/functions";
9-
import { GcpConfig } from "@space48/cloud-seed";
8+
import { CloudEventFunction } from "@google-cloud/functions-framework";
9+
import type { GcpConfig } from "@space48/cloud-seed";
1010

11-
const fn: EventFunction (data) => {
11+
const fn: CloudEventFunction (data) => {
1212
console.log("This is a event triggered function", data);
1313
};
1414

15+
export default fn;
16+
1517
export const runtimeConfig: GcpConfig = {
1618
cloud: "gcp",
1719
type: "event",
18-
// By defining the topicName it will create to the topic for you.
20+
// By defining the topicName it will create the topic for you.
1921
topicName: "hello-world",
2022
};
2123
```
22-

docs/queue.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Queue Functions
2+
3+
If you want your function to be triggered by a Cloud Tasks queue, you can use the `queue` type in the runtimeConfig.
4+
5+
## Code sample
6+
7+
```typescript
8+
import { HttpFunction } from "@google-cloud/functions-framework";
9+
import type { GcpConfig } from "@space48/cloud-seed";
10+
11+
const fn: HttpFunction = (req, res) => {
12+
console.log(req.body);
13+
return res.sendStatus(200);
14+
};
15+
16+
export default fn;
17+
18+
export const runtimeConfig: GcpConfig = {
19+
cloud: "gcp",
20+
type: "queue",
21+
};
22+
```

stacks/gcp/GcpStack.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
CloudfunctionsFunctionConfig,
88
CloudfunctionsFunctionIamMember,
99
CloudSchedulerJob,
10+
CloudTasksQueue,
1011
ComputeAddress,
1112
ComputeNetwork,
1213
ComputeRouter,
@@ -25,6 +26,7 @@ import { StackOptions, GcpFunction } from "./types";
2526
export default class GcpStack extends TerraformStack {
2627
private options: StackOptions;
2728
private existingTopics: string[] = [];
29+
private existingQueues: string[] = [];
2830
private existingStaticIpVpcSubnets: string[] = [];
2931
constructor(scope: Construct, id: string, options: StackOptions) {
3032
super(scope, id);
@@ -103,6 +105,7 @@ export default class GcpStack extends TerraformStack {
103105
if (func.type === "event" && !this.existingTopics.includes(func.topicName)) {
104106
new PubsubTopic(this, func.topicName, {
105107
name: func.topicName,
108+
messageRetentionDuration: func.topicConfig?.messageRetentionDuration,
106109
});
107110
this.existingTopics.push(func.topicName);
108111
}
@@ -122,6 +125,26 @@ export default class GcpStack extends TerraformStack {
122125
});
123126
}
124127

128+
// Create Cloud Tasks queue if it doesn't exist already
129+
if (func.type === "queue" && !this.existingQueues.includes(func.name)) {
130+
new CloudTasksQueue(this, func.name + "-queue", {
131+
name: func.name,
132+
location: this.options.gcpOptions.region,
133+
rateLimits: {
134+
maxConcurrentDispatches: func.queueConfig?.maxConcurrentDispatches,
135+
maxDispatchesPerSecond: func.queueConfig?.maxDispatchesPerSecond,
136+
},
137+
retryConfig: {
138+
maxAttempts: func.queueConfig?.maxAttempts,
139+
minBackoff: func.queueConfig?.minBackoff,
140+
maxBackoff: func.queueConfig?.maxBackoff,
141+
maxDoublings: func.queueConfig?.maxDoublings,
142+
maxRetryDuration: func.queueConfig?.maxRetryDuration,
143+
},
144+
});
145+
this.existingQueues.push(func.name);
146+
}
147+
125148
// Configure static IP constraint
126149
if (func.staticIp) {
127150
const vpcAccessConnectorCidrRange = "10.1.1.0/28";
@@ -153,7 +176,7 @@ export default class GcpStack extends TerraformStack {
153176
private generateFunctionTriggerConfig(
154177
config: GcpFunction,
155178
): Pick<CloudfunctionsFunctionConfig, "triggerHttp" | "eventTrigger"> {
156-
if (config.type === "http") {
179+
if (config.type === "http" || config.type === "queue") {
157180
return {
158181
triggerHttp: true,
159182
};

types/runtime.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,29 @@ export type HttpConfig = {
2424
export type EventConfig = {
2525
type: "event";
2626
topicName: string;
27+
topicConfig?: {
28+
messageRetentionDuration?: string;
29+
};
2730
} & FunctionConfig;
2831

2932
export type ScheduleConfig = {
3033
type: "schedule";
3134
schedule: string;
3235
} & FunctionConfig;
3336

37+
export type QueueConfig = {
38+
type: "queue";
39+
queueConfig?: {
40+
maxDispatchesPerSecond?: number;
41+
maxConcurrentDispatches?: number;
42+
maxAttempts?: number;
43+
maxRetryDuration?: string;
44+
minBackoff?: string;
45+
maxBackoff?: string;
46+
maxDoublings?: number;
47+
};
48+
} & FunctionConfig;
49+
3450
export type FirestoreConfig = {
3551
type: "firestore";
3652
document: string;
@@ -50,6 +66,7 @@ export type GcpConfig = (
5066
| HttpConfig
5167
| EventConfig
5268
| ScheduleConfig
69+
| QueueConfig
5370
| FirestoreConfig
5471
| StorageConfig
5572
) & {

0 commit comments

Comments
 (0)