Skip to content

Commit a03afc7

Browse files
committed
refactor: Simplify blog sync process and improve task handling
- Updated the Makefile to streamline the blog sync command, removing unnecessary environment variable checks and directly invoking a Python script for execution. - Refactored the `trigger_blog_sync_cron.py` script to utilize asynchronous task handling, allowing for inline execution when requested and queuing via Celery otherwise. - Enhanced the `trigger_bridge_sync_cron.py` script to avoid public self-HTTP calls, directly enqueuing tasks for bridge synchronization. These changes improve the efficiency and clarity of the blog and bridge sync processes, ensuring better task management and execution.
1 parent a80560d commit a03afc7

3 files changed

Lines changed: 44 additions & 53 deletions

File tree

Makefile

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -970,29 +970,15 @@ kamal-clear-cache: ## Clear remote cache on Kamal (KAMAL_CACHE_TYPE=search|resou
970970
# make kamal-blog-sync # queue via Celery (RUN_NOW defaults false)
971971
# make kamal-blog-sync RUN_NOW=1 # run inline via FastAPI
972972
kamal-blog-sync: ## Trigger home page blog sync on Kamal (RUN_NOW=1 for inline)
973-
@echo "Triggering GIN blog sync on Kamal via $(KAMAL_API_URL)... (RUN_NOW=$(RUN_NOW))"
973+
@echo "Triggering GIN blog sync on Kamal... (RUN_NOW=$(RUN_NOW))"
974974
@if [ -z "$$KAMAL_SSH_USER" ] || [ -z "$$KAMAL_HOST" ]; then \
975975
echo "ERROR: KAMAL_SSH_USER and KAMAL_HOST environment variables must be set."; \
976976
echo "Use KAMAL_DEST=dev1 or dev2. Ensure .kamal/secrets-common and .kamal/secrets.dev1 (or .secrets.dev2) exist."; \
977977
exit 1; \
978978
fi
979979
@kamal app exec -d $(KAMAL_DEST) --roles $(KAMAL_APP_ROLE) "bash -lc '\
980-
ADMIN_USER=\$${ADMIN_USERNAME:-admin}; \
981-
ADMIN_PASS=\$${ADMIN_PASSWORD:-changeme}; \
982-
API_BASE=\"$(KAMAL_API_URL)\"; \
983-
if [ -z \"\$$API_BASE\" ]; then API_BASE=\"\$$APPLICATION_URL\"; fi; \
984-
if [ -z \"\$$API_BASE\" ]; then echo \"ERROR: KAMAL_API_URL or APPLICATION_URL must be set.\"; exit 1; fi; \
985-
API_BASE=\"\$${API_BASE%/}\"; \
986-
RUN_NOW_FLAG=\"$(RUN_NOW)\"; \
987-
if [ \"\$$RUN_NOW_FLAG\" = \"1\" ] || [ \"\$$RUN_NOW_FLAG\" = \"true\" ] || [ \"\$$RUN_NOW_FLAG\" = \"TRUE\" ]; then \
988-
BODY='\''{\"run_now\": true}'\''; \
989-
else \
990-
BODY='\''{\"run_now\": false}'\''; \
991-
fi; \
992-
curl -fsS -u \"\$$ADMIN_USER:\$$ADMIN_PASS\" -X POST \
993-
\"\$$API_BASE/api/v1/admin/home/blog/sync\" \
994-
-H \"Content-Type: application/json\" \
995-
-d \"\$$BODY\"'"
980+
export RUN_NOW=\"$(RUN_NOW)\"; \
981+
/opt/venv/bin/python3 /app/scripts/trigger_blog_sync_cron.py'"
996982
@echo
997983
@echo "GIN blog sync request submitted (Kamal)."
998984

backend/scripts/trigger_blog_sync_cron.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
from __future__ import annotations
22

3+
import asyncio
4+
import json
35
import os
46

5-
import requests
7+
from app.tasks.gin_blog_sync import gin_blog_sync, run_gin_blog_sync
8+
9+
10+
def _env_truthy(name: str) -> bool:
11+
value = os.getenv(name, "")
12+
return value.lower() in {"1", "true", "yes", "on"}
613

714

815
def main() -> None:
9-
admin_username = os.getenv("ADMIN_USERNAME", "admin")
10-
admin_password = os.getenv("ADMIN_PASSWORD", "changeme")
11-
application_url = os.getenv("APPLICATION_URL", "").rstrip("/")
12-
13-
if not application_url:
14-
raise RuntimeError("APPLICATION_URL is required")
15-
16-
# Enqueue the Celery job (equivalent to make blog-sync with RUN_NOW omitted/false).
17-
url = f"{application_url}/api/v1/admin/home/blog/sync"
18-
resp = requests.post(
19-
url,
20-
json={"run_now": False},
21-
auth=(admin_username, admin_password),
22-
timeout=60,
23-
)
24-
resp.raise_for_status()
25-
print(resp.text)
16+
run_now = _env_truthy("RUN_NOW")
17+
18+
# Avoid public self-HTTP calls from Kamal containers. Enqueue via Celery directly,
19+
# or run the sync inline when RUN_NOW is requested by the Make target.
20+
if run_now:
21+
result = asyncio.run(run_gin_blog_sync())
22+
print(json.dumps({"queued": "inline", "result": result}, default=str))
23+
return
24+
25+
task = gin_blog_sync.delay()
26+
print(json.dumps({"queued": "gin_blog_sync", "task_id": task.id}))
2627

2728

2829
if __name__ == "__main__":

backend/scripts/trigger_bridge_sync_cron.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from __future__ import annotations
22

3+
import json
34
import os
45
from datetime import datetime, timedelta, timezone
56

6-
import requests
7+
from app.tasks.bridge_sync import bridge_sync_all
78

89

910
def _previous_utc_day_start_iso_z() -> str:
@@ -28,25 +29,28 @@ def _previous_utc_day_start_iso_z() -> str:
2829

2930

3031
def main() -> None:
31-
admin_username = os.getenv("ADMIN_USERNAME", "admin")
32-
admin_password = os.getenv("ADMIN_PASSWORD", "changeme")
33-
application_url = os.getenv("APPLICATION_URL", "").rstrip("/")
3432
bridge_trigger = os.getenv("BRIDGE_TRIGGER", "manual")
35-
36-
if not application_url:
37-
raise RuntimeError("APPLICATION_URL is required")
38-
3933
changed_since = os.getenv("CHANGED_SINCE") or _previous_utc_day_start_iso_z()
40-
41-
url = f"{application_url}/api/v1/admin/bridge/sync"
42-
payload: dict[str, object] = {"bridge_trigger": bridge_trigger, "changed_since": changed_since}
43-
44-
# Basic Auth via (user, pass) tuple (FastAPI/HTTPBasic).
45-
resp = requests.post(url, json=payload, auth=(admin_username, admin_password), timeout=60)
46-
resp.raise_for_status()
47-
48-
# Cron logs should be readable from `docker logs`; print response body.
49-
print(resp.text)
34+
limit_raw = os.getenv("BRIDGE_LIMIT", "").strip()
35+
limit = int(limit_raw) if limit_raw else None
36+
37+
# Avoid public self-HTTP calls from Kamal containers; enqueue the worker task directly.
38+
task = bridge_sync_all.delay(
39+
trigger=bridge_trigger,
40+
limit=limit,
41+
changed_since=changed_since,
42+
)
43+
print(
44+
json.dumps(
45+
{
46+
"queued": "kithe_bridge",
47+
"task_id": task.id,
48+
"bridge_trigger": bridge_trigger,
49+
"limit": limit,
50+
"changed_since": changed_since,
51+
}
52+
)
53+
)
5054

5155

5256
if __name__ == "__main__":

0 commit comments

Comments
 (0)