Giám sát Cron Jobs#
Mục tiêu: DevOps monitor + troubleshoot các cron jobs và Asynq queue của T-Fluencers backend.
Mục tiêu#
- Xem trạng thái các job (pending, active, success, fail).
- Xử lý job fail/stuck.
- Trigger manual khi cần.
- Setup alert khi queue backlog lớn.
Điều kiện tiên quyết#
- Access Asynq dashboard (URL do Infra cung cấp).
- SSH / kubectl access vào worker pods (nếu cần).
Các job chính#
1. MXH Crawlers#
- Job name:
crawl_tiktok,crawl_youtube,crawl_facebook. - Tần suất: 1–6h (tùy config).
- Mục đích: update stats bài dự thi.
- Failure modes: API rate limit, token expired, network timeout.
2. Snapshot Jobs#
- Daily Snapshot:
snapshot_daily— 2 AM mỗi ngày. - Post-expiry Snapshot:
snapshot_postexpiry— trigger khi event hết hạn. - Makeup Crawl: manual trigger.
3. Reward Processing#
- Running: trigger khi Admin click Running trên đợt đối soát.
- Budget check: mỗi 15 phút — check limit của từng campaign.
4. Segment Match#
- Job:
segment_automatic_match— mỗi giờ. - Cập nhật user vào segment AUTOMATIC.
5. Score Calculation#
- Job:
creator_score_daily— daily.
6. Notification Dispatch#
- Job:
notification_send— trigger on-demand.
7. Transfer Sync#
- Job:
transfer_sync_tcb— poll status từ Techcombank API.
Các bước thực hiện#
1. Check queue status#
- Mở Asynq Dashboard.
- Xem queues:
- default — general jobs.
- critical — priority high (reward processing, transfer).
- scheduled — delayed jobs.
- retry — jobs đang retry.
- archived — fail vĩnh viễn.
2. Xem active jobs#
Tab Active → list job đang chạy.
- Status: processing, duration.
- Worker handling.
3. Xem failed jobs#
Tab Archived / Retry:
- Job name, payload, error message, retry count.
- Action: Retry hoặc Delete.
4. Trigger manual#
2 cách:
A. Qua Asynq Dashboard (nếu support):
- Select job type → Enqueue manual.
B. Qua backend command:
# SSH vào backend pod hoặc local
go run cmd/worker/main.go enqueue snapshot_daily
Hoặc qua admin API (nếu expose):
curl -X POST https://api.internal/jobs/trigger \
-H "Authorization: Bearer ..." \
-d '{"job": "snapshot_daily"}'
5. Check logs#
# Kubernetes
kubectl logs -l app=tf-worker --tail=100 -f
# Docker Compose
docker compose logs -f worker
# Với filter
kubectl logs ... | grep "snapshot_daily"
6. Setup alert#
Grafana/Prometheus metrics:
asynq_pending > threshold→ alert.asynq_failed_rate > 5%→ alert.worker_active = 0(should be > 0) → urgent alert.
Chi tiết từng bước#
Retry policy#
Mặc định Asynq retry 25 lần với exponential backoff. Sau đó → archived.
Để change: tại job definition:
asynq.NewTask("snapshot_daily", payload,
asynq.MaxRetry(10),
asynq.Timeout(30*time.Minute),
)
Stuck jobs#
Job ở Active quá lâu (> timeout):
- Có thể worker crash.
- Restart worker pod.
- Asynq sẽ pickup job ở active → retry.
Queue priority#
Nếu backlog lớn ở default, nên:
- Move job critical sang queue critical.
- Scale up workers.
Maintenance endpoints (OpsHub)#
# Rerun AI
curl -X POST https://opshub-api.diso.vn/api/v1/maintenance/rerun-ai \
-H "Authorization: Bearer <token>" \
-d '{"apply": true, "campaign_id": "<id>", "failed_check_ids": ["AI25_xxx"]}'
# Redispatch verdict
curl -X POST https://opshub-api.diso.vn/api/v1/maintenance/redispatch-verdict \
-H "Authorization: Bearer <token>" \
-d '{"apply": false, "project": "tfluencers", "campaign_id": "<id>", "outcome": "auto_approved"}'
Lỗi thường gặp#
1. Crawl MXH fail rate tăng#
- Token expired: TikTok/YouTube token cần refresh. Check secret rotation.
- Rate limit: throttle crawl rate hoặc upgrade API tier.
- API change: MXH đổi API → cần update code.
2. Snapshot daily không chạy#
- Cron schedule sai.
- Worker không up.
- Check log
cron_schedulerservice.
3. Reward job stuck ở "Running"#
- Worker crash giữa chừng.
- Manual: inspect đợt đối soát, xem reconciliation-item nào đã cộng, đâu chưa.
- Resume từ item chưa xong.
4. Notification queue đầy#
- Bulk send lớn (segment 100k+).
- Scale worker temporarily.
- Throttle rate nếu push provider rate limit.
5. Transfer sync delay#
- Techcombank API chậm / downtime.
- Check với TCB team.
- Retry logic đảm bảo data converge cuối cùng.
Câu hỏi thường gặp (FAQ)#
Asynq có UI giống Bull không?
Có, Asynq có asynqmon dashboard tương tự Bull Board.
Job nào priority cao nhất?
- Reward running (tiền không thể sai).
- Transfer sync (phản hồi user rút tiền).
- Notification time-sensitive.
Crash recovery thế nào?
Asynq + Redis persist → jobs không mất khi worker restart. Stuck job tự pickup sau timeout.
Backup queue data?
Redis RDB/AOF backup định kỳ (Infra setup).
Monitoring tool?
- Prometheus cho metrics.
- Grafana dashboards.
- Sentry cho error tracking.
- Logs tập trung ở ELK/Loki.
Liên kết liên quan#
Tham khảo SRS: background-system/01-background-jobs