Skip to content

Commit

Permalink
Merge pull request #59 from Space48/support-queues
Browse files Browse the repository at this point in the history
Add support for Queues
  • Loading branch information
S48-Mo authored Sep 5, 2022
2 parents 403996f + 27e6146 commit 94af942
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 17 deletions.
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,18 @@ export const runtimeConfig: GcpConfig = {

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

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

export default fn;

export const runtimeConfig: GcpConfig = {
cloud: "gcp",
type: "event",
// By defining the topicName it will create to the topic for you.
// By defining the topicName it will create the topic for you.
topicName: "hello-world",
};
```
Expand All @@ -93,7 +95,7 @@ export const runtimeConfig: GcpConfig = {

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

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

### Queues:

```typescript
import { HttpFunction } from "@google-cloud/functions-framework";
import type { GcpConfig } from "@space48/cloud-seed";

const fn: HttpFunction = (req, res) => {
console.log(req.body);
return res.sendStatus(200);
};

export default fn;

export const runtimeConfig: GcpConfig = {
cloud: "gcp",
type: "queue",
};
```

### Firestore document triggers:

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

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

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

const fn: CloudEventFunction = (data) => {
console.log("This is a cloud storage triggered function", data);
Expand Down
9 changes: 4 additions & 5 deletions build/esbuild/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ function generateFunctionName(file: string) {
.replace(/(src|index|functions|function|\.ts)/g, "")
.replace(/^\//, "")
.replace(/\/$/, "")
.replace(/[\/]{2,}/g, "/")
.replace(/\//g, "-")
.replace(/[-_]{2,}/g, "-")
.replace(/^[-_]+/, "")
.replace(/[-_]+$/, "");
.replace(/[\/_]/g, "-")
.replace(/[-]{2,}/g, "-")
.replace(/^[-]+/, "")
.replace(/[-]+$/, "");
}

function detectRuntimeConfig(node: ts.Node) {
Expand Down
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ More information, see:
- [Creating HTTP Functions](./http.md)
- [Creating PubSub Topics and Subscriptions](./pubsub.md)
- [Creating Scheduled Functions](./scheduled.md)
- [Creating Queue Functions](./queue.md)
- [Creating Firestore triggered Functions](./firestore.md)
- [Creating Cloud Storage triggered Functions](./storage.md)
- [Creating Secrets](./secrets.md)
- [Creating Secrets](./secrets.md)
11 changes: 6 additions & 5 deletions docs/pubsub.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ If you want your function to be subscribed to a pubsub topic, you can use the `e
## Code sample

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

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

export default fn;

export const runtimeConfig: GcpConfig = {
cloud: "gcp",
type: "event",
// By defining the topicName it will create to the topic for you.
// By defining the topicName it will create the topic for you.
topicName: "hello-world",
};
```

22 changes: 22 additions & 0 deletions docs/queue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Queue Functions

If you want your function to be triggered by a Cloud Tasks queue, you can use the `queue` type in the runtimeConfig.

## Code sample

```typescript
import { HttpFunction } from "@google-cloud/functions-framework";
import type { GcpConfig } from "@space48/cloud-seed";

const fn: HttpFunction = (req, res) => {
console.log(req.body);
return res.sendStatus(200);
};

export default fn;

export const runtimeConfig: GcpConfig = {
cloud: "gcp",
type: "queue",
};
```
25 changes: 24 additions & 1 deletion stacks/gcp/GcpStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
CloudfunctionsFunctionConfig,
CloudfunctionsFunctionIamMember,
CloudSchedulerJob,
CloudTasksQueue,
ComputeAddress,
ComputeNetwork,
ComputeRouter,
Expand All @@ -25,6 +26,7 @@ import { StackOptions, GcpFunction } from "./types";
export default class GcpStack extends TerraformStack {
private options: StackOptions;
private existingTopics: string[] = [];
private existingQueues: string[] = [];
private existingStaticIpVpcSubnets: string[] = [];
constructor(scope: Construct, id: string, options: StackOptions) {
super(scope, id);
Expand Down Expand Up @@ -103,6 +105,7 @@ export default class GcpStack extends TerraformStack {
if (func.type === "event" && !this.existingTopics.includes(func.topicName)) {
new PubsubTopic(this, func.topicName, {
name: func.topicName,
messageRetentionDuration: func.topicConfig?.messageRetentionDuration,
});
this.existingTopics.push(func.topicName);
}
Expand All @@ -122,6 +125,26 @@ export default class GcpStack extends TerraformStack {
});
}

// Create Cloud Tasks queue if it doesn't exist already
if (func.type === "queue" && !this.existingQueues.includes(func.name)) {
new CloudTasksQueue(this, func.name + "-queue", {
name: func.name,
location: this.options.gcpOptions.region,
rateLimits: {
maxConcurrentDispatches: func.queueConfig?.maxConcurrentDispatches,
maxDispatchesPerSecond: func.queueConfig?.maxDispatchesPerSecond,
},
retryConfig: {
maxAttempts: func.queueConfig?.maxAttempts,
minBackoff: func.queueConfig?.minBackoff,
maxBackoff: func.queueConfig?.maxBackoff,
maxDoublings: func.queueConfig?.maxDoublings,
maxRetryDuration: func.queueConfig?.maxRetryDuration,
},
});
this.existingQueues.push(func.name);
}

// Configure static IP constraint
if (func.staticIp) {
const vpcAccessConnectorCidrRange = "10.1.1.0/28";
Expand Down Expand Up @@ -153,7 +176,7 @@ export default class GcpStack extends TerraformStack {
private generateFunctionTriggerConfig(
config: GcpFunction,
): Pick<CloudfunctionsFunctionConfig, "triggerHttp" | "eventTrigger"> {
if (config.type === "http") {
if (config.type === "http" || config.type === "queue") {
return {
triggerHttp: true,
};
Expand Down
17 changes: 17 additions & 0 deletions types/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,29 @@ export type HttpConfig = {
export type EventConfig = {
type: "event";
topicName: string;
topicConfig?: {
messageRetentionDuration?: string;
};
} & FunctionConfig;

export type ScheduleConfig = {
type: "schedule";
schedule: string;
} & FunctionConfig;

export type QueueConfig = {
type: "queue";
queueConfig?: {
maxDispatchesPerSecond?: number;
maxConcurrentDispatches?: number;
maxAttempts?: number;
maxRetryDuration?: string;
minBackoff?: string;
maxBackoff?: string;
maxDoublings?: number;
};
} & FunctionConfig;

export type FirestoreConfig = {
type: "firestore";
document: string;
Expand All @@ -50,6 +66,7 @@ export type GcpConfig = (
| HttpConfig
| EventConfig
| ScheduleConfig
| QueueConfig
| FirestoreConfig
| StorageConfig
) & {
Expand Down

0 comments on commit 94af942

Please sign in to comment.