Skip to content

Commit 3f2e176

Browse files
committed
docs(ci): add CircleCI worker guide + config — 3 approaches for running background workers
1 parent 3d5d4da commit 3f2e176

4 files changed

Lines changed: 1352 additions & 0 deletions

File tree

.circleci/config.yml

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
version: 2.1
2+
3+
# ──────────────────────────────────────────────────────────────
4+
# OpenCodeHub Worker - CircleCI Configuration
5+
# Run background workers (merge queue, mirror sync, cleanup)
6+
# using CircleCI infrastructure
7+
# ──────────────────────────────────────────────────────────────
8+
9+
executors:
10+
bun-executor:
11+
docker:
12+
- image: oven/bun:1
13+
resource_class: medium
14+
15+
# Self-hosted runner executor (recommended for production)
16+
# Replace 'your-namespace' with your actual CircleCI namespace
17+
och-runner:
18+
machine:
19+
image: ubuntu-2204:current
20+
resource_class: your-namespace/opencodehub-runner
21+
22+
# ── Jobs ─────────────────────────────────────────────────────
23+
24+
jobs:
25+
# ──────────────────────────────────────────────
26+
# Continuous Background Worker (Self-Hosted Runner)
27+
# ──────────────────────────────────────────────
28+
worker-continuous:
29+
executor: och-runner
30+
environment:
31+
NODE_ENV: production
32+
WORKER_INTERVAL: 5000
33+
WORKER_HEALTH_PORT: 9090
34+
WORKER_STALE_TIMEOUT: 300000
35+
steps:
36+
- checkout
37+
38+
- run:
39+
name: Install Bun
40+
command: |
41+
if ! command -v bun &> /dev/null; then
42+
curl -fsSL https://bun.sh/install | bash
43+
echo 'export PATH="$HOME/.bun/bin:$PATH"' >> $BASH_ENV
44+
fi
45+
source $BASH_ENV
46+
bun --version
47+
48+
- run:
49+
name: Install dependencies
50+
command: |
51+
for i in 1 2 3; do
52+
bun install --frozen-lockfile && exit 0
53+
echo "bun install attempt $i failed, retrying in 10s..."
54+
sleep 10
55+
done
56+
echo "::error::bun install failed after 3 attempts"
57+
exit 1
58+
59+
- run:
60+
name: Start OpenCodeHub Worker
61+
command: |
62+
echo "Starting OpenCodeHub background worker..."
63+
bun run scripts/worker.ts &
64+
WORKER_PID=$!
65+
echo "Worker PID: $WORKER_PID"
66+
67+
# Wait for health check
68+
for i in $(seq 1 30); do
69+
if curl -sf http://localhost:9090/health; then
70+
echo "✅ Worker is healthy"
71+
break
72+
fi
73+
echo "Waiting for worker to start... ($i/30)"
74+
sleep 2
75+
done
76+
77+
# Keep alive
78+
wait $WORKER_PID
79+
no_output_timeout: 24h
80+
81+
# ──────────────────────────────────────────────
82+
# Merge Queue Worker (Scheduled / Docker)
83+
# ──────────────────────────────────────────────
84+
merge-queue:
85+
executor: bun-executor
86+
environment:
87+
NODE_ENV: production
88+
WORKER_INTERVAL: 5000
89+
WORKER_STALE_TIMEOUT: 300000
90+
steps:
91+
- checkout
92+
93+
- run:
94+
name: Install dependencies
95+
command: |
96+
for i in 1 2 3; do
97+
bun install --frozen-lockfile && exit 0
98+
echo "bun install attempt $i failed, retrying in 10s..."
99+
sleep 10
100+
done
101+
echo "::error::bun install failed after 3 attempts"
102+
exit 1
103+
104+
- run:
105+
name: Process Merge Queue
106+
command: |
107+
echo "Running merge queue processor for 4 hours..."
108+
timeout 14400 bun run scripts/worker.ts || true
109+
echo "Merge queue cycle complete."
110+
no_output_timeout: 4h
111+
112+
# ──────────────────────────────────────────────
113+
# Mirror Sync (Scheduled)
114+
# ──────────────────────────────────────────────
115+
mirror-sync:
116+
executor: bun-executor
117+
environment:
118+
NODE_ENV: production
119+
steps:
120+
- checkout
121+
122+
- run:
123+
name: Install dependencies
124+
command: bun install --frozen-lockfile
125+
126+
- run:
127+
name: Sync Mirrors
128+
command: bun run scripts/worker.ts --task=mirror-sync || true
129+
130+
# ──────────────────────────────────────────────
131+
# Cleanup (Scheduled)
132+
# ──────────────────────────────────────────────
133+
cleanup:
134+
executor: bun-executor
135+
environment:
136+
NODE_ENV: production
137+
steps:
138+
- checkout
139+
140+
- run:
141+
name: Install dependencies
142+
command: bun install --frozen-lockfile
143+
144+
- run:
145+
name: Run Cleanup Tasks
146+
command: bun run scripts/worker.ts --task=cleanup || true
147+
148+
# ──────────────────────────────────────────────
149+
# Worker Health Check
150+
# ──────────────────────────────────────────────
151+
health-check:
152+
executor: bun-executor
153+
steps:
154+
- run:
155+
name: Check Worker Health
156+
command: |
157+
curl -sf http://your-worker-host:9090/health || exit 1
158+
echo "✅ Worker is healthy"
159+
160+
# ── Workflows ────────────────────────────────────────────────
161+
162+
workflows:
163+
version: 2
164+
165+
# OPTION A: Continuous worker (requires self-hosted runner)
166+
# Uncomment this if you have a self-hosted runner
167+
# worker-continuous:
168+
# jobs:
169+
# - worker-continuous:
170+
# filters:
171+
# branches:
172+
# only:
173+
# - main
174+
175+
# OPTION B: Scheduled worker tasks (no self-hosted runner needed)
176+
# Runs merge queue every 4 hours
177+
scheduled-merge-queue:
178+
triggers:
179+
- schedule:
180+
cron: "0 */4 * * *"
181+
filters:
182+
branches:
183+
only:
184+
- main
185+
jobs:
186+
- merge-queue
187+
188+
# Runs mirror sync every hour
189+
scheduled-mirror-sync:
190+
triggers:
191+
- schedule:
192+
cron: "0 * * * *"
193+
filters:
194+
branches:
195+
only:
196+
- main
197+
jobs:
198+
- mirror-sync
199+
200+
# Runs cleanup daily at 3 AM UTC
201+
scheduled-cleanup:
202+
triggers:
203+
- schedule:
204+
cron: "0 3 * * *"
205+
filters:
206+
branches:
207+
only:
208+
- main
209+
jobs:
210+
- cleanup
211+
212+
# Health check every hour
213+
scheduled-health:
214+
triggers:
215+
- schedule:
216+
cron: "0 * * * *"
217+
filters:
218+
branches:
219+
only:
220+
- main
221+
jobs:
222+
- health-check

0 commit comments

Comments
 (0)