Skip to content

Commit

Permalink
run console.ts with bun in lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
rhyek committed Mar 19, 2024
1 parent e6a200e commit bad2056
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 61 deletions.
23 changes: 7 additions & 16 deletions projects/scrape-txs/build/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
FROM node:18-buster-slim as build
WORKDIR /app
COPY ./package.json ./
RUN npm install
COPY ./src ./src
COPY ./tsconfig.json ./tsconfig.build.json ./
RUN npm run build

# runner

FROM mcr.microsoft.com/playwright:v1.42.1-jammy

RUN apt-get update && \
Expand All @@ -21,17 +11,18 @@ RUN apt-get update && \
libcurl4-openssl-dev \
python3

ENV NPM_CONFIG_CACHE=/tmp/.npm
RUN curl -fsSL https://bun.sh/install | bash
ENV PATH=$PATH:/root/.bun/bin

ARG FUNCTION_DIR="/function"
RUN mkdir -p ${FUNCTION_DIR}
WORKDIR ${FUNCTION_DIR}

COPY ./package.json ./
RUN npm install --production
RUN npm install aws-lambda-ric
COPY ./package.json ./bun.lockb ./
RUN bun install

COPY --from=build /app/dist ./
COPY lambda.js ./
COPY ./src ./src

ENTRYPOINT ["/usr/bin/npx", "aws-lambda-ric"]
CMD ["lambda.handler"]
CMD ["./lambda.handler"]
Binary file modified projects/scrape-txs/bun.lockb
Binary file not shown.
16 changes: 16 additions & 0 deletions projects/scrape-txs/lambda.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { spawn } from 'node:child_process';

export const handler = async () => {
const process = spawn('bun', ['./src/console.ts']);
process.stdout.on('data', (data) => {
console.log(data.toString());
});
process.stderr.on('data', (data) => {
console.error(data.toString());
});
await new Promise((resolve) => {
process.on('exit', () => {
resolve();
});
});
};
2 changes: 1 addition & 1 deletion projects/scrape-txs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"@tsconfig/node20": "~20.1.2",
"@tsconfig/strictest": "~2.0.3",
"@types/aws-lambda": "^8.10.136",
"@types/bun": "^1.0.8",
"@types/lodash": "^4.17.0",
"@types/node": "^20.11.20",
"@types/pg": "^8.11.2",
Expand All @@ -17,6 +16,7 @@
"typescript": "^5.4.2"
},
"dependencies": {
"aws-lambda-ric": "^3.1.0",
"commander": "^12.0.0",
"dayjs": "~1.11.10",
"decimal.js": "^10.4.3",
Expand Down
3 changes: 0 additions & 3 deletions projects/scrape-txs/src/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@ if (options.month) {
}
}

// // // console.log('config', config);

await bancoIndustrialScrape({
biConfig: config.banks.bancoIndustrialGt,
months,
isLambda: false,
});

// // await updateYnab({
Expand Down
26 changes: 0 additions & 26 deletions projects/scrape-txs/src/lambda.ts

This file was deleted.

23 changes: 11 additions & 12 deletions projects/scrape-txs/src/lib/banco-industrial/scrape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ function waitRandomMs() {
});
}

async function login(
auth: {
code: string;
username: string;
password: string;
},
isLambda: boolean
) {
const browser = isLambda
function isLambda() {
return !!process.env['AWS_LAMBDA_FUNCTION_NAME'];
}

async function login(auth: {
code: string;
username: string;
password: string;
}) {
const browser = isLambda()
? ((await launchChromium({
headless: true,
})) as Browser)
Expand Down Expand Up @@ -137,18 +138,16 @@ export type BiConfig = {
export async function bancoIndustrialScrape({
biConfig: { auth, accounts },
months,
isLambda,
}: {
biConfig: BiConfig;
months: dayjs.Dayjs[];
isLambda: boolean;
}) {
console.log(
`Scraping Banco Industrial GT transactions for months: ${months
.map((m) => m.format('YYYY-MM'))
.join(', ')}`
);
const ctx = await login(auth, isLambda);
const ctx = await login(auth);
try {
const bankTxs: InsertObject<DB, 'bank_txs'>[] = [];
for (const account of accounts) {
Expand Down
4 changes: 1 addition & 3 deletions projects/scrape-txs/src/lib/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Kysely, PostgresDialect, type InsertObject } from 'kysely';
import * as pg from 'pg';
import { Pool } from 'pg';
import type { DB } from './codegen';

export type { DB, InsertObject };

const { Pool } = pg;

export const db = new Kysely<DB>({
dialect: new PostgresDialect({
pool: new Pool({
Expand Down

0 comments on commit bad2056

Please sign in to comment.