Skip to main content

Support scheduling of task via command line interface.

Project description

Python Scheduler

A Python library supporting task scheduling.

Installation

Install the package using pip:

pip install python-cli-scheduler

Schedule Format

The scheduler format defines how tasks are executed. Use:

^<run_now>@<interval>/<delay>$<end_timestamp>#<retry>

Parameters

Parameter Description Default
run_now Whether to execute immediately (True or False). True
interval Number of seconds to repeat the task. If None, it runs only once. None
delay Seconds to wait in each interval before executing. 0
end_timestamp UNIX timestamp to stop execution. If None, runs infinitely. None
retry Retry execution if an exception occurs (True or False). True

Usage

Use a Decorator

You can use the scheduler_handle decorator to execute recurring tasks. Below is an example that prints a random "lucky number" every 10 seconds, starting at the 3rd second, and continues for 30 seconds.

import random
import time

from cli_scheduler import scheduler_handle

end_time = int(time.time()) + 30


@scheduler_handle(schedule=f'^true@10/3${end_time}#true')
def rotation_lucky(start=0, end=1000):
    n = random.randint(start, end)
    print(f'The lucky number is {n}')


if __name__ == '__main__':
    rotation_lucky(10, end=100)

Using a Scheduler Job Class

With SchedulerJob, you gain more control over the task execution lifecycle. This example runs a job every 60 seconds, starting at the 3rd second, for 5 minutes. It logs the task lifecycle phases and prints a "lucky number."

import time
import random

from cli_scheduler import SchedulerJob


class RotationLuckJob(SchedulerJob):
    def __init__(self, scheduler):
        super().__init__(scheduler=scheduler)

    def _pre_start(self):
        self.logger.info('Rotation luck. Have fun !')
        
    def _start(self):
        self.n = random.randint(0, 1000)

    def _execute(self, *args, **kwargs):
        self.logger.info(f'The lucky number is {self.n}')
    
    def _end(self):
        del self.n
        
    def _follow_end(self):
        self.logger.info('Done')

        
if __name__ == '__main__':
    end_time = int(time.time()) + 5 * 60

    # ^false: Not execute now
    # @60: Repeat execute every 60 second
    # /3: Execute at second 3
    # ${end_time}: Run until end_time
    # #false: Don't retry if exception when execute
    job = RotationLuckJob(scheduler=f'^false@60/3${end_time}#false')
    job.run()

Features

  • Task Scheduling: Automates task execution using a flexible crontab-like syntax.

  • Decorator Support: Simplifies task scheduling using Python function decorators.

  • Lifecycle Hooks: Gain fine-grained control with SchedulerJob methods:

    • _pre_start: Preparation before starting the task.
    • _start: Runs once before task execution begins.
    • _execute: The core task logic is implemented here.
    • _end: Final cleanup after task execution ends.
    • _follow_end: Actions to follow the end of the task lifecycle.
  • Retry Support: Retry tasks in case of runtime exceptions.

License

This library is licensed under the MIT License. See the LICENSE file for more details.

Contributing

Contributions are welcome! Feel free to submit issues, feature requests, or pull requests to improve the library.

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_cli_scheduler-1.1.7.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

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

python_cli_scheduler-1.1.7-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file python_cli_scheduler-1.1.7.tar.gz.

File metadata

  • Download URL: python_cli_scheduler-1.1.7.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.9

File hashes

Hashes for python_cli_scheduler-1.1.7.tar.gz
Algorithm Hash digest
SHA256 64293447719552d5796e6123aa7e143cea7ad484383c6ae97ea847b6d3534697
MD5 96538763875efaf4b04486f98e5fa380
BLAKE2b-256 bc6d58923625b0c77e15639788ee4c5b6669cb0e6f3d88f65d2928f15f4d0266

See more details on using hashes here.

File details

Details for the file python_cli_scheduler-1.1.7-py3-none-any.whl.

File metadata

File hashes

Hashes for python_cli_scheduler-1.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 89f417fcb19d395d667a7cb957c715ac355cf39f8804761eda453df2a2372040
MD5 a03168f25ab8548da3e484223882baf1
BLAKE2b-256 1f9c2ec2800d5feafb204a204be54bb1d7ea3b196e024a9b15622609c244585e

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