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

pip install python_scs

Caso vá utilizar o painel em Streamlit

pip install python_scs[streamlit]

🚀 Configuração

Confira o exemplo completo.

  1. Certifique-se de ter a seguinte estrutura de diretórios:
├── scripts/
│   ├── logs/
│   ├── script_teste.py
│   ├── __init__.py
├── main.py
  1. Instancie o gerenciador de scripts e configure o painel:
# main.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 utilizando o streamlit:
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-1.0.tar.gz (9.8 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-1.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for python_scs-1.0.tar.gz
Algorithm Hash digest
SHA256 17873753774e69f8f570a48ca6d0b58304cccd838a840db729058bbbcda5f89a
MD5 b72c78bcaabc947a52000b65e1e8e296
BLAKE2b-256 644482dfa5ad76b58d968bdd3ff1893523b0001d2dfe8b0d2014edd1f623676c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_scs-1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 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-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 47e03ffd8322b8edab698032110e3b6663c0bc6978f0fb0d160c090b43fc3908
MD5 f632819671199204ef1d61ff01a72c25
BLAKE2b-256 1e795e228e132663a50c296f02625de86c49078fd521914c27a15f32a26cdb3f

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