@panter/cloud-tasks 0.0.0-main-bd6f546b → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -175
- package/package.json +4 -5
package/README.md
CHANGED
|
@@ -1,176 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Make Google Cloud Tasks Great Again
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
## 🔹 Why Use This Library?
|
|
6
|
-
|
|
7
|
-
Using Google Cloud Tasks typically requires:
|
|
8
|
-
|
|
9
|
-
1. Implementing a worker service.
|
|
10
|
-
2. Defining and managing the API between the worker and client.
|
|
11
|
-
3. Handling task queue management and scheduling.
|
|
12
|
-
4. Setting up a local development environment.
|
|
13
|
-
5. Configuring Google Cloud Tasks to send HTTP requests to the worker.
|
|
14
|
-
|
|
15
|
-
This library eliminates much of this complexity by leveraging **tRPC** to define the API between the worker and the client.
|
|
16
|
-
|
|
17
|
-
### ✅ Key Features
|
|
18
|
-
|
|
19
|
-
- **Seamless API communication** between workers and clients.
|
|
20
|
-
- **Simple task scheduling** with an intuitive API.
|
|
21
|
-
- **Type safety and jump-to-definition** support via tRPC.
|
|
22
|
-
- **Built-in support for local development** with a Cloud Tasks emulator.
|
|
23
|
-
|
|
24
|
-
---
|
|
25
|
-
|
|
26
|
-
## 📂 Example Usage
|
|
27
|
-
|
|
28
|
-
Suppose we have two task workers (`when-multiple` and `this-is-nice-convention`) and a backend API (`some-backend-api`) that schedules tasks.
|
|
29
|
-
|
|
30
|
-
### Project Structure
|
|
31
|
-
|
|
32
|
-
```plaintext
|
|
33
|
-
.
|
|
34
|
-
└── apps/
|
|
35
|
-
├── tasks-worker-when-multiple/
|
|
36
|
-
│ ├── api.ts
|
|
37
|
-
│ └── index.ts
|
|
38
|
-
├── tasks-worker-this-is-nice-convention/
|
|
39
|
-
│ ├── api.ts
|
|
40
|
-
│ └── index.ts
|
|
41
|
-
└── some-backend-api/
|
|
42
|
-
└── src/
|
|
43
|
-
└── someEndpoint.ts
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
---
|
|
47
|
-
|
|
48
|
-
### 🛠 Step 1: Create the Task Worker (`api.ts`)
|
|
49
|
-
|
|
50
|
-
Define a **tRPC-powered task server**:
|
|
51
|
-
|
|
52
|
-
```ts
|
|
53
|
-
import { logger } from "@repo/logger";
|
|
54
|
-
import { createTasksServer } from "@panter/cloud-tasks/server";
|
|
55
|
-
import { z } from "zod";
|
|
56
|
-
|
|
57
|
-
logger.info("Starting tasks server...");
|
|
58
|
-
|
|
59
|
-
export const { runServer, router } = createTasksServer((t) =>
|
|
60
|
-
t.router({
|
|
61
|
-
getUser: t.procedure.input(z.string()).query((opts) => {
|
|
62
|
-
return { id: opts.input, name: "Bilbo" };
|
|
63
|
-
}),
|
|
64
|
-
createUser: t.procedure
|
|
65
|
-
.input(z.object({ name: z.string().min(5) }))
|
|
66
|
-
.mutation(async (opts) => {
|
|
67
|
-
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
68
|
-
return { id: "1", name: opts.input.name };
|
|
69
|
-
}),
|
|
70
|
-
doNothing: t.procedure.mutation(() => {
|
|
71
|
-
logger.info("Executing a no-op task");
|
|
72
|
-
}),
|
|
73
|
-
sendEmail: t.procedure.mutation(() => {}),
|
|
74
|
-
}),
|
|
75
|
-
);
|
|
76
|
-
|
|
77
|
-
// Export the router type for use in the client
|
|
78
|
-
export type Router = typeof router;
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
---
|
|
82
|
-
|
|
83
|
-
### 🚀 Step 2: Start the Task Worker (`index.ts`)
|
|
84
|
-
|
|
85
|
-
Initialize the worker server:
|
|
86
|
-
|
|
87
|
-
```ts
|
|
88
|
-
import { logger } from "@repo/logger";
|
|
89
|
-
import { runServer } from "./api";
|
|
90
|
-
|
|
91
|
-
const port = parseInt(process.env.PORT);
|
|
92
|
-
runServer(port);
|
|
93
|
-
logger.info(`🚀 Task worker tRPC server running at http://localhost:${port}/`);
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
---
|
|
97
|
-
|
|
98
|
-
### 🏗 Step 3: Create the Task Client (`some-backend-api`)
|
|
99
|
-
|
|
100
|
-
Now, in the backend API (`some-backend-api`), define a **task client** to send tasks:
|
|
101
|
-
|
|
102
|
-
```ts
|
|
103
|
-
import { builder } from "../builder";
|
|
104
|
-
import { logger } from "@repo/logger";
|
|
105
|
-
|
|
106
|
-
// Import the router type from the worker
|
|
107
|
-
import type { Router } from "@repo/tasks-worker-when-multiple/api";
|
|
108
|
-
import { createTasksClient } from "@panter/cloud-tasks/client";
|
|
109
|
-
|
|
110
|
-
const tasks = createTasksClient<Router>({
|
|
111
|
-
tasksWorkerUrl: new URL(process.env.TASKS_WORKER_URL),
|
|
112
|
-
queueName: "reasonable-queue-name",
|
|
113
|
-
useEmulator: process.env.ENV_SHORT === "local", // Enable emulator in local development
|
|
114
|
-
logger,
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
builder.mutationField("runJob", (t) =>
|
|
118
|
-
t.field({
|
|
119
|
-
type: "String",
|
|
120
|
-
resolve: async () => {
|
|
121
|
-
// Schedule a task with a payload
|
|
122
|
-
await tasks.createUser.schedule({ name: "Bilbo Baggins" });
|
|
123
|
-
// ^^^^^^^^^^ Try jumping to definition here!
|
|
124
|
-
return "ok";
|
|
125
|
-
},
|
|
126
|
-
}),
|
|
127
|
-
);
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
#### 📌 Important: Avoid Circular Dependencies
|
|
131
|
-
|
|
132
|
-
Notice that `Router` is **imported as a type** from `api.ts`. This prevents circular dependencies since `@repo/tasks-worker-when-multiple` is only included as a `devDependency`:
|
|
133
|
-
|
|
134
|
-
```json
|
|
135
|
-
{
|
|
136
|
-
"dependencies": {
|
|
137
|
-
"@panter/cloud-tasks": "^0.0.1"
|
|
138
|
-
},
|
|
139
|
-
"devDependencies": {
|
|
140
|
-
"@repo/tasks-worker-when-multiple": "workspace:*"
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
---
|
|
146
|
-
|
|
147
|
-
## 🛠 Local Development: Using Cloud Tasks Emulator
|
|
148
|
-
|
|
149
|
-
For local testing, use the **Cloud Tasks Emulator** with Docker:
|
|
150
|
-
|
|
151
|
-
```yml
|
|
152
|
-
version: "3.1"
|
|
153
|
-
|
|
154
|
-
services:
|
|
155
|
-
gcloud-tasks-emulator:
|
|
156
|
-
image: ghcr.io/aertje/cloud-tasks-emulator:latest
|
|
157
|
-
command: -host 0.0.0.0 -port 8123
|
|
158
|
-
ports:
|
|
159
|
-
- "6020:8123"
|
|
160
|
-
# NOTE: Comment out `extra_hosts` if using Podman (see: https://github.com/containers/podman/issues/21681)
|
|
161
|
-
extra_hosts:
|
|
162
|
-
- "host.containers.internal:host-gateway"
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
---
|
|
166
|
-
|
|
167
|
-
## 🎯 Summary
|
|
168
|
-
|
|
169
|
-
This library makes **Google Cloud Tasks** easy to use by:
|
|
170
|
-
|
|
171
|
-
- Removing the need for manual HTTP request handling.
|
|
172
|
-
- Providing a **type-safe, tRPC-powered API**.
|
|
173
|
-
- Enabling seamless communication between workers and clients.
|
|
174
|
-
- Offering **built-in support for local development**.
|
|
175
|
-
|
|
176
|
-
💡 **With this library, scheduling background tasks is as simple as calling a function!** 🚀
|
|
3
|
+
This allows you create quickly type safe API between your tasks
|
|
4
|
+
worker and a client that sends tasks to it.
|
package/package.json
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"name": "@panter/cloud-tasks",
|
|
3
3
|
"repository": {
|
|
4
4
|
"type": "git",
|
|
5
|
-
"url": "https://git.panter.ch/open-source/cloud-tasks.git"
|
|
5
|
+
"url": "git+https://git.panter.ch/open-source/cloud-tasks.git"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.0.0
|
|
7
|
+
"version": "0.0.0",
|
|
8
8
|
"sideEffects": false,
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
@@ -48,18 +48,17 @@
|
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"@google-cloud/tasks": "^5.5.2",
|
|
51
|
-
"@trpc/client": "^10.45.2",
|
|
52
51
|
"@trpc/server": "^10.45.2",
|
|
53
52
|
"express": "^4.21.2",
|
|
54
53
|
"ts-results-es": "^4.2.0"
|
|
55
54
|
},
|
|
56
55
|
"devDependencies": {
|
|
57
56
|
"@biomejs/biome": "1.9.4",
|
|
57
|
+
"@trpc/client": "^10.45.2",
|
|
58
58
|
"@types/express": "^4.17.21",
|
|
59
59
|
"esbuild": "^0.24.2",
|
|
60
60
|
"prettier": "^3.4.2",
|
|
61
61
|
"typescript": "5.7.3"
|
|
62
62
|
},
|
|
63
|
-
"packageManager": "yarn@4.6.0"
|
|
64
|
-
"stableVersion": "0.0.0"
|
|
63
|
+
"packageManager": "yarn@4.6.0"
|
|
65
64
|
}
|