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
├── streamlit_pannel.py
  1. Instancie o gerenciador de scripts e configure o painel:
# streamlit_pannel.py
from python_scs import PythonScriptsCronManager, streamlit_ui

manager = PythonScriptsCronManager(
    user=True
)

streamlit_pannel = streamlit_ui.init(
    manager,
    layout='wide',
    title='Scripts Manager',
    subheader='Manage your python scripts'
)
  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.1.tar.gz (10.3 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.1-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_scs-1.1.tar.gz
  • Upload date:
  • Size: 10.3 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.1.tar.gz
Algorithm Hash digest
SHA256 ee0f934c55379746c7d5240e9773dfb645bd2a8df2d710b57c1f8a92756801d5
MD5 dd50329648ab1fc1aabf088970f6e476
BLAKE2b-256 1ab664539f9c66c8935e733a782f06a6f22ecc34f5d7283211020b77c5ebc858

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_scs-1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.4 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7f241dc141f7459c8ce663ff84e83cb8c7d36674135867cea5b9a3948ce74ffa
MD5 2badfe5955c6e3f8f6ca0a333dbae7be
BLAKE2b-256 b01dacdcb131451350cdfb5cabc5b97e3637250c8841eec4e78bb290fbfc1c5b

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