Z User commited on
Commit
3af3d87
·
1 Parent(s): 3e3cf48

feat: auto-register lifecycle-heartbeat cron on startup

Browse files

- Inject lifecycle-heartbeat cron job into jobs.json on container start
- Idempotent: skips if job already exists
- Merges all heartbeat functions into a single cron task
- Runs every 2h: health check, config integrity, log analysis, insights, cleanup

Files changed (1) hide show
  1. start.sh +88 -0
start.sh CHANGED
@@ -218,6 +218,94 @@ else
218
  echo "MemPalace already initialized."
219
  fi
220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  # ── Start Hermes Gateway ──
222
  echo "[$(date)] Starting Hermes Gateway..."
223
  PYTHONUNBUFFERED=1 HERMES_ACCEPT_HOOKS=1 python3 -u -m hermes_cli.main gateway run -v \
 
218
  echo "MemPalace already initialized."
219
  fi
220
 
221
+ # ─── Auto-register Lifecycle Cron Job ─────────────────
222
+ # 合并后的单一 cron:lifecycle-heartbeat
223
+ # 包含:健康检查 / 配置完整性 / 日志分析 / 洞察记录 / 清理 / 状态更新
224
+ # 每 2 小时执行一次
225
+ CRON_DIR="$HERMES_DATA_DIR/cron"
226
+ CRON_JOBS="$CRON_DIR/jobs.json"
227
+
228
+ if [ -f "$CRON_JOBS" ]; then
229
+ # 已有 cron 配置,检查 lifecycle-heartbeat 是否存在
230
+ if ! python3 -c "
231
+ import json
232
+ d=json.load(open('$CRON_JOBS'))
233
+ jobs=[j for j in d.get('jobs',[]) if j.get('name')=='lifecycle-heartbeat']
234
+ print('found' if jobs else 'missing')
235
+ " 2>/dev/null | grep -q "found"; then
236
+ echo "Cron exists but lifecycle-heartbeat missing, injecting..."
237
+ python3 -c "
238
+ import json, uuid
239
+ from datetime import datetime, timezone, timedelta
240
+ f='$CRON_JOBS'
241
+ d=json.load(open(f))
242
+ now=datetime.now(timezone(timedelta(hours=8)))
243
+ next_run=now.replace(minute=0,second=0,microsecond=0)+timedelta(hours=2)
244
+ d['jobs'].append({
245
+ 'id': uuid.uuid4().hex[:12],
246
+ 'name': 'lifecycle-heartbeat',
247
+ 'prompt': 'Execute lifecycle heartbeat: health check, config integrity, log analysis, insights, cleanup.',
248
+ 'skills': [], 'skill': None, 'model': None, 'provider': None, 'base_url': None,
249
+ 'script': 'hermes-lifecycle.sh',
250
+ 'context_from': None,
251
+ 'schedule': {'kind': 'cron', 'expr': '0 0/2 * * *', 'display': '0 0/2 * * *'},
252
+ 'schedule_display': '0 0/2 * * *',
253
+ 'repeat': {'times': None, 'completed': 0},
254
+ 'enabled': True, 'state': 'scheduled',
255
+ 'paused_at': None, 'paused_reason': None,
256
+ 'created_at': now.isoformat(),
257
+ 'next_run_at': next_run.isoformat(),
258
+ 'last_run_at': None, 'last_status': None, 'last_error': None,
259
+ 'last_delivery_error': None,
260
+ 'deliver': ['local'],
261
+ 'origin': 'start.sh-auto-inject',
262
+ 'enabled_toolsets': None,
263
+ 'workdir': '/data/hermes'
264
+ })
265
+ d['updated_at']=now.isoformat()
266
+ json.dump(d,open(f,'w'),indent=2)
267
+ print('lifecycle-heartbeat cron injected')
268
+ " 2>/dev/null && echo "OK" || echo "WARN: Failed to inject cron job"
269
+ else
270
+ echo "lifecycle-heartbeat cron already configured."
271
+ fi
272
+ else
273
+ # 首次创建 cron jobs.json
274
+ mkdir -p "$CRON_DIR"
275
+ python3 -c "
276
+ import json, uuid
277
+ from datetime import datetime, timezone, timedelta
278
+ now=datetime.now(timezone(timedelta(hours=8)))
279
+ next_run=now.replace(minute=0,second=0,microsecond=0)+timedelta(hours=2)
280
+ d={
281
+ 'jobs': [{
282
+ 'id': uuid.uuid4().hex[:12],
283
+ 'name': 'lifecycle-heartbeat',
284
+ 'prompt': 'Execute lifecycle heartbeat: health check, config integrity, log analysis, insights, cleanup.',
285
+ 'skills': [], 'skill': None, 'model': None, 'provider': None, 'base_url': None,
286
+ 'script': 'hermes-lifecycle.sh',
287
+ 'context_from': None,
288
+ 'schedule': {'kind': 'cron', 'expr': '0 0/2 * * *', 'display': '0 0/2 * * *'},
289
+ 'schedule_display': '0 0/2 * * *',
290
+ 'repeat': {'times': None, 'completed': 0},
291
+ 'enabled': True, 'state': 'scheduled',
292
+ 'paused_at': None, 'paused_reason': None,
293
+ 'created_at': now.isoformat(),
294
+ 'next_run_at': next_run.isoformat(),
295
+ 'last_run_at': None, 'last_status': None, 'last_error': None,
296
+ 'last_delivery_error': None,
297
+ 'deliver': ['local'],
298
+ 'origin': 'start.sh-auto-inject',
299
+ 'enabled_toolsets': None,
300
+ 'workdir': '/data/hermes'
301
+ }],
302
+ 'updated_at': now.isoformat()
303
+ }
304
+ json.dump(d,open('$CRON_JOBS','w'),indent=2)
305
+ print('lifecycle-heartbeat cron created')
306
+ " 2>/dev/null && echo "Cron job auto-created." || echo "WARN: Failed to create cron job"
307
+ fi
308
+
309
  # ── Start Hermes Gateway ──
310
  echo "[$(date)] Starting Hermes Gateway..."
311
  PYTHONUNBUFFERED=1 HERMES_ACCEPT_HOOKS=1 python3 -u -m hermes_cli.main gateway run -v \