Skip to content

Commit 806ac58

Browse files
authored
Transactional email RPC service (#198)
Splitting transactional email to its own RPC Service ![image](https://github.com/user-attachments/assets/332b3ed3-bfe9-4d45-ad36-229b044c4053) ## Pasamos de 1.1mb minificado a 400kb minificado :D (Y de `130ms` de inicio, a `40ms`) ![Screenshot 2024-08-06 at 10 46 06 PM](https://github.com/user-attachments/assets/cac78c14-a776-47b4-8678-c2b4ce5f9bdd) ![Screenshot 2024-08-06 at 10 45 47 PM](https://github.com/user-attachments/assets/4bf278b2-c93b-4704-96a7-8e08b206da63) <img width="1593" alt="image" src="https://github.com/user-attachments/assets/602a80e7-1068-47e6-b14e-656d968fe3c4">
1 parent abb5db6 commit 806ac58

20 files changed

Lines changed: 318 additions & 142 deletions

File tree

.github/workflows/deploy_dev.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ jobs:
2828
key: CACHE-${{ env.cache-name }}-${{ hashFiles('package-lock.json') }}
2929
- name: 📥 Install dependencies
3030
run: npm ci --prefer-offline --no-audit --legacy-peer-deps --cache ~/.npm
31-
# Deploy the API
32-
- name: 🛳️ DEPLOY Cloudflare Workers
33-
run: CLOUDFLARE_API_TOKEN=${{ secrets.CLOUDFLARE_WORKERS_JSCONF_DEV_DEPLOY }} npx wrangler deploy --minify
31+
# Deploy the email RPC Service
32+
- name: 🛳️ DEPLOY EMAIL-RPC SERVICE WORKER
33+
run: CLOUDFLARE_API_TOKEN=${{ secrets.CLOUDFLARE_WORKERS_JSCONF_DEV_DEPLOY }} npx wrangler deploy --minify --config ./workers/transactional_email_service/wrangler.toml
3434
# Deploy the auth tokens worker
3535
- name: 🛳️ DEPLOY AUTH-TOKEN WORKER
3636
run: CLOUDFLARE_API_TOKEN=${{ secrets.CLOUDFLARE_WORKERS_JSCONF_DEV_DEPLOY }} npx wrangler deploy --minify --config ./workers/auth_tokens/wrangler.toml
@@ -46,6 +46,9 @@ jobs:
4646
# Deploy the sanity asset importer worker
4747
- name: 🛳️ DEPLOY SANITY-ASSET-IMPORTER WORKER
4848
run: CLOUDFLARE_API_TOKEN=${{ secrets.CLOUDFLARE_WORKERS_JSCONF_DEV_DEPLOY }} npx wrangler deploy --minify --config ./workers/sanity_asset_importer/wrangler.toml
49+
# Deploy the API
50+
- name: 🛳️ DEPLOY Cloudflare Workers
51+
run: CLOUDFLARE_API_TOKEN=${{ secrets.CLOUDFLARE_WORKERS_JSCONF_DEV_DEPLOY }} npx wrangler deploy --minify
4952
# Redeploy the EMAIL queue consumer
5053
- name: 💀/🛳️ RE-DEPLOY MAIL queue consumer
5154
run: |

.github/workflows/deploy_prod.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ jobs:
3131
key: CACHE-${{ env.cache-name }}-${{ hashFiles('package-lock.json') }}
3232
- name: 📥 Install dependencies
3333
run: npm ci --prefer-offline --no-audit --legacy-peer-deps --cache ~/.npm
34-
# Deploy the API
35-
- name: 🛳️ DEPLOY API WORKER
36-
run: CLOUDFLARE_API_TOKEN=${{ secrets.CLOUDFLARE_WORKERS_JSCONF_DEV_DEPLOY }} npx wrangler deploy --env=production --minify
34+
# Deploy the email RPC Service
35+
- name: 🛳️ DEPLOY EMAIL-RPC SERVICE WORKER
36+
run: CLOUDFLARE_API_TOKEN=${{ secrets.CLOUDFLARE_WORKERS_JSCONF_DEV_DEPLOY }} npx wrangler deploy --env=production --minify --config ./workers/transactional_email_service/wrangler.toml
3737
# Deploy the auth tokens worker
3838
- name: 🛳️ DEPLOY AUTH-TOKEN WORKER
3939
run: CLOUDFLARE_API_TOKEN=${{ secrets.CLOUDFLARE_WORKERS_JSCONF_DEV_DEPLOY }} npx wrangler deploy --env=production --minify --config ./workers/auth_tokens/wrangler.toml
@@ -49,7 +49,10 @@ jobs:
4949
# Deploy the sanity asset importer worker
5050
- name: 🛳️ DEPLOY SANITY-ASSET-IMPORTER WORKER
5151
run: CLOUDFLARE_API_TOKEN=${{ secrets.CLOUDFLARE_WORKERS_JSCONF_DEV_DEPLOY }} npx wrangler deploy --env=production --minify --config ./workers/sanity_asset_importer/wrangler.toml
52-
# Redeploy the EMAIL queue consumer
52+
# Deploy the API
53+
- name: 🛳️ DEPLOY API WORKER
54+
run: CLOUDFLARE_API_TOKEN=${{ secrets.CLOUDFLARE_WORKERS_JSCONF_DEV_DEPLOY }} npx wrangler deploy --env=production --minify
55+
# Redeploy the EMAIL queue consumer
5356
- name: 💀/🛳️ RE-DEPLOY queue consumer
5457
run: |
5558
CLOUDFLARE_API_TOKEN=${{ secrets.CLOUDFLARE_WORKERS_JSCONF_DEV_DEPLOY }} npx wrangler queues consumer remove mail-queue-production mail-queue-consumer-production && \

.github/workflows/deploy_staging.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ jobs:
3131
key: CACHE-${{ env.cache-name }}-${{ hashFiles('package-lock.json') }}
3232
- name: 📥 Install dependencies
3333
run: npm ci --prefer-offline --no-audit --legacy-peer-deps --cache ~/.npm
34-
# Deploy the API
35-
- name: 🛳️ DEPLOY Cloudflare Workers
36-
run: CLOUDFLARE_API_TOKEN=${{ secrets.CLOUDFLARE_WORKERS_JSCONF_DEV_DEPLOY }} npx wrangler deploy --env=staging --minify
34+
# Deploy the email RPC Service
35+
- name: 🛳️ DEPLOY EMAIL-RPC SERVICE WORKER
36+
run: CLOUDFLARE_API_TOKEN=${{ secrets.CLOUDFLARE_WORKERS_JSCONF_DEV_DEPLOY }} npx wrangler deploy --env=staging --minify --config ./workers/transactional_email_service/wrangler.toml
3737
# Deploy the auth tokens worker
3838
- name: 🛳️ DEPLOY AUTH-TOKEN WORKER
3939
run: CLOUDFLARE_API_TOKEN=${{ secrets.CLOUDFLARE_WORKERS_JSCONF_DEV_DEPLOY }} npx wrangler deploy --env=staging --minify --config ./workers/auth_tokens/wrangler.toml
@@ -49,6 +49,9 @@ jobs:
4949
# Deploy the sanity asset importer worker
5050
- name: 🛳️ DEPLOY SANITY-ASSET-IMPORTER WORKER
5151
run: CLOUDFLARE_API_TOKEN=${{ secrets.CLOUDFLARE_WORKERS_JSCONF_DEV_DEPLOY }} npx wrangler deploy --env=staging --minify --config ./workers/sanity_asset_importer/wrangler.toml
52+
# Deploy the API
53+
- name: 🛳️ DEPLOY Cloudflare Workers
54+
run: CLOUDFLARE_API_TOKEN=${{ secrets.CLOUDFLARE_WORKERS_JSCONF_DEV_DEPLOY }} npx wrangler deploy --env=staging --minify
5255
# Redeploy the EMAIL queue consumer
5356
- name: 💀/🛳️ RE-DEPLOY MAIL queue consumer
5457
run: |

package-lock.json

Lines changed: 45 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"prettier:ci": "prettier ./src --cache --check",
1919
"prettier:fix": "prettier ./src --cache --write",
2020
"typecheck": "tsc",
21-
"dev": "wrangler dev --log-level info --port 50123",
21+
"dev": "wrangler dev --log-level info",
2222
"dev:wall-of-fame": "wrangler dev --test-scheduled --config ./workers/wall_of_fame_cron/wrangler.toml",
2323
"dev:sanity": "wrangler dev --test-scheduled --config ./workers/sanity_asset_importer/wrangler.toml",
2424
"dev:auth": "wrangler dev --test-scheduled --config ./workers/auth_tokens/wrangler.toml",
@@ -27,7 +27,7 @@
2727
"email:export": "email export --dir ./emails/templates --outDir ./emails/out --pretty true"
2828
},
2929
"devDependencies": {
30-
"@cloudflare/workers-types": "^4.20231218.0",
30+
"@cloudflare/workers-types": "^4.20240806.0",
3131
"@graphql-codegen/add": "^5.0.0",
3232
"@graphql-codegen/cli": "4.0.1",
3333
"@graphql-codegen/import-types-preset": "^2.2.6",
@@ -64,7 +64,7 @@
6464
"uuid": "^9.0.1",
6565
"vite-tsconfig-paths": "^4.2.0",
6666
"vitest": "^0.33.0",
67-
"wrangler": "^3.65.0"
67+
"wrangler": "^3.69.1"
6868
},
6969
"engines": {
7070
"node": ">=18"

src/context.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const createGraphqlContext = async ({
2828
STRIPE_KEY,
2929
HYPERDRIVE,
3030
MERCADOPAGO_KEY,
31+
RPC_SERVICE_EMAIL,
3132
logger,
3233
}: YogaInitialContext &
3334
Env & {
@@ -37,6 +38,10 @@ export const createGraphqlContext = async ({
3738
throw new Error("Missing MAIL_QUEUE");
3839
}
3940

41+
if (!RPC_SERVICE_EMAIL) {
42+
throw new Error("RPC_SERVICE_EMAIL is not defined");
43+
}
44+
4045
if (!GOOGLE_PHOTOS_IMPORT_QUEUE) {
4146
throw new Error("Missing GOOGLE_PHOTOS_IMPORT_QUEUE");
4247
}
@@ -102,6 +107,7 @@ export const createGraphqlContext = async ({
102107
DB,
103108
logger,
104109
});
110+
105111
// This is the user that will be used for mostly all queries across the
106112
// application. However, in some cases, we might want to use the original
107113
// user. (Like on the "me" query)
@@ -127,5 +133,6 @@ export const createGraphqlContext = async ({
127133
GET_STRIPE_CLIENT,
128134
GET_MERCADOPAGO_CLIENT,
129135
logger,
136+
RPC_SERVICE_EMAIL,
130137
} satisfies Context;
131138
};
File renamed without changes.

src/errors.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import { Logger } from "pino";
33

44
import { builder } from "~/builder";
55

6-
enum ServiceErrors {
6+
export enum ServiceErrors {
77
UNAUTHENTICATED = "UNAUTHENTICATED",
8+
FAILED_PRECONDITION = "FAILED_PRECONDITION",
9+
FORBIDDEN = "FORBIDDEN",
810
}
911

1012
builder.enumType(ServiceErrors, {
@@ -13,6 +15,8 @@ builder.enumType(ServiceErrors, {
1315

1416
const error_codes = {
1517
[ServiceErrors.UNAUTHENTICATED]: 401,
18+
[ServiceErrors.FORBIDDEN]: 403,
19+
[ServiceErrors.FAILED_PRECONDITION]: 412,
1620
} as const;
1721

1822
export const applicationError = (

0 commit comments

Comments
 (0)