← back to Daily Reporting

setup-cron.sh

174 lines

#!/bin/bash
#
# Setup Cron Jobs for Daily Reporting
#
# This script configures automated daily reports:
# - Morning standup at 6:00 AM
# - End-of-day summary at 5:00 PM
#

set -e

echo "=============================================="
echo "🤖 DW-AGENTS DAILY REPORTING CRON SETUP"
echo "=============================================="
echo ""

# Check if running as root
if [ "$EUID" -eq 0 ]; then
  echo "⚠️  Warning: Running as root. Cron will be set up for root user."
  echo ""
fi

# Paths
SKILL_DIR="/root/.claude/skills/daily-reporting"
TSX_PATH="/root/DW-Agents/node_modules/.bin/tsx"
RUNNER_SCRIPT="$SKILL_DIR/daily-report-runner.ts"
LOG_DIR="/root/DW-Agents/logs"

# Verify files exist
echo "📋 Checking prerequisites..."

if [ ! -f "$TSX_PATH" ]; then
  echo "❌ Error: tsx not found at $TSX_PATH"
  echo "   Please install dependencies: cd /root/DW-Agents && npm install"
  exit 1
fi

if [ ! -f "$RUNNER_SCRIPT" ]; then
  echo "❌ Error: Runner script not found at $RUNNER_SCRIPT"
  exit 1
fi

echo "✅ All prerequisites found"
echo ""

# Create log directory
mkdir -p "$LOG_DIR"

# Check if SLACK_WEBHOOK_URL is set
echo "🔍 Checking Slack configuration..."
if [ -z "$SLACK_WEBHOOK_URL" ]; then
  echo "⚠️  Warning: SLACK_WEBHOOK_URL not set in environment"
  echo "   Reports will be saved to files only (no Slack notifications)"
  echo ""
  read -p "Do you want to configure Slack webhook now? (y/N) " -n 1 -r
  echo ""
  if [[ $REPLY =~ ^[Yy]$ ]]; then
    read -p "Enter Slack webhook URL: " WEBHOOK_URL

    # Add to .bashrc for persistence
    if ! grep -q "SLACK_WEBHOOK_URL" ~/.bashrc; then
      echo "" >> ~/.bashrc
      echo "# DW-Agents Daily Reporting Slack Configuration" >> ~/.bashrc
      echo "export SLACK_WEBHOOK_URL=\"$WEBHOOK_URL\"" >> ~/.bashrc
      echo "export SLACK_CHANNEL=\"#morning-report\"" >> ~/.bashrc
      echo "✅ Added SLACK_WEBHOOK_URL to ~/.bashrc"
    fi

    export SLACK_WEBHOOK_URL="$WEBHOOK_URL"
    export SLACK_CHANNEL="${SLACK_CHANNEL:-#morning-report}"

    echo "✅ Slack webhook configured"
    echo ""
  fi
else
  echo "✅ Slack webhook found: ${SLACK_WEBHOOK_URL:0:50}..."
  echo "   Channel: ${SLACK_CHANNEL:-#morning-report}"
  echo ""
fi

# Create cron entries
echo "📅 Setting up cron jobs..."
echo ""

# Backup existing crontab
crontab -l > /tmp/crontab.backup 2>/dev/null || true

# Remove any existing daily-reporting cron jobs
crontab -l 2>/dev/null | grep -v "daily-report-runner.ts" > /tmp/crontab.new || true

# Add environment variables export
cat >> /tmp/crontab.new << EOF

# DW-Agents Daily Reporting System
# Generated on $(date)
SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL:-}
SLACK_CHANNEL=${SLACK_CHANNEL:-#morning-report}
REPORTS_DIR=$LOG_DIR

# Morning standup report at 6:00 AM daily
0 6 * * * cd $SKILL_DIR && $TSX_PATH daily-report-runner.ts morning >> $LOG_DIR/morning-report.log 2>&1

# End-of-day summary at 5:00 PM daily
0 17 * * * cd $SKILL_DIR && $TSX_PATH daily-report-runner.ts eod >> $LOG_DIR/eod-report.log 2>&1

EOF

# Install new crontab
crontab /tmp/crontab.new

echo "✅ Cron jobs installed successfully!"
echo ""
echo "📋 Installed cron jobs:"
echo "   • Morning report: Daily at 6:00 AM"
echo "   • EOD report: Daily at 5:00 PM"
echo ""
echo "📝 Logs will be saved to:"
echo "   • Morning: $LOG_DIR/morning-report.log"
echo "   • EOD: $LOG_DIR/eod-report.log"
echo ""

# Show current crontab
echo "📅 Current crontab:"
crontab -l | tail -10
echo ""

# Test report generation
echo "🧪 Testing report generation..."
read -p "Do you want to run a test report now? (Y/n) " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Nn]$ ]]; then
  echo ""
  echo "Running test morning report..."
  cd "$SKILL_DIR"
  $TSX_PATH daily-report-runner.ts morning
fi

echo ""
echo "=============================================="
echo "✅ SETUP COMPLETE!"
echo "=============================================="
echo ""
echo "NEXT STEPS:"
echo ""
echo "1. Verify cron is running:"
echo "   systemctl status cron"
echo ""
echo "2. Monitor report logs:"
echo "   tail -f $LOG_DIR/morning-report.log"
echo ""
echo "3. Test Slack connection:"
echo "   cd $SKILL_DIR"
echo "   $TSX_PATH daily-report-runner.ts test"
echo ""
echo "4. Manual report generation:"
echo "   cd $SKILL_DIR"
echo "   $TSX_PATH daily-report-runner.ts morning"
echo "   $TSX_PATH daily-report-runner.ts eod"
echo ""
echo "5. View scheduled jobs:"
echo "   crontab -l"
echo ""
echo "6. Remove cron jobs:"
echo "   crontab -e  # Remove the daily-report lines"
echo ""
echo "SLACK CHANNEL SETUP:"
echo "1. Create #morning-report channel in Slack"
echo "2. The webhook will automatically post there"
echo "3. Optional: Invite team members to the channel"
echo ""

# Cleanup
rm -f /tmp/crontab.new