Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Python service scheduler. Run services on a set schedule. Add or modify schedules with IPC.

Project description

Python 3 service scheduler to run services at a desired interval. This library uses a long running process which can listen for commands to change the schedule without restarting down the process.

API

Schedule:

  • Properties - days (int)[0]: Number of days when the next run should happen. - hours (int)[0]: Number of hours when the next run should happen. - minutes (int)[0]: Number of minutes when the next run should happen. - seconds (int)[0]: Number of seconds when the next run should happen. - milliseconds (int)[0]: Number of milliseconds when the next run should happen. - microseconds (int)[0]: Number of microseconds when the next run should happen. - sundays (bool)[None]: Allow the schedule to run on sundays. If all weekdays are None allow all weekdays. - mondays (bool)[None]: Allow the schedule to run on mondays. If all weekdays are None allow all weekdays. - tuesdays (bool)[None]: Allow the schedule to run on tuesdays. If all weekdays are None allow all weekdays. - wednesdays (bool)[None]: Allow the schedule to run on wednesdays. If all weekdays are None allow all weekdays. - thursdays (bool)[None]: Allow the schedule to run on thursdays. If all weekdays are None allow all weekdays. - fridays (bool)[None]: Allow the schedule to run on fridays. If all weekdays are None allow all weekdays. - saturdays (bool)[None]: Allow the schedule to run on saturdays. If all weekdays are None allow all weekdays. - at (str)[None]: Time of day when the schedule should run. - start_on (str)[None]: Date and time on which to start on. - end_on (str)[None]: Date and time on which to end on. - callback (function)[None]: Function to call when run is called.
  • Methods - can_run(): Return True if it is time to run the schedule function. - run(callback=None): Run the callback function and create the next_run interval datetime.

SubprocessService:

  • Properties - cmd (str/list): String shell command which will be split by shlex or a list of command arguments. - output_log (str)[None]: Standard Output log file name. - error_log (str)[None]: Standard Error log file name. - shell (bool)[None]: If the subprocess should run a shell command.
  • Methods - __call__(): Run the main start function. - start(): Main run function. - stop(): Stop the main function. - is_running(): Return if this function is still running.

ServiceScheduler:

  • Properties - print_func (function)[None]: Custom print function that gets called every time a service is run.
  • Methods - print_func(name, service, schedule): Override this function. Print when service is run.

Code Example

Schedule

import pyservice_scheduler as pss

def hello():
    print('hello')

sched = pss.Schedule(callback=hello, seconds=5)

while True:
    if sched.can_run():
        sched.run()
    time.sleep(1)

Service

import pyservice_scheduler as pss

service = pss.SubprocessService('ls')

# Run the Service
service()

Service Scheduler

import time
import pyservice_scheduler as pss

# Create the service scheduler
scheduler = pss.ServiceScheduler()

# Register the service
scheduler.register('ls', pss.Schedule(seconds=30), pss.SubprocessService('ls', output_log='ls.log', shell=True))

while True:
    scheduler.run()
    time.sleep(1)

Command Line

Several command line options have been made in “server.py”. This is to help create a general service schedule server to get started without much hassle.

Environment Variables

Setup the server using environment variables. Defaults are already set.

$> python -m pyservice_scheduler save_server_defaults --address "0.0.0.0" --port "8000"

Run Server

Start running the service scheduler with

$> python -m pyservice_scheduler run

Alternatively run with a password

$> python -m pyservice_scheduler run --authkey 'abc123'

Note

Keep using the authkey argument if your server is password protected.

$> python -m pyservice_scheduler quit –authkey “abc123”

Register Service

Register a service to run

$> python -m pyservice_scheduler register --name "ls" --cmd ls --shell --output_log 'ls.log' --seconds 30

List Services

List the registered services

$> python -m pyservice_scheduler list
Services:
ls - Next Run: 2019-08-29 16:05:07.279050

Remove Service

Remove a service

$> python -m pyservice_scheduler remove --name "ls"

Quit the Server

Quit the service scheduler

$> python -m pyservice_scheduler quit

Project details


Release history Release notifications

This version

0.0.1

Download files

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

Files for pyservice-scheduler, version 0.0.1
Filename, size File type Python version Upload date Hashes
Filename, size pyservice_scheduler-0.0.1.tar.gz (18.5 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page