Skip to main content

A lightweight SQLite-based run tracker for Python scripts

Project description

Script Run Tracker

A lightweight SQLite-based run tracker for Python scripts with automatic logging capabilities.

This library is a companion tool for the Python Automation Project, designed to track execution history, monitor scheduled jobs, data pipelines, and automation scripts with detailed logging.

Features

  • 📊 SQLite-based tracking - No external dependencies
  • 📝 Automatic logging - Creates timestamped log files for each run
  • 🔄 Log rotation - Automatically manages old log files
  • Context manager support - Clean and simple API
  • 🎯 Trigger tracking - Distinguishes between manual and scheduled runs
  • Status tracking - Automatically tracks success/failure states
  • 📁 Project organization - Logs stored alongside your scripts

Installation

pip install script-run-tracker

Related Projects

This library is part of the Python Automation Project ecosystem, designed to provide robust tracking and monitoring capabilities for automated workflows.

Quick Start

1. Initialize the database

First, create the database schema (run once):

from run_tracker import init_database

init_database('tracking.db')

2. Register your flow

from run_tracker import register_flow

register_flow(
    db_path='tracking.db',
    flow_name='Daily Data Processing',
    flow_path='/path/to/your/script.py',
    description='Processes daily sales data'
)

3. Use in your script

from run_tracker import RunTracker

with RunTracker('tracking.db', trigger_type='scheduler') as tracker:
    tracker.log("Starting data processing")
    
    # Your code here
    data = load_data()
    tracker.log(f"Loaded {len(data)} records")
    
    process_data(data)
    tracker.log("Processing complete")

Usage

Basic Usage

from run_tracker import RunTracker

with RunTracker('tracking.db') as tracker:
    tracker.log("Process started")
    # Your code here
    tracker.log("Process completed")

Custom Project Name

with RunTracker('tracking.db', project_name='my_etl_job') as tracker:
    tracker.log("ETL job started")
    # Your code here

Trigger Types

Automatically detect whether the script was run manually or by a scheduler:

import sys
from run_tracker import RunTracker

# Detect trigger type from command line argument
trigger_type = sys.argv[1] if len(sys.argv) > 1 else "scheduler"

with RunTracker('tracking.db', trigger_type=trigger_type, max_log_files=10) as tracker:
    tracker.log("Process started")
    # Your code here

Usage:

# Run manually (defaults to 'scheduler' if no argument)
python your_script.py

# Explicitly set trigger type
python your_script.py manual
python your_script.py scheduler

Alternatively, you can explicitly set the trigger type:

# For scheduled runs
with RunTracker('tracking.db', trigger_type='scheduler') as tracker:
    tracker.log("Automated run started")

# For manual runs
with RunTracker('tracking.db', trigger_type='manual') as tracker:
    tracker.log("Manual run started")

Log Levels

with RunTracker('tracking.db') as tracker:
    tracker.log("Informational message", level='INFO')
    tracker.log("Debug information", level='DEBUG')
    tracker.log("Warning message", level='WARNING')
    tracker.log("Error occurred", level='ERROR')
    tracker.log("Critical issue", level='CRITICAL')

Configure Log Retention

# Keep only the last 5 log files
with RunTracker('tracking.db', max_log_files=5) as tracker:
    tracker.log("Starting with custom retention")

Database Schema

The package automatically creates two tables:

flows - Stores information about your scripts

  • flow_id (PRIMARY KEY)
  • flow_name
  • flow_path
  • description
  • is_active
  • created_at

runs - Stores execution history

  • run_id (PRIMARY KEY)
  • flow_id (FOREIGN KEY)
  • status ('running', 'success', 'fail')
  • trigger_type ('scheduler', 'manual')
  • start_time
  • finish_time
  • error_message
  • log_file_path

Log Files

Log files are automatically created in a logs/ directory next to your script:

your_project/
├── your_script.py
└── logs/
    ├── your_script_run_1.log
    ├── your_script_run_2.log
    └── your_script_run_3.log

Error Handling

RunTracker automatically captures and logs exceptions:

with RunTracker('tracking.db') as tracker:
    tracker.log("Starting risky operation")
    
    # If this raises an exception, it will be:
    # 1. Logged to the log file
    # 2. Stored in the database
    # 3. Re-raised for your handling
    risky_operation()

Utility Functions

Initialize Database

from run_tracker import init_database

init_database('tracking.db')

Register a Flow

from run_tracker import register_flow

register_flow(
    db_path='tracking.db',
    flow_name='Data Sync Job',
    flow_path='/opt/scripts/data_sync.py',
    description='Syncs data from external API'
)

Deactivate a Flow

from run_tracker import deactivate_flow

deactivate_flow('tracking.db', 'Data Sync Job')

Requirements

  • Python 3.7+
  • No external dependencies (uses only Python standard library)

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

If you encounter any issues or have questions, please file an issue on the GitHub repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

script_run_tracker-0.1.4.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

script_run_tracker-0.1.4-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file script_run_tracker-0.1.4.tar.gz.

File metadata

  • Download URL: script_run_tracker-0.1.4.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for script_run_tracker-0.1.4.tar.gz
Algorithm Hash digest
SHA256 6fc0b2521f3ef8555fea8249bf4c1d7a73f51801a6e138eda6985f46735aec17
MD5 950586a92e6970d1e5027e77a5d9e540
BLAKE2b-256 a19ea5176d9e8ad487a63e42133a11cb20786d2087afb0583a4ba67bba245246

See more details on using hashes here.

File details

Details for the file script_run_tracker-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for script_run_tracker-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fc2038ae11730a624aef3f88ffcefd143eb7138988c1a34e5bfc88708fd3d432
MD5 7da43d3cbcbcce31f26a1bbf592a0097
BLAKE2b-256 38321b8184e08ea069a411a5f2b72eaa3a2bf6dc55ab786440efd24082712791

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page