Skip to content

Commit

Permalink
Fixing the build pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
dorukgezici committed Oct 13, 2024
1 parent 36adf07 commit 1973fc5
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 25 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/backend-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: "Backend: Lint & Test"

on:
push:
branches:
- main

jobs:
lint_and_test:
name: Lint & Test
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install uv
uses: astral-sh/setup-uv@v3

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version-file: "backend/.python-version"

- name: Install the project
run: uv sync --all-extras --dev

- name: Lint the project
run: uv run task lint

- name: Run tests
run: uv run task test
2 changes: 1 addition & 1 deletion .github/workflows/backend.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build & Deploy Backend
name: "Backend: Build & Deploy"

on: workflow_dispatch

Expand Down
24 changes: 11 additions & 13 deletions .github/workflows/prefect.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Deploy Prefect flow
name: "Prefect: Deploy Flow"

on:
push:
Expand All @@ -19,21 +19,21 @@ jobs:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"

- name: Setup Poetry
uses: abatilo/actions-poetry@v2

- name: Tailscale
uses: tailscale/github-action@v2
with:
oauth-client-id: ${{ secrets.TS_OAUTH_CLIENT_ID }}
oauth-secret: ${{ secrets.TS_OAUTH_SECRET }}
tags: tag:ci

- name: Install uv
uses: astral-sh/setup-uv@v3

- name: "Set up Python"
uses: actions/setup-python@v5
with:
python-version-file: "backend/.python-version"

- name: Prefect Deploy
env:
PREFECT_API_URL: ${{ secrets.PREFECT_API_URL }}
Expand All @@ -48,7 +48,5 @@ jobs:
SLACK_CHANNEL_ID: ${{ secrets.SLACK_CHANNEL_ID }}
run: |
cd backend
poetry config virtualenvs.create true --local
poetry config virtualenvs.in-project true --local
poetry install --no-interaction
poetry run prefect
uv sync --all-extras --dev
uv run prefect
2 changes: 1 addition & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ RUN uv sync --frozen

FROM base AS runtime
ENV PYTHONUNBUFFERED=1
COPY src/ ./src/
COPY subabot/ ./subabot/
CMD ["uv", "run", "start"]
2 changes: 1 addition & 1 deletion backend/Dockerfile.prefect
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ RUN uv sync --frozen

FROM base AS runtime
ENV PYTHONUNBUFFERED=1
COPY src/ ./src/
COPY subabot/ ./subabot/
CMD ["uv", "run", "start"]
2 changes: 1 addition & 1 deletion backend/prefect.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Generic metadata about this project
name: subabot
prefect-version: 3.0.0rc1
prefect-version: 3.0.9dev1

# build section allows you to manage and build docker images
build:
Expand Down
8 changes: 7 additions & 1 deletion backend/subabot/db.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from abc import ABC
from dataclasses import dataclass
from typing import Annotated

from fastapi import Depends
from sqlmodel import Session, create_engine, select

from subabot.utils import now_timestamp

engine = create_engine("sqlite:///database.db", connect_args={"check_same_thread": False})


Expand All @@ -16,7 +19,7 @@ def get_session():


@dataclass
class DBMixin:
class DBMixin(ABC):
key: str

@classmethod
Expand All @@ -36,6 +39,9 @@ def upsert(cls, key: str, **fields):
for key, value in fields.items():
setattr(obj, key, value)

if "updated_at" in getattr(cls, "model_fields"):
setattr(obj, "updated_at", now_timestamp())

session.add(obj)
session.commit()

Expand Down
6 changes: 2 additions & 4 deletions backend/subabot/rss/crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
from asyncer import asyncify
from fastapi.logger import logger
from feedparser import FeedParserDict, parse

from sqlmodel import select

from subabot.db import Session, engine
from subabot.rss.models import Crawl, Feed, History, Keyword, Search
from subabot.utils import now_timestamp
from subabot.rss.utils import find_matches, get_matching_entries
from subabot.utils import now_timestamp


async def crawl_feed(feed: Feed, keywords: Sequence[Keyword]) -> list[dict]:
Expand All @@ -35,15 +34,14 @@ async def crawl_feed(feed: Feed, keywords: Sequence[Keyword]) -> list[dict]:
keyword=keyword.value,
feed=feed.key,
paths=keyword_matches,
updated_at=now_timestamp(),
)

entries = get_matching_entries(entries, matches)
logger.debug(f"Found {len(entries)} new entries for {url}.")

for entry in entries:
if link := entry.get("link"):
History.upsert(link, updated_at=now_timestamp())
History.upsert(link)

return entries

Expand Down
8 changes: 5 additions & 3 deletions backend/subabot/rss/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from pydantic import model_validator
from slugify import slugify
from sqlmodel import JSON, Column, Field, SQLModel

from subabot.db import DBMixin
from subabot.utils import now_timestamp


class Keyword(SQLModel, DBMixin, table=True):
Expand Down Expand Up @@ -33,17 +35,17 @@ class Crawl(SQLModel, DBMixin, table=True):
key: str = Field(primary_key=True)
feed: dict = Field(sa_column=Column(JSON), default_factory=dict)
entries: list[dict] = Field(sa_column=Column(JSON), default_factory=list)
updated_at: int
updated_at: int = Field(default_factory=now_timestamp)


class Search(SQLModel, DBMixin, table=True):
key: str = Field(primary_key=True)
keyword: str
feed: str
paths: list[tuple] = Field(sa_column=Column(JSON), default_factory=list)
updated_at: int
updated_at: int = Field(default_factory=now_timestamp)


class History(SQLModel, DBMixin, table=True):
key: str = Field(primary_key=True)
updated_at: int
updated_at: int = Field(default_factory=now_timestamp)

0 comments on commit 1973fc5

Please sign in to comment.