Files
attune/work-summary/sessions/2025-01-18-timer-triggers.md
2026-02-04 17:46:30 -06:00

7.3 KiB

Implementation Summary: Built-in Timer Triggers

Date: 2025-01-18
Status: Complete (Implementation) - Testing Pending
Branch: main

Overview

Implemented comprehensive built-in timer trigger system for Attune, enabling time-based automation without requiring custom sensor code. This is a critical feature for the minimum viable product, allowing users to create scheduled tasks similar to cron jobs.

What Was Implemented

1. TimerManager Module (crates/sensor/src/timer_manager.rs)

Created a comprehensive timer management system with support for three trigger types:

Features:

  • One-shot timers: Fire once at a specific date/time
  • Interval timers: Fire at regular intervals (seconds, minutes, hours)
  • Cron-style timers: Fire on cron schedule (e.g., "0 0 * * * *")

Architecture:

  • TimerManager: Main component that manages multiple timer instances
  • TimerInstance: Individual timer task with its own tokio task
  • TimerConfig: Enum for timer configuration (serializable to/from JSON)
  • Thread-safe design using Arc and RwLock
  • Automatic event generation through callback pattern

Key Methods:

  • start_timer(trigger): Start a timer for a given trigger
  • stop_timer(trigger_id): Stop a specific timer
  • stop_all(): Stop all timers (cleanup)
  • timer_count(): Get active timer count

2. Service Integration (crates/sensor/src/service.rs)

Integrated TimerManager into the SensorService:

  • Added timer_manager as a component alongside sensor_manager
  • Created event generation callback that:
    • Generates system events (no sensor source)
    • Matches rules against events
    • Creates enforcements
  • Loads all enabled timer triggers on service startup
  • Stops all timers on service shutdown
  • Updated health checks to include timer count

3. Core Pack with Timer Triggers (scripts/seed_core_pack.sql)

Created comprehensive seed data including:

Packs:

  • core pack with built-in functionality

Runtimes:

  • shell runtime for executing shell commands

Timer Triggers:

  • core.timer_10s: Fires every 10 seconds
  • core.timer_1m: Fires every minute
  • core.timer_hourly: Fires at top of every hour (cron)

Actions:

  • core.echo: Echo a message to stdout
  • core.sleep: Sleep for N seconds
  • core.noop: No operation (testing)

All with proper JSON schemas for parameters and outputs.

4. Setup Script (scripts/setup_timer_echo_rule.sh)

Created automated setup script that:

  • Authenticates with API
  • Verifies core pack exists
  • Verifies timer trigger exists
  • Verifies echo action exists
  • Creates rule linking timer to action
  • Provides monitoring commands

5. Quick Start Guide (docs/quickstart-timer-demo.md)

Comprehensive documentation including:

  • Prerequisites and architecture overview
  • Step-by-step setup instructions
  • Service startup sequence
  • Monitoring and troubleshooting
  • Experimentation examples
  • Clean up instructions

Technical Details

Timer Configuration Format

{
  "type": "interval",
  "seconds": 10,
  "minutes": 0,
  "hours": 0
}
{
  "type": "cron",
  "expression": "0 0 * * * *"
}
{
  "type": "one_shot",
  "fire_at": "2025-01-20T15:00:00Z"
}

Event Payload Format

Timer events include:

  • type: Timer type (interval/cron/one_shot)
  • fired_at: ISO 8601 timestamp when fired
  • interval_seconds: For interval timers
  • scheduled_at: For cron timers

Dependencies Added

  • cron = "0.12": Cron expression parsing and scheduling

Event Flow

Timer fires → TimerManager callback →
generate_system_event → Event record created →
match_event → Rule evaluation → Enforcement created →
Executor processes → Worker executes action

Testing Status

Completed:

  • Unit tests for TimerConfig serialization/deserialization
  • Unit tests for interval calculation
  • Unit tests for cron parsing
  • Code compiles successfully (no type errors)

Pending:

  • SQLx query cache preparation (cargo sqlx prepare)
  • Integration tests with database
  • End-to-end test: timer → event → rule → execution
  • Test all three timer types (one-shot, interval, cron)
  • Test timer restart and cleanup
  • Test timer precision and drift
  • Load testing with many concurrent timers

Known Issues & Limitations

  1. SQLx Query Cache: Sensor service requires cargo sqlx prepare to be run with DATABASE_URL set
  2. Timer Precision: Tokio's interval timer may drift over time for long-running timers
  3. One-shot Timers: Past times are rejected, no persistence across restarts
  4. Configuration Reload: Timers require service restart to pick up trigger changes
  5. No UI: Timer management currently requires SQL/API access

Files Created

  • attune/crates/sensor/src/timer_manager.rs (510 lines)
  • attune/scripts/seed_core_pack.sql (321 lines)
  • attune/scripts/setup_timer_echo_rule.sh (160 lines)
  • attune/docs/quickstart-timer-demo.md (353 lines)
  • attune/work-summary/2025-01-18-timer-triggers.md (this file)

Files Modified

  • attune/crates/sensor/Cargo.toml: Added cron dependency
  • attune/crates/sensor/src/main.rs: Added timer_manager module
  • attune/crates/sensor/src/service.rs: Integrated TimerManager
  • attune/crates/sensor/src/rule_matcher.rs: Removed unused import
  • attune/crates/common/src/repositories/trigger.rs: Already had find_enabled method

Next Steps

Immediate (Before Testing):

  1. Run SQLx prepare: Set DATABASE_URL and run cargo sqlx prepare for sensor service
  2. Create admin user: Seed an admin identity for API access
  3. Start services: API, Sensor, Executor, Worker

Testing:

  1. Load core pack: psql $DATABASE_URL -f scripts/seed_core_pack.sql
  2. Run setup script: ./scripts/setup_timer_echo_rule.sh
  3. Verify timer fires every 10 seconds
  4. Monitor logs and database for event → rule → enforcement → execution flow
  5. Test all timer types (interval, cron, one-shot)

Future Enhancements:

  • Dynamic reload: Hot reload timer configuration without restart
  • Timer persistence: Save one-shot timers to survive restarts
  • Timezone support: Allow specifying timezone for cron expressions
  • Drift correction: Implement drift correction for long-running timers
  • Timer UI: Add web UI for managing timer triggers
  • Webhook triggers: Implement HTTP webhook triggers
  • File watch triggers: Implement filesystem monitoring triggers

Success Criteria

Timer manager compiles and integrates with sensor service
Core pack seed data creates all necessary records
Setup script automates rule creation
Documentation provides clear path from zero to working demo
Timer fires and generates events (pending testing)
Events match rules and create enforcements (pending testing)
Executions run on workers (pending testing)
"Hello World" appears in worker logs every 10 seconds (pending testing)

Conclusion

The timer trigger implementation provides the foundation for time-based automation in Attune. All code is written, documented, and ready for testing. The implementation follows Attune's architectural patterns and integrates seamlessly with existing components.

Critical Path Status: IMPLEMENTED, READY FOR TESTING

The system is now ready to demonstrate the complete automation flow: sensor → trigger → event → rule → enforcement → execution → action.