-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: telegram / cron running on seperate enviroments (#3)
* docker support * rico cron oh yeah * funciona el docking * schedule cron on ci + telegram on seperate file * docs: docker instructions
- Loading branch information
1 parent
2aad31d
commit 302743b
Showing
15 changed files
with
151 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
node_modules | ||
Dockerfile* | ||
docker-compose* | ||
.dockerignore | ||
.git | ||
.gitignore | ||
README.md | ||
LICENSE | ||
.vscode | ||
Makefile | ||
helm-charts | ||
.env | ||
.editorconfig | ||
.idea | ||
coverage* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,9 @@ on: | |
push: | ||
branches: | ||
- main | ||
schedule: # daily at 8 am | ||
- cron: "0 8 * * *" | ||
|
||
jobs: | ||
scrape: | ||
runs-on: ubuntu-latest | ||
|
@@ -15,7 +18,7 @@ jobs: | |
- uses: oven-sh/[email protected] | ||
- name: Install dependencies | ||
run: bun install | ||
- name: Install browser | ||
run: bunx playwright install --with-deps chromium | ||
# - name: Install browser | ||
# run: bunx playwright install --with-deps chromium | ||
- name: execute | ||
run: bun run src/main.ts | ||
run: bun run src/cron.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
FROM oven/bun AS build | ||
|
||
WORKDIR /app | ||
|
||
COPY bun.lockb . | ||
COPY package.json . | ||
|
||
RUN bun install --frozen-lockfile | ||
|
||
COPY src ./src | ||
|
||
# compile everything to a binary called cli which includes the bun runtime | ||
RUN bun build ./src/server.ts --compile --outfile cli | ||
|
||
# use a smaller image without bun | ||
FROM ubuntu:22.04 | ||
|
||
WORKDIR /app | ||
|
||
# copy the compiled binary from the build image | ||
COPY --from=build /app/cli /app/cli | ||
|
||
# execute the binary! | ||
CMD ["/app/cli"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,19 @@ | ||
{ | ||
"scripts": { | ||
"start": "tsx src/index.ts" | ||
"start": "bun --env-file=.env src/server.ts" | ||
}, | ||
"dependencies": { | ||
"@libsql/client": "^0.3.6", | ||
"croner": "^9.0.0", | ||
"eslint-plugin-neverthrow": "^1.1.4", | ||
"grammy": "^1.32.0", | ||
"neverthrow": "^8.1.0", | ||
"pino": "^9.5.0", | ||
"playwright": "^1.39.0", | ||
"zod": "^3.22.4" | ||
}, | ||
"devDependencies": { | ||
"tsx": "^3.12.2" | ||
} | ||
}, | ||
"packageManager": "^[email protected]" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { save } from "./lib/save"; | ||
import { getGetonboardJobs } from "./sources/getonboard"; | ||
|
||
// TODO: normalize for multiple sources and use neverthrow | ||
async function cron() { | ||
const [getonboard] = await Promise.allSettled([getGetonboardJobs()]); | ||
|
||
if (getonboard.status === "rejected") | ||
console.error(`couldn't steal from getonboard ${getonboard.reason}`); | ||
|
||
const jobs = [...(getonboard.status === "fulfilled" ? getonboard.value : [])]; | ||
|
||
if (jobs.length === 0) { | ||
console.info("no new jobs found"); | ||
return; | ||
} | ||
|
||
const results = await save(jobs); | ||
if (results.isErr()) { | ||
console.error(`coudn't save new jobs, ${results.error}`); | ||
} else { | ||
console.info( | ||
"saved " + | ||
results.value.fulfilled + | ||
", " + | ||
results.value.rejected + | ||
" failed" | ||
); | ||
} | ||
return; | ||
} | ||
|
||
cron(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Bot } from "grammy"; | ||
import { ResultAsync } from "neverthrow"; | ||
import { jobSchema } from "../sources/schema"; | ||
import { save } from "./save"; | ||
|
||
const TOKEN = process.env.TELEGRAM_BOT_TOKEN; | ||
export async function unsafeDCCListener() { | ||
if (!TOKEN) throw new Error("no telegram bot token found in .env"); | ||
const bot = new Bot(TOKEN); | ||
bot.on("channel_post", (ctx) => { | ||
if (ctx.chat.title === "DCCEmpleo") { | ||
const job = jobSchema.safeParse({ | ||
id: `${ctx.channelPost.message_id}_DCCEmpleo`, | ||
content: ctx.channelPost.text, | ||
date: new Date( | ||
new Date().toLocaleString("en-US", { timeZone: "America/Santiago" }) | ||
), | ||
source: "DCC_TELEGRAM", | ||
}); | ||
|
||
if (job.success) { | ||
save([job.data]); | ||
} else { | ||
console.error(job.error); | ||
} | ||
} | ||
}); | ||
|
||
await bot.start({ | ||
onStart() { | ||
console.info("telegram bot is runnning"); | ||
}, | ||
}); | ||
} | ||
|
||
export const dccListener = ResultAsync.fromThrowable( | ||
unsafeDCCListener, | ||
(error) => (error instanceof Error ? error : new Error("Unknown error")) | ||
); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { dccListener } from "./lib/telegram"; | ||
|
||
async function server() { | ||
console.log("runtime: " + process.release.name); | ||
const listener = await dccListener(); | ||
if (listener.isErr()) { | ||
console.error(listener.error.message); | ||
return; | ||
} | ||
} | ||
|
||
server(); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters