Skip to main content

Agendador de Scripts Python usando Crontab com Painel Streamlit

Project description

Python Scripts Cron Scheduler

Abstração com Painel em Streamlit para gestão do agendamento de scripts Python através de CronJobs.

Demo

📌 Instalação

Você pode instalar as bibliotecas necessárias manualmente ou utilizando um arquivo requirements.txt.

pip install python-crontab psutil streamlit

🚀 Configuração

  1. Copie o arquivo python_scs.py para sua aplicação.
  2. Certifique-se de ter a seguinte estrutura de diretórios:
└── sua_aplicação/
    ├── scripts/
    │   ├── logs/
    │   ├── script_teste.py
    │   ├── __init__.py
    ├── python_scs.py
    ├── streamlit_pannel.py
    ├── app.py
  1. Configure o painel no arquivo streamlit_pannel.py:
import os
from python_scs import PythonScriptsCronManager
from python_scs.streamlit import PannelConfig, StreamlitPannel

scripts_manager = PythonScriptsCronManager(
    config=PythonScriptsCronManager.Config(
        app_path=os.path.abspath('.'),  # Raiz onde scripts_folder estará
        scripts_folder='scripts',       # Diretório com os códigos
        logs_folder='scripts/logs'      # Diretório de logs
    ),
    user=True
)

streamlit_pannel = StreamlitPannel(scripts_manager, config=PannelConfig(
    layout='wide',
    title='Crontab Interface',
    subheader='Interface para gerenciamento de agendamentos',
    allow_upload_script=True,
    allow_create_job=True,
    allow_execute_job=True,
    allow_toggle_job=True,
    allow_remove_job=True
))
  1. Execute o painel:
streamlit run streamlit_pannel.py

🛠 Uso da API

Instanciando o gerenciador de scripts

import os
from python_scs import PythonScriptsCronManager

scripts_manager = PythonScriptsCronManager(
    config=PythonScriptsCronManager.Config(
        app_path=os.path.abspath("."),  # Raiz onde scripts_folder estará
        scripts_folder="scripts",       # Diretório com os códigos
        logs_folder="scripts/logs"      # Diretório de logs
    ),
    user=True
)

📌 Veja a documentação do python-crontab para entender o parâmetro user.

Listando os scripts disponíveis

scripts = scripts_manager.get_scripts()
print(scripts)  # ["script_teste.py"]

Criando um agendamento

job = scripts_manager.set_script_job(
    script_name="script_teste.py",
    schedule=["* * * * *"],
    comment="Agendamento teste",
    enable=True
)

# Criando um agendamento com comando customizado
job = scripts_manager.set_job(
    command='echo "Teste"',
    schedule=["* * * * *"],
    log_file_name="teste.txt",  # Necessário para armazenar a saída
    comment="Agendamento customizado",
    enable=True
)

📌 Para verificar se o agendamento foi criado, execute:

crontab -l

Listando os agendamentos configurados

jobs = scripts_manager.get_jobs()
for job in jobs:
    print(f"{job.comment} - {job.script_name} - {job.is_runing()}")

# Busca um agendamento por filtros
job_script_test = scripts_manager.get_job({
    "script_name": "script_teste.py",
    "comment": "Agendamento teste"
})

Habilitando, desabilitando, executando e removendo um agendamento

job = scripts_manager.get_job({
    "script_name": "script_teste.py",
    "comment": "Agendamento teste"
})

job.enable_job()     # Habilita o job
job.disable_job()    # Desabilita o job
job.toggle_job()     # Alterna entre ativado/desativado

# Executa o script manualmente
scripts_manager.execute(job)

# Executa como subprocesso
scripts_manager.execute(job, use_subprocess=True)

# Remove o agendamento
scripts_manager.remove_job(job)

📜 Licença

Este projeto é distribuído sob a licença MIT. Consulte o arquivo LICENSE para mais detalhes.

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

python_scs-0.1.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

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

python_scs-0.1.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file python_scs-0.1.0.tar.gz.

File metadata

  • Download URL: python_scs-0.1.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for python_scs-0.1.0.tar.gz
Algorithm Hash digest
SHA256 da34ac86574a0646d5be63393a9026cf7ec55516e669faf98fad4778746a20fc
MD5 673700bfdbbd36948142d5c2eb6e9a86
BLAKE2b-256 17eeeefeae1a448cf73a9a5463f03795f3a183d3062f893229dbe5496f3b6463

See more details on using hashes here.

File details

Details for the file python_scs-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: python_scs-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for python_scs-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bc94cecea01aeeb38bb833cf9afe1b77e20c6bb540c34e040c582517c8f33020
MD5 6eb69778ec2c852170c19637cb44d454
BLAKE2b-256 7a430c1d6726bd9bb87e633e161d3fc3e6eb9b4c68e6cd248a95c3dbd5432790

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