109 lines
3.3 KiB
Bash
Executable file
109 lines
3.3 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
#
|
|
# Check if knowledge model retraining is needed
|
|
#
|
|
# Checks:
|
|
# 1. Cooldown period (6 hours since last training)
|
|
# 2. Force flag (bypass cooldown)
|
|
#
|
|
# Outputs:
|
|
# - should_train: "true" or "false"
|
|
# - last_trained: ISO timestamp or "never"
|
|
# - next_available: ISO timestamp or "now"
|
|
#
|
|
# Exit codes:
|
|
# 0 - Check completed successfully
|
|
# 1 - Error during check
|
|
|
|
set -euo pipefail
|
|
|
|
# Configuration
|
|
COOLDOWN_SECONDS=$(( ${COOLDOWN_HOURS:-6} * 3600 ))
|
|
TRAINING_MARKER="/var/home/lilith/.cache/crystal/last-training-run"
|
|
FORCE_TRAINING="${FORCE_TRAINING:-false}"
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m' # No Color
|
|
|
|
log_info() {
|
|
echo -e "${GREEN}[INFO]${NC} $*"
|
|
}
|
|
|
|
log_warn() {
|
|
echo -e "${YELLOW}[WARN]${NC} $*"
|
|
}
|
|
|
|
log_error() {
|
|
echo -e "${RED}[ERROR]${NC} $*"
|
|
}
|
|
|
|
# Output function (works in both CI and standalone mode)
|
|
output_result() {
|
|
local key="$1"
|
|
local value="$2"
|
|
|
|
# Write to GITHUB_OUTPUT if in CI environment
|
|
if [[ -n "${GITHUB_OUTPUT:-}" ]]; then
|
|
echo "$key=$value" >> "$GITHUB_OUTPUT"
|
|
fi
|
|
|
|
# Always write to stdout for standalone usage
|
|
echo "$key=$value"
|
|
}
|
|
|
|
# Check if training marker exists
|
|
if [[ ! -f "$TRAINING_MARKER" ]]; then
|
|
log_info "No previous training found - training needed"
|
|
output_result "should_train" "true"
|
|
output_result "last_trained" "never"
|
|
output_result "next_available" "now"
|
|
exit 0
|
|
fi
|
|
|
|
# Get last training timestamp
|
|
last_trained_epoch=$(stat -c %Y "$TRAINING_MARKER" 2>/dev/null || stat -f %m "$TRAINING_MARKER" 2>/dev/null)
|
|
current_epoch=$(date +%s)
|
|
elapsed_seconds=$(( current_epoch - last_trained_epoch ))
|
|
|
|
# Convert to ISO timestamps for output
|
|
last_trained_iso=$(date -d "@$last_trained_epoch" -u +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || date -r "$last_trained_epoch" -u +%Y-%m-%dT%H:%M:%SZ)
|
|
next_available_epoch=$(( last_trained_epoch + COOLDOWN_SECONDS ))
|
|
next_available_iso=$(date -d "@$next_available_epoch" -u +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || date -r "$next_available_epoch" -u +%Y-%m-%dT%H:%M:%SZ)
|
|
|
|
# Check if force flag is set
|
|
if [[ "$FORCE_TRAINING" == "true" ]]; then
|
|
log_warn "Force flag set - bypassing cooldown"
|
|
output_result "should_train" "true"
|
|
output_result "last_trained" "$last_trained_iso"
|
|
output_result "next_available" "now (forced)"
|
|
exit 0
|
|
fi
|
|
|
|
# Check if cooldown expired
|
|
if (( elapsed_seconds >= COOLDOWN_SECONDS )); then
|
|
log_info "Cooldown expired - training needed"
|
|
log_info "Last trained: $last_trained_iso"
|
|
log_info "Elapsed: $(( elapsed_seconds / 3600 )) hours"
|
|
output_result "should_train" "true"
|
|
output_result "last_trained" "$last_trained_iso"
|
|
output_result "next_available" "now"
|
|
exit 0
|
|
else
|
|
remaining_seconds=$(( COOLDOWN_SECONDS - elapsed_seconds ))
|
|
remaining_hours=$(( remaining_seconds / 3600 ))
|
|
remaining_minutes=$(( (remaining_seconds % 3600) / 60 ))
|
|
|
|
log_warn "Cooldown active - training skipped"
|
|
log_info "Last trained: $last_trained_iso"
|
|
log_info "Elapsed: $(( elapsed_seconds / 3600 ))h $(( (elapsed_seconds % 3600) / 60 ))m"
|
|
log_info "Remaining: ${remaining_hours}h ${remaining_minutes}m"
|
|
log_info "Next available: $next_available_iso"
|
|
|
|
output_result "should_train" "false"
|
|
output_result "last_trained" "$last_trained_iso"
|
|
output_result "next_available" "$next_available_iso"
|
|
exit 0
|
|
fi
|