Files
scheduler/setup_database.sql
T
jpmschweitzerandClaude Opus 4.5 64574bcc39
Build and Push / build (release) Failing after 17s
Initial commit: scheduler service extraction from portainer-core
Extracted standalone scheduler service with:
- FastAPI REST API for task management
- APScheduler-based task execution
- PostgreSQL persistence
- Docker container support
- Gitea Actions CI/CD workflow

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-11 11:59:32 +01:00

121 lines
4.3 KiB
SQL

-- The Scheduler Database Setup
-- Run this from postgres-shared container
-- Create database and user
CREATE DATABASE scheduler;
CREATE USER scheduler_user WITH PASSWORD 'a/Ph0NhC4pTDDjSpL6q/DtBI+z0nf43ijVHjTo1KrXc=';
GRANT ALL PRIVILEGES ON DATABASE scheduler TO scheduler_user;
-- Connect to the new database
\c scheduler
-- Grant schema permissions
GRANT ALL ON SCHEMA public TO scheduler_user;
-- Create scheduled_tasks table
CREATE TABLE scheduled_tasks (
id SERIAL PRIMARY KEY,
task_name VARCHAR(100) NOT NULL UNIQUE,
service VARCHAR(50) NOT NULL, -- Which service owns this task
executor VARCHAR(100) NOT NULL, -- Executor module to run
priority INTEGER NOT NULL DEFAULT 30, -- Lower = higher priority (1-100)
-- Scheduling (supports wildcards: -1 = any)
minute INTEGER DEFAULT -1, -- 0-59 or -1 (any)
hour INTEGER DEFAULT -1, -- 0-23 or -1 (any)
day_of_month INTEGER DEFAULT -1, -- 1-31 or -1 (any)
month INTEGER DEFAULT -1, -- 1-12 or -1 (any)
day_of_week INTEGER DEFAULT -1, -- 0-6 (0=Monday) or -1 (any)
enabled BOOLEAN DEFAULT true,
description TEXT,
config JSONB, -- Arguments for executor
-- Execution tracking
last_run TIMESTAMP,
last_status VARCHAR(20), -- success, failed, timeout
last_duration_seconds INTEGER,
retry_count INTEGER DEFAULT 0,
max_retries INTEGER DEFAULT 3,
timeout_seconds INTEGER DEFAULT 3600, -- 1 hour default
-- Metadata
created_at TIMESTAMP DEFAULT NOW(),
updated_at TIMESTAMP DEFAULT NOW(),
created_by VARCHAR(50),
-- Constraints
CHECK (priority >= 1 AND priority <= 100),
CHECK (minute >= -1 AND minute <= 59),
CHECK (hour >= -1 AND hour <= 23),
CHECK (day_of_month >= -1 AND day_of_month <= 31),
CHECK (month >= -1 AND month <= 12),
CHECK (day_of_week >= -1 AND day_of_week <= 6)
);
-- Create task_executions table
CREATE TABLE task_executions (
id SERIAL PRIMARY KEY,
task_id INTEGER NOT NULL REFERENCES scheduled_tasks(id),
task_name VARCHAR(100) NOT NULL,
service VARCHAR(50) NOT NULL,
executor VARCHAR(100) NOT NULL,
priority INTEGER NOT NULL,
status VARCHAR(20) NOT NULL, -- pending, running, success, failed, timeout
triggered_by VARCHAR(50), -- scheduler, manual, retry
triggered_at TIMESTAMP DEFAULT NOW(),
started_at TIMESTAMP,
completed_at TIMESTAMP,
duration_seconds INTEGER,
output TEXT,
error TEXT,
retry_count INTEGER DEFAULT 0,
metadata JSONB
);
-- Create doc_sources table
CREATE TABLE doc_sources (
id SERIAL PRIMARY KEY,
project_name VARCHAR(100) NOT NULL UNIQUE,
current_version VARCHAR(50),
last_mirrored TIMESTAMP,
last_checked TIMESTAMP,
gitea_repo VARCHAR(200),
config JSONB
);
-- Indexes for scheduled_tasks
CREATE INDEX idx_tasks_enabled ON scheduled_tasks(enabled) WHERE enabled = true;
CREATE INDEX idx_tasks_priority ON scheduled_tasks(priority);
CREATE INDEX idx_tasks_service ON scheduled_tasks(service);
CREATE INDEX idx_tasks_schedule ON scheduled_tasks(minute, hour, day_of_month, month, day_of_week) WHERE enabled = true;
-- Indexes for task_executions
CREATE INDEX idx_executions_task_id ON task_executions(task_id);
CREATE INDEX idx_executions_task_name ON task_executions(task_name);
CREATE INDEX idx_executions_status ON task_executions(status);
CREATE INDEX idx_executions_triggered_at ON task_executions(triggered_at DESC);
CREATE INDEX idx_executions_service ON task_executions(service);
CREATE INDEX idx_executions_running ON task_executions(task_id) WHERE status = 'running';
-- Grant permissions
GRANT ALL ON ALL TABLES IN SCHEMA public TO scheduler_user;
GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO scheduler_user;
-- Insert example test task
INSERT INTO scheduled_tasks
(task_name, service, executor, priority, minute, hour, description, config, created_by)
VALUES
('test_example_task', 'scheduler', 'example_executor', 50, -1, -1,
'Example task that runs every minute for testing',
'{"message": "Scheduler is working!", "delay_seconds": 2}'::jsonb,
'setup_script');
-- Verify tables created
\dt
-- Show the test task
SELECT task_name, service, priority, enabled, description FROM scheduled_tasks;