@upstash/workflow 1.4.0 → 1.5.1-canary.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 +9 -0
- package/astro.d.mts +19 -2
- package/astro.d.ts +19 -2
- package/astro.js +670 -159
- package/astro.mjs +1 -1
- package/{chunk-DRHEHWMS.mjs → chunk-J3PXPHSB.mjs} +828 -317
- package/cloudflare.d.mts +17 -2
- package/cloudflare.d.ts +17 -2
- package/cloudflare.js +670 -159
- package/cloudflare.mjs +1 -1
- package/docs/agents/examples.mdx +8 -0
- package/docs/agents/features.mdx +375 -0
- package/docs/agents/getting-started.mdx +139 -0
- package/docs/agents/overview.mdx +40 -0
- package/docs/agents/patterns/evaluator-optimizer.mdx +72 -0
- package/docs/agents/patterns/orchestrator-workers.mdx +95 -0
- package/docs/agents/patterns/parallelization.mdx +77 -0
- package/docs/agents/patterns/prompt-chaining.mdx +84 -0
- package/docs/basics/best-practices/at-least-one-step.mdx +57 -0
- package/docs/basics/best-practices/business-logic-in-steps.mdx +52 -0
- package/docs/basics/best-practices/deterministic-code.mdx +132 -0
- package/docs/basics/best-practices/idempotency.mdx +38 -0
- package/docs/basics/best-practices/nested-context-methods.mdx +38 -0
- package/docs/basics/best-practices/overview.mdx +35 -0
- package/docs/basics/best-practices/promise-any.mdx +9 -0
- package/docs/basics/best-practices/return-step-results.mdx +80 -0
- package/docs/basics/best-practices/workflow-limits.mdx +62 -0
- package/docs/basics/client/cancel.mdx +137 -0
- package/docs/basics/client/dlq/callback.mdx +21 -0
- package/docs/basics/client/dlq/delete.mdx +120 -0
- package/docs/basics/client/dlq/list.mdx +96 -0
- package/docs/basics/client/dlq/restart.mdx +164 -0
- package/docs/basics/client/dlq/resume.mdx +167 -0
- package/docs/basics/client/logs.mdx +139 -0
- package/docs/basics/client/notify.mdx +79 -0
- package/docs/basics/client/trigger.mdx +139 -0
- package/docs/basics/client/waiters.mdx +31 -0
- package/docs/basics/client.mdx +40 -0
- package/docs/basics/how.mdx +65 -0
- package/docs/basics/serve/advanced.mdx +435 -0
- package/docs/basics/serve.mdx +142 -0
- package/docs/changelog.mdx +174 -0
- package/docs/examples/allInOne.mdx +377 -0
- package/docs/examples/authWebhook.mdx +575 -0
- package/docs/examples/customRetry.mdx +309 -0
- package/docs/examples/customerOnboarding.mdx +230 -0
- package/docs/examples/dynamicWorkflow.mdx +203 -0
- package/docs/examples/eCommerceOrderFulfillment.mdx +281 -0
- package/docs/examples/imageProcessing.mdx +339 -0
- package/docs/examples/paymentRetry.mdx +438 -0
- package/docs/examples/waitForEvent.mdx +201 -0
- package/docs/features/dlq/callback.mdx +42 -0
- package/docs/features/dlq/delete.mdx +29 -0
- package/docs/features/dlq/restart.mdx +33 -0
- package/docs/features/dlq/resume.mdx +37 -0
- package/docs/features/dlq.mdx +39 -0
- package/docs/features/failure-callback.mdx +102 -0
- package/docs/features/failureFunction/advanced.mdx +36 -0
- package/docs/features/failureFunction/reliability.mdx +33 -0
- package/docs/features/flow-control/monitor.mdx +22 -0
- package/docs/features/flow-control/parallelism.mdx +45 -0
- package/docs/features/flow-control/rate-period.mdx +46 -0
- package/docs/features/flow-control.mdx +164 -0
- package/docs/features/invoke/serveMany.mdx +109 -0
- package/docs/features/invoke.mdx +39 -0
- package/docs/features/notify.mdx +127 -0
- package/docs/features/parallel-steps.mdx +41 -0
- package/docs/features/retries/prevent-retries.mdx +140 -0
- package/docs/features/retries.mdx +93 -0
- package/docs/features/wait-for-event.mdx +104 -0
- package/docs/features/wait.mdx +44 -0
- package/docs/features/webhooks.mdx +116 -0
- package/docs/getstarted.mdx +200 -0
- package/docs/howto/cancel.mdx +50 -0
- package/docs/howto/changes.mdx +33 -0
- package/docs/howto/configure.mdx +42 -0
- package/docs/howto/failures.mdx +185 -0
- package/docs/howto/get-token.mdx +18 -0
- package/docs/howto/local-development/development-server.mdx +130 -0
- package/docs/howto/local-development/local-tunnel.mdx +73 -0
- package/docs/howto/middlewares.mdx +263 -0
- package/docs/howto/migrations.mdx +335 -0
- package/docs/howto/multi-region.mdx +107 -0
- package/docs/howto/parallel-runs.mdx +52 -0
- package/docs/howto/readonly-token.mdx +19 -0
- package/docs/howto/realtime/basic.mdx +315 -0
- package/docs/howto/realtime/human-in-the-loop.mdx +454 -0
- package/docs/howto/redact-fields.mdx +61 -0
- package/docs/howto/retry-now.mdx +41 -0
- package/docs/howto/schedule.mdx +114 -0
- package/docs/howto/security.mdx +143 -0
- package/docs/howto/singleton.mdx +137 -0
- package/docs/howto/start.mdx +69 -0
- package/docs/howto/use-webhooks.mdx +256 -0
- package/docs/integrations/aisdk.mdx +316 -0
- package/docs/integrations/anthropic.mdx +51 -0
- package/docs/integrations/datadog.mdx +69 -0
- package/docs/integrations/openai.mdx +87 -0
- package/docs/integrations/prometheus.mdx +65 -0
- package/docs/integrations/resend.mdx +82 -0
- package/docs/llms-txt.mdx +4 -0
- package/docs/openapi.yaml +2946 -0
- package/docs/pricing.mdx +22 -0
- package/docs/quickstarts/astro.mdx +185 -0
- package/docs/quickstarts/cloudflare-workers.mdx +190 -0
- package/docs/quickstarts/express.mdx +190 -0
- package/docs/quickstarts/fastapi.mdx +282 -0
- package/docs/quickstarts/flask.mdx +282 -0
- package/docs/quickstarts/hono.mdx +221 -0
- package/docs/quickstarts/nextjs-fastapi.mdx +147 -0
- package/docs/quickstarts/nextjs-flask.mdx +147 -0
- package/docs/quickstarts/nuxt.mdx +169 -0
- package/docs/quickstarts/platforms.mdx +78 -0
- package/docs/quickstarts/solidjs.mdx +161 -0
- package/docs/quickstarts/svelte.mdx +170 -0
- package/docs/quickstarts/tanstack-start.mdx +205 -0
- package/docs/quickstarts/vercel-nextjs.mdx +233 -0
- package/docs/roadmap.mdx +8 -0
- package/docs/sdk/workflow-js.mdx +4 -0
- package/docs/sdk/workflow-py.mdx +4 -0
- package/docs/steps/api.mdx +64 -0
- package/docs/steps/call.mdx +183 -0
- package/docs/steps/cancel.mdx +25 -0
- package/docs/steps/createWebhook.mdx +40 -0
- package/docs/steps/invoke.mdx +100 -0
- package/docs/steps/notify.mdx +105 -0
- package/docs/steps/run.mdx +150 -0
- package/docs/steps/sleep.mdx +97 -0
- package/docs/steps/sleepUntil.mdx +88 -0
- package/docs/steps/waitForEvent.mdx +60 -0
- package/docs/steps/waitForWebhook.mdx +78 -0
- package/docs/steps.mdx +108 -0
- package/docs/troubleshooting/general.mdx +331 -0
- package/docs/troubleshooting/vercel.mdx +82 -0
- package/express.d.mts +19 -2
- package/express.d.ts +19 -2
- package/express.js +670 -159
- package/express.mjs +1 -1
- package/h3.d.mts +19 -2
- package/h3.d.ts +19 -2
- package/h3.js +670 -159
- package/h3.mjs +1 -1
- package/hono.d.mts +17 -2
- package/hono.d.ts +17 -2
- package/hono.js +670 -159
- package/hono.mjs +1 -1
- package/index.d.mts +50 -21
- package/index.d.ts +50 -21
- package/index.js +686 -171
- package/index.mjs +17 -13
- package/nextjs.d.mts +40 -2
- package/nextjs.d.ts +40 -2
- package/nextjs.js +670 -159
- package/nextjs.mjs +1 -1
- package/package.json +1 -1
- package/react-router.d.mts +16 -2
- package/react-router.d.ts +16 -2
- package/react-router.js +670 -159
- package/react-router.mjs +1 -1
- package/{serve-many-CG3BFvO3.d.mts → serve-many-BKukvLNl.d.mts} +1 -1
- package/{serve-many-iJF1IUXk.d.ts → serve-many-CrLKQpuZ.d.ts} +1 -1
- package/solidjs.d.mts +10 -1
- package/solidjs.d.ts +10 -1
- package/solidjs.js +670 -159
- package/solidjs.mjs +1 -1
- package/src/platforms/astro.ts +84 -0
- package/src/platforms/cloudflare.ts +127 -0
- package/src/platforms/express.ts +147 -0
- package/src/platforms/h3.ts +105 -0
- package/src/platforms/hono.ts +103 -0
- package/src/platforms/nextjs.ts +193 -0
- package/src/platforms/react-router.ts +92 -0
- package/src/platforms/solidjs.ts +59 -0
- package/src/platforms/svelte.ts +99 -0
- package/src/platforms/tanstack.ts +96 -0
- package/src/src/client/dlq.ts +387 -0
- package/src/src/client/filter-types.ts +166 -0
- package/src/src/client/index.ts +350 -0
- package/src/src/client/types.ts +501 -0
- package/src/src/client/utils.ts +307 -0
- package/src/src/constants.ts +82 -0
- package/src/src/context/api/anthropic.ts +58 -0
- package/src/src/context/api/base.ts +52 -0
- package/src/src/context/api/index.ts +24 -0
- package/src/src/context/api/openai.ts +125 -0
- package/src/src/context/api/resend.ts +57 -0
- package/src/src/context/auto-executor.ts +707 -0
- package/src/src/context/context.ts +681 -0
- package/src/src/context/index.ts +1 -0
- package/src/src/context/provider.ts +37 -0
- package/src/src/context/steps.ts +965 -0
- package/src/src/error.ts +203 -0
- package/src/src/index.ts +13 -0
- package/src/src/middleware/default-callbacks.ts +20 -0
- package/src/src/middleware/index.ts +2 -0
- package/src/src/middleware/logging.ts +39 -0
- package/src/src/middleware/manager.ts +148 -0
- package/src/src/middleware/middleware.ts +46 -0
- package/src/src/middleware/types.ts +72 -0
- package/src/src/qstash/headers.ts +397 -0
- package/src/src/qstash/step-config.ts +268 -0
- package/src/src/qstash/submit-steps.ts +284 -0
- package/src/src/serve/authorization.ts +118 -0
- package/src/src/serve/index.ts +475 -0
- package/src/src/serve/multi-region/handlers.ts +225 -0
- package/src/src/serve/multi-region/utils.ts +88 -0
- package/src/src/serve/options.ts +241 -0
- package/src/src/serve/serve-many.ts +117 -0
- package/src/src/serve/utils.ts +11 -0
- package/src/src/types.ts +590 -0
- package/src/src/utils.ts +141 -0
- package/src/src/workflow-parser.ts +472 -0
- package/src/src/workflow-requests.ts +575 -0
- package/svelte.d.mts +17 -2
- package/svelte.d.ts +17 -2
- package/svelte.js +670 -159
- package/svelte.mjs +1 -1
- package/tanstack.d.mts +15 -2
- package/tanstack.d.ts +15 -2
- package/tanstack.js +670 -159
- package/tanstack.mjs +1 -1
- package/{types-CekOpKvz.d.ts → types-CrgK5P6p.d.mts} +372 -32
- package/{types-CekOpKvz.d.mts → types-CrgK5P6p.d.ts} +372 -32
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Image Processing"
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
<Note>
|
|
6
|
+
Since parallel steps are not yet available in
|
|
7
|
+
[workflow-py](https://github.com/upstash/workflow-py), Python example is implemented in a sequential manner. See our
|
|
8
|
+
[Roadmap](/workflow/roadmap) for feature parity plans and
|
|
9
|
+
[Changelog](/workflow/changelog) for updates.
|
|
10
|
+
</Note>
|
|
11
|
+
|
|
12
|
+
## Introduction
|
|
13
|
+
|
|
14
|
+
This example demonstrates how to process images using Upstash Workflow. The following workflow will upload an image, resize it into multiple resolutions, apply filters, and store the processed versions for later retrieval.
|
|
15
|
+
|
|
16
|
+
## Use Case
|
|
17
|
+
|
|
18
|
+
Our workflow will:
|
|
19
|
+
|
|
20
|
+
1. Receive an image upload request
|
|
21
|
+
2. Resize the image into different resolutions
|
|
22
|
+
3. Apply various filters to the resized images
|
|
23
|
+
4. Store the processed images in a cloud storage
|
|
24
|
+
|
|
25
|
+
## Code Example
|
|
26
|
+
|
|
27
|
+
<CodeGroup>
|
|
28
|
+
|
|
29
|
+
```typescript api/workflow/route.ts
|
|
30
|
+
import { serve } from "@upstash/workflow/nextjs"
|
|
31
|
+
import {
|
|
32
|
+
resizeImage,
|
|
33
|
+
applyFilters,
|
|
34
|
+
storeImage,
|
|
35
|
+
getImageUrl,
|
|
36
|
+
} from "./utils"
|
|
37
|
+
|
|
38
|
+
type ImageResult = {
|
|
39
|
+
imageUrl: string
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const { POST } = serve<{ imageId: string; userId: string }>(
|
|
43
|
+
async (context) => {
|
|
44
|
+
const { imageId, userId } = context.requestPayload
|
|
45
|
+
|
|
46
|
+
// Step 1: Retrieve the uploaded image
|
|
47
|
+
const imageUrl = await context.run("get-image-url", async () => {
|
|
48
|
+
return await getImageUrl(imageId)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
// Step 2: Resize the image to multiple resolutions
|
|
52
|
+
const resolutions = [640, 1280, 1920]
|
|
53
|
+
|
|
54
|
+
const resizedImages: { body: ImageResult }[] = await Promise.all(resolutions.map(
|
|
55
|
+
resolution => context.call<ImageResult>(
|
|
56
|
+
`resize-image-${resolution}`,
|
|
57
|
+
{
|
|
58
|
+
// endpoint which returns ImageResult type in response
|
|
59
|
+
url: "https://image-processing-service.com/resize",
|
|
60
|
+
method: "POST",
|
|
61
|
+
body: JSON.stringify({
|
|
62
|
+
imageUrl,
|
|
63
|
+
width: resolution,
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
)
|
|
67
|
+
))
|
|
68
|
+
|
|
69
|
+
// Step 3: Apply filters to each resized image
|
|
70
|
+
const filters = ["grayscale", "sepia", "contrast"]
|
|
71
|
+
const processedImagePromises: Promise<string>[] = []
|
|
72
|
+
|
|
73
|
+
for (const resizedImage of resizedImages) {
|
|
74
|
+
for (const filter of filters) {
|
|
75
|
+
const processedImagePromise = context.call<ImageResult>(
|
|
76
|
+
`apply-filter-${filter}`,
|
|
77
|
+
{
|
|
78
|
+
// endpoint which returns ImageResult type in response
|
|
79
|
+
url: "https://image-processing-service.com/filter",
|
|
80
|
+
method: "POST",
|
|
81
|
+
body: JSON.stringify({
|
|
82
|
+
imageUrl: resizedImage.body.imageUrl,
|
|
83
|
+
filter,
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
)
|
|
87
|
+
processedImagePromises.push(processedImagePromise)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const processedImages: { body: ImageResult }[] = await Promise.all(processedImagePromises)
|
|
91
|
+
|
|
92
|
+
// Step 4: Store processed images in cloud storage
|
|
93
|
+
const storedImageUrls: string[] = await Promise.all(
|
|
94
|
+
processedImages.map(
|
|
95
|
+
processedImage => context.run(`store-image`, async () => {
|
|
96
|
+
return await storeImage(processedImage.body.imageUrl)
|
|
97
|
+
})
|
|
98
|
+
)
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
```python main.py
|
|
105
|
+
from fastapi import FastAPI
|
|
106
|
+
from typing import List, TypedDict
|
|
107
|
+
from upstash_workflow.fastapi import Serve
|
|
108
|
+
from upstash_workflow import AsyncWorkflowContext, CallResponse
|
|
109
|
+
from utils import store_image, get_image_url
|
|
110
|
+
|
|
111
|
+
app = FastAPI()
|
|
112
|
+
serve = Serve(app)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class ImageResult(TypedDict):
|
|
116
|
+
image_url: str
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class ImageProcessingPayload(TypedDict):
|
|
120
|
+
image_id: str
|
|
121
|
+
user_id: str
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
@serve.post("/process-image")
|
|
125
|
+
async def process_image(context: AsyncWorkflowContext[ImageProcessingPayload]) -> None:
|
|
126
|
+
payload = context.request_payload
|
|
127
|
+
image_id = payload["image_id"]
|
|
128
|
+
user_id = payload["user_id"]
|
|
129
|
+
|
|
130
|
+
# Step 1: Retrieve the uploaded image
|
|
131
|
+
async def _get_image_url() -> str:
|
|
132
|
+
return await get_image_url(image_id)
|
|
133
|
+
|
|
134
|
+
image_url: str = await context.run("get-image-url", _get_image_url)
|
|
135
|
+
|
|
136
|
+
# Step 2: Resize the image to multiple resolutions
|
|
137
|
+
resolutions = [640, 1280, 1920]
|
|
138
|
+
resize_responses = []
|
|
139
|
+
|
|
140
|
+
for resolution in resolutions:
|
|
141
|
+
response: CallResponse[ImageResult] = await context.call(
|
|
142
|
+
f"resize-image-{resolution}",
|
|
143
|
+
# endpoint which returns ImageResult type in response
|
|
144
|
+
url="https://image-processing-service.com/resize",
|
|
145
|
+
method="POST",
|
|
146
|
+
body={"imageUrl": image_url, "width": resolution},
|
|
147
|
+
)
|
|
148
|
+
resize_responses.append(response)
|
|
149
|
+
|
|
150
|
+
resized_images = [response.body for response in resize_responses]
|
|
151
|
+
|
|
152
|
+
# Step 3: Apply filters to each resized image
|
|
153
|
+
filters = ["grayscale", "sepia", "contrast"]
|
|
154
|
+
filter_responses = []
|
|
155
|
+
|
|
156
|
+
for resized_image in resized_images:
|
|
157
|
+
for filter in filters:
|
|
158
|
+
response: CallResponse[ImageResult] = await context.call(
|
|
159
|
+
f"apply-filter-{filter}",
|
|
160
|
+
# endpoint which returns ImageResult type in response
|
|
161
|
+
url="https://image-processing-service.com/filter",
|
|
162
|
+
method="POST",
|
|
163
|
+
body={"imageUrl": resized_image["imageUrl"], "filter": filter},
|
|
164
|
+
)
|
|
165
|
+
filter_responses.append(response)
|
|
166
|
+
|
|
167
|
+
processed_images = [response.body for response in filter_responses]
|
|
168
|
+
|
|
169
|
+
# Step 4: Store processed images in cloud storage
|
|
170
|
+
async def _store_image() -> str:
|
|
171
|
+
return await store_image(processed_image["imageUrl"])
|
|
172
|
+
|
|
173
|
+
stored_image_urls: List[str] = []
|
|
174
|
+
for processed_image in processed_images:
|
|
175
|
+
stored_image_url = await context.run("store-image", _store_image)
|
|
176
|
+
stored_image_urls.append(stored_image_url)
|
|
177
|
+
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
</CodeGroup>
|
|
181
|
+
|
|
182
|
+
## Code Breakdown
|
|
183
|
+
|
|
184
|
+
### 1. Retrieving the Image
|
|
185
|
+
|
|
186
|
+
We begin by getting the URL of the uploaded image based on its ID:
|
|
187
|
+
|
|
188
|
+
<CodeGroup>
|
|
189
|
+
|
|
190
|
+
```typescript TypeScript
|
|
191
|
+
const imageUrl = await context.run("get-image-url", async () => {
|
|
192
|
+
return await getImageUrl(imageId)
|
|
193
|
+
})
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
```python Python
|
|
197
|
+
async def _get_image_url() -> str:
|
|
198
|
+
return await get_image_url(image_id)
|
|
199
|
+
|
|
200
|
+
image_url: str = await context.run("get-image-url", _get_image_url)
|
|
201
|
+
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
</CodeGroup>
|
|
205
|
+
|
|
206
|
+
### 2. Resizing the Image
|
|
207
|
+
|
|
208
|
+
We resize the image into three different resolutions (640, 1280, 1920) using an external image processing service.
|
|
209
|
+
|
|
210
|
+
We call `context.call` for each resolution and use `Promise.all` to run them parallel:
|
|
211
|
+
|
|
212
|
+
<CodeGroup>
|
|
213
|
+
|
|
214
|
+
```typescript TypeScript
|
|
215
|
+
const resolutions = [640, 1280, 1920]
|
|
216
|
+
|
|
217
|
+
const resizedImages: { body: ImageResult }[] = await Promise.all(resolutions.map(
|
|
218
|
+
resolution => context.call<ImageResult>(
|
|
219
|
+
`resize-image-${resolution}`,
|
|
220
|
+
{
|
|
221
|
+
// endpoint which returns ImageResult type in response
|
|
222
|
+
url: "https://image-processing-service.com/resize",
|
|
223
|
+
method: "POST",
|
|
224
|
+
body: JSON.stringify({
|
|
225
|
+
imageUrl,
|
|
226
|
+
width: resolution,
|
|
227
|
+
})
|
|
228
|
+
}
|
|
229
|
+
)
|
|
230
|
+
))
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
```python Python
|
|
234
|
+
resolutions = [640, 1280, 1920]
|
|
235
|
+
resize_responses = []
|
|
236
|
+
|
|
237
|
+
for resolution in resolutions:
|
|
238
|
+
response: CallResponse[ImageResult] = await context.call(
|
|
239
|
+
f"resize-image-{resolution}",
|
|
240
|
+
# endpoint which returns ImageResult type in response
|
|
241
|
+
url="https://image-processing-service.com/resize",
|
|
242
|
+
method="POST",
|
|
243
|
+
body={"imageUrl": image_url, "width": resolution},
|
|
244
|
+
)
|
|
245
|
+
resize_responses.append(response)
|
|
246
|
+
|
|
247
|
+
resized_images = [response.body for response in resize_responses]
|
|
248
|
+
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
</CodeGroup>
|
|
252
|
+
|
|
253
|
+
### 3. Applying Filters
|
|
254
|
+
|
|
255
|
+
After resizing, we apply filters such as grayscale, sepia, and contrast to the resized images.
|
|
256
|
+
|
|
257
|
+
Again, we call `context.call` for each filter & image pair. We collect the promises of these requests in an array `processedImagePromise`. Then, we call `Promise.all` again to run them all parallel.
|
|
258
|
+
|
|
259
|
+
<CodeGroup>
|
|
260
|
+
|
|
261
|
+
```typescript TypeScript
|
|
262
|
+
const filters = ["grayscale", "sepia", "contrast"]
|
|
263
|
+
const processedImagePromises: Promise<string>[] = []
|
|
264
|
+
|
|
265
|
+
for (const resizedImage of resizedImages) {
|
|
266
|
+
for (const filter of filters) {
|
|
267
|
+
const processedImagePromise = context.call<ImageResult>(
|
|
268
|
+
`apply-filter-${filter}`,
|
|
269
|
+
{
|
|
270
|
+
// endpoint which returns ImageResult type in response
|
|
271
|
+
url: "https://image-processing-service.com/filter",
|
|
272
|
+
method: "POST",
|
|
273
|
+
body: {
|
|
274
|
+
imageUrl: resizedImage.body.imageUrl,
|
|
275
|
+
filter,
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
)
|
|
279
|
+
processedImagePromises.push(processedImagePromise)
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
const processedImages: { body: ImageResult }[] = await Promise.all(processedImagePromises)
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
```python Python
|
|
286
|
+
filters = ["grayscale", "sepia", "contrast"]
|
|
287
|
+
filter_responses = []
|
|
288
|
+
|
|
289
|
+
for resized_image in resized_images:
|
|
290
|
+
for filter in filters:
|
|
291
|
+
response: CallResponse[ImageResult] = await context.call(
|
|
292
|
+
f"apply-filter-{filter}",
|
|
293
|
+
# endpoint which returns ImageResult type in response
|
|
294
|
+
url="https://image-processing-service.com/filter",
|
|
295
|
+
method="POST",
|
|
296
|
+
body={"imageUrl": resized_image["imageUrl"], "filter": filter},
|
|
297
|
+
)
|
|
298
|
+
filter_responses.append(response)
|
|
299
|
+
|
|
300
|
+
processed_images = [response.body for response in filter_responses]
|
|
301
|
+
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
</CodeGroup>
|
|
305
|
+
|
|
306
|
+
### 4. Storing the Processed Images
|
|
307
|
+
|
|
308
|
+
We store each processed image in cloud storage and return the URLs of the stored images:
|
|
309
|
+
|
|
310
|
+
<CodeGroup>
|
|
311
|
+
|
|
312
|
+
```typescript TypeScript
|
|
313
|
+
const storedImageUrls: string[] = await Promise.all(
|
|
314
|
+
processedImages.map(
|
|
315
|
+
processedImage => context.run(`store-image`, async () => {
|
|
316
|
+
return await storeImage(processedImage.body.imageUrl)
|
|
317
|
+
})
|
|
318
|
+
)
|
|
319
|
+
)
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
```python Python
|
|
323
|
+
async def _store_image() -> str:
|
|
324
|
+
return await store_image(processed_image["imageUrl"])
|
|
325
|
+
|
|
326
|
+
stored_image_urls: List[str] = []
|
|
327
|
+
for processed_image in processed_images:
|
|
328
|
+
stored_image_url = await context.run("store-image", _store_image)
|
|
329
|
+
stored_image_urls.append(stored_image_url)
|
|
330
|
+
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
</CodeGroup>
|
|
334
|
+
|
|
335
|
+
## Key Features
|
|
336
|
+
|
|
337
|
+
1. **Batch Processing**: This workflow handles batch resizing and filtering of images in parallel to minimize processing time.
|
|
338
|
+
2. **Scalability**: Image resizing and filtering are handled through external services, making it easy to scale. Also, the requests to these services are handled by QStash, not by the compute where the workflow is hosted.
|
|
339
|
+
3. **Storage Integration**: The workflow integrates with cloud storage to persist processed images for future access.
|