Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for jobs with cron #30

Open
xmnlab opened this issue Mar 30, 2023 · 1 comment
Open

Add support for jobs with cron #30

xmnlab opened this issue Mar 30, 2023 · 1 comment
Assignees
Labels
psf-grant-proposal Issues used for a PSF grant proposal size:large >16h

Comments

@xmnlab
Copy link
Member

xmnlab commented Mar 30, 2023

It would be nice to have some support for jobs that will be "installed" the cron.

so we could define the jobs inside the makim, also it could reuse the targets defined, and using the cli it could create the job in the as a cron task.

for this we need to have root access. so maybe we could have some cron task folder for the user locally where we can add any job, with no super user power, and call that from a cron tab folder, where we will need root permissions just once.

Example of makim file:

version: 1.0
groups:
  main:
    env-file: .env
    targets:
      clean:
        help: Use this target to clean up temporary files
        args:
          all:
            type: bool
            action: store_true
            help: Remove all files that are tracked by git
        run: |
          echo "remove file X"
      build:
        help: Build the program
        args:
          clean:
            type: bool
            action: store_true
            help: if not set, the clean dependency will not be triggered.
        dependencies:
          - target: clean
            if: {% raw %}{{ args.clean == true }}{% endraw %}
        run: |
          echo "build file x"
          echo "build file y"
          echo "build file z"

cron:  # or scheduler
  build:
    backend: crontab  # default
    schedule: "* * * * *"
    target: main.build
      args:
      clean: true
  clean:
    schedule: "* * * * *"
    target: main.clean

example of the CLI call:

makim cron start build
makim cron stop clean
makim cron run build
makim cron list
  • start: install and enable cron job
  • stop: disable cron job
  • run: execute the target
  • list: show all cron tasks and their status
@xmnlab
Copy link
Member Author

xmnlab commented Feb 12, 2024

some alternative for the backend: https://apscheduler.readthedocs.io/en/3.x/userguide.html


from gpt:

Using APScheduler as a backend for task management within an application like Makim involves creating a wrapper or interface around APScheduler that allows you to create, delete, and list scheduled tasks. Below is an example implementation that demonstrates how you might accomplish this. This example assumes you have APScheduler installed and are familiar with basic Python programming concepts.

Example Scheduler Interface

from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.jobstores.base import JobLookupError

class TaskScheduler:
    def __init__(self):
        self.scheduler = BackgroundScheduler()
        self.scheduler.start()

    def add_task(self, task_func, task_id, trigger, **trigger_args):
        """
        Adds a new task to the scheduler.

        Parameters:
        - task_func: The function to be scheduled.
        - task_id: A unique identifier for the task.
        - trigger: The type of trigger ('date', 'interval', or 'cron').
        - **trigger_args: Arguments specific to the chosen trigger type.
        """
        self.scheduler.add_job(task_func, trigger, id=task_id, **trigger_args)

    def remove_task(self, task_id):
        """
        Removes a task from the scheduler using its unique identifier.

        Parameters:
        - task_id: The unique identifier for the task.
        """
        try:
            self.scheduler.remove_job(task_id)
        except JobLookupError:
            print(f"Failed to remove task {task_id}: Task not found")

    def list_tasks(self):
        """
        Lists all scheduled tasks.
        """
        jobs = self.scheduler.get_jobs()
        for job in jobs:
            print(f"ID: {job.id}, Next Run: {job.next_run_time}, Trigger: {job.trigger}")

# Example usage
def my_scheduled_task():
    print("Executing my scheduled task.")

# Initialize the task scheduler
scheduler = TaskScheduler()

# Add a new task
scheduler.add_task(my_scheduled_task, 'task1', 'interval', seconds=30)

# List scheduled tasks
scheduler.list_tasks()

# Remove a task
scheduler.remove_task('task1')

Features of This Implementation:

  • Initialization: Sets up a background scheduler that runs independently of the main application thread.
  • Adding Tasks: Allows adding tasks with a unique ID, a trigger type (date, interval, cron), and trigger arguments (like seconds for interval triggers).
  • Removing Tasks: Enables the removal of tasks by their unique ID, handling cases where the task ID does not exist.
  • Listing Tasks: Provides a simple way to list all currently scheduled tasks, including their IDs, next run time, and trigger configuration.

This example provides a basic framework for managing tasks within an application. You can expand upon this by adding more features, such as task persistence (saving tasks to a database), error handling, and more sophisticated task querying capabilities.

APScheduler's versatility makes it suitable for a wide range of scheduling needs, from simple one-off tasks to complex, cron-like scheduling, making it an excellent choice for backend task management in applications.

@xmnlab xmnlab added psf-grant-proposal Issues used for a PSF grant proposal size:large >16h labels Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
psf-grant-proposal Issues used for a PSF grant proposal size:large >16h
Projects
None yet
Development

No branches or pull requests

2 participants