### Install sqlalchemy-celery-beat from Source Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md Installs the sqlalchemy-celery-beat package by cloning the repository and running the setup script. This is useful for development or using the latest unreleased code. ```Shell $ git clone git@github.com:farahats9/sqlalchemy-celery-beat.git $ cd sqlalchemy-celery-beat $ python setup.py install ``` -------------------------------- ### Install sqlalchemy-celery-beat Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md Installs the sqlalchemy-celery-beat package using pip. This is a prerequisite for using the library. ```Shell $ pip install sqlalchemy-celery-beat ``` -------------------------------- ### Install Celery and SQLAlchemy Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md Installs the necessary Celery and SQLAlchemy libraries. Celery should be version 5.0 or higher, and SQLAlchemy should be version 1.4 or higher. ```Shell $ pip install sqlalchemy celery ``` -------------------------------- ### Configure Celery with MySQL Database URI Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md Provides an example of configuring the Celery beat scheduler to use a MySQL database. Requires the 'mysql-connector' package to be installed. ```Python # MySQL: `pip install mysql-connector` beat_dburi = 'mysql+mysqlconnector://root:root@127.0.0.1:3306/celery-schedule' ``` -------------------------------- ### Configure Celery with PostgreSQL Database URI Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md Provides an example of configuring the Celery beat scheduler to use a PostgreSQL database. Requires the 'psycopg2' package to be installed. ```Python # PostgreSQL: `pip install psycopg2` beat_dburi = 'postgresql+psycopg2://postgres:postgres@127.0.0.1:5432/celery-schedule' ``` -------------------------------- ### Start Celery Beat with Database Scheduler Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md Starts the Celery beat scheduler using the SQLAlchemy scheduler. It specifies the scheduler class and logging level. ```Shell $ celery -A tasks beat -S sqlalchemy_celery_beat.schedulers:DatabaseScheduler -l info ``` -------------------------------- ### Start Celery Worker Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md Starts a Celery worker service. Ensure you specify your project name. This service is necessary for executing the periodic tasks. ```bash $ celery -A [project-name] worker --loglevel=info ``` -------------------------------- ### Pass Arguments to SQLAlchemy Engine Creation Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md Demonstrates how to pass arguments to the SQLAlchemy engine creation process via the Celery configuration. This example enables verbose output by setting 'echo' to True. ```Python celery.conf.update({ 'beat_dburi': beat_dburi, 'beat_engine_options': { 'echo': True }, ... }) ``` -------------------------------- ### Start Celery Worker Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md Starts a Celery worker process. This command assumes your Celery application is named 'tasks' and is located in the current directory. ```Shell $ celery -A tasks worker -l info ``` -------------------------------- ### Start Celery Beat Service with Database Scheduler Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md Starts the Celery beat service as a separate process. It uses the `sqlalchemy_celery_beat.schedulers:DatabaseScheduler` to manage periodic tasks from a database. You need to specify your project name and can set the log level. ```bash $ celery -A [project-name] beat -l info --scheduler sqlalchemy_celery_beat.schedulers:DatabaseScheduler ``` -------------------------------- ### Configure Celery with SQLAlchemy Database URI Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md Configures the Celery application to use a specific SQLAlchemy database URI for the beat scheduler. This example shows how to set the database connection string. ```Python from celery import Celery celery = Celery('tasks') beat_dburi = 'sqlite:///schedule.db' celery.conf.update({ 'beat_dburi': beat_dburi, 'beat_schema': None # you can make the scheduler tables under different schema (tested for postgresql, not available in sqlite) }) ``` -------------------------------- ### Create Periodic Task with Args and Expiry (Python) Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md This example illustrates creating a periodic task with additional parameters such as arguments, keyword arguments, and an expiry time. Arguments and keyword arguments must be JSON serialized. The expiry time is set using Python's datetime objects. ```Python >>> import json >>> from datetime import datetime, timedelta, timezone >>> periodic_task = PeriodicTask( ... schedule_model=schedule, # we created this above. ... name='Importing contacts', # simply describes this periodic task. ... task='proj.tasks.import_contacts', # name of task. ... args=json.dumps(['arg1', 'arg2']), ... kwargs=json.dumps({ ... 'be_careful': True, ... }), ... expires=datetime.now(tz=timezone.utc) + timedelta(seconds=30) ... ) ... session.add(periodic_task) ... session.commit() ``` -------------------------------- ### Disable Periodic Task (Python) Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md This example shows how to temporarily disable a periodic task by setting its `enabled` flag to `False`. The change is then committed to the session. ```Python >>> periodic_task.enabled = False >>> session.add(periodic_task) >>> session.commit() ``` -------------------------------- ### Run Celery Beat (Older Versions) Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md Command to run the Celery beat scheduler for versions prior to Celery 5.0, using the custom DatabaseScheduler. ```Shell $ celery beat -A tasks:celery -S tasks:DatabaseScheduler -l info ``` -------------------------------- ### Run Celery Beat (Celery >= 5.0) Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md Command to run the Celery beat scheduler for Celery version 5.0 and above, using the custom DatabaseScheduler. ```Shell $ celery -A tasks:celery beat -S tasks:DatabaseScheduler -l info ``` -------------------------------- ### Create Periodic Task (Python) Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md This code shows how to create a basic periodic task using a previously defined schedule. It includes the schedule model, a descriptive name for the task, and the specific task to be executed. ```Python >>> task = PeriodicTask( ... schedule_model=schedule, # we created this above. ... name='Importing contacts', # simply describes this periodic task. ... task='proj.tasks.import_contacts', # name of task. ... ) >>> session.add(task) >>> session.commit() ``` -------------------------------- ### Run Celery Worker (Celery >= 5.0) Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md Command to run a Celery worker for Celery version 5.0 and above. It specifies the application and logging level. ```Shell $ celery -A tasks:celery worker -l info ``` -------------------------------- ### Run Celery Worker (Older Versions) Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md Command to run a Celery worker for versions prior to Celery 5.0. It specifies the application and logging level. ```Shell $ celery worker -A tasks:celery -l info ``` -------------------------------- ### Create Interval-Based Periodic Task Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md Python code snippet demonstrating how to create an interval schedule for a periodic task using sqlalchemy-celery-beat. It involves creating an IntervalSchedule object and a session to interact with the database. ```Python >>> from sqlalchemy_celery_beat.models import PeriodicTask, IntervalSchedule, Period >>> from sqlalchemy_celery_beat.session import SessionManager >>> from celeryconfig import beat_dburi >>> session_manager = SessionManager() >>> session = session_manager.session_factory(beat_dburi) ``` -------------------------------- ### Create Interval Schedule (Python) Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md This snippet demonstrates how to create or retrieve an interval schedule for Celery Beat tasks. It defines a schedule that executes every 10 seconds using SQLAlchemy models. If the schedule doesn't exist, it's created and added to the session. ```Python >>> schedule = session.query(IntervalSchedule).filter_by(every=10, period=Period.SECONDS).first() >>> if not schedule: ... schedule = IntervalSchedule(every=10, period=Period.SECONDS) ... session.add(schedule) ... session.commit() ``` -------------------------------- ### Create Crontab Schedule (Python) Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md This snippet demonstrates how to define a crontab schedule for Celery Beat tasks. It allows specifying minute, hour, day of week, day of month, month of year, and timezone for task execution, mimicking standard cron syntax. ```Python >>> from sqlalchemy_celery_beat.models import PeriodicTask, CrontabSchedule >>> schedule = CrontabSchedule( ... minute='30', ... hour='*', ... day_of_week='*', ... day_of_month='*', ... month_of_year='*', ... timezone='UTC', ... ) ``` -------------------------------- ### Create Periodic Task with Crontab Schedule (Python) Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md This code shows how to create a periodic task using a crontab schedule. It highlights the use of the generic foreign key relationship via `schedule_model`, which simplifies linking different schedule types to tasks. ```Python >>> periodic_task = PeriodicTask( ... schedule_model=schedule, ... name='Importing contacts', ... task='proj.tasks.import_contacts', ... ) ``` -------------------------------- ### Periodic Task with Schedule ID and Discriminator (Python) Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md This snippet illustrates the underlying mechanism for linking schedules to tasks, showing how `schedule_id` and `discriminator` are used. It's an alternative to using the `schedule_model` property. ```Python >>> periodic_task = PeriodicTask( ... schedule_id=schedule.id, ... discriminator=schedule.discriminator, ... name='Importing contacts', ... task='proj.tasks.import_contacts', ... ) ``` -------------------------------- ### Update Periodic Tasks and Notify Scheduler (Python) Source: https://github.com/farahats9/sqlalchemy-celery-beat/blob/master/README.md This code demonstrates how to handle bulk updates to periodic tasks and ensure the Celery Beat scheduler is aware of these changes. It uses `update_from_session` after executing a bulk update statement. ```Python from sqlalchemy_celery_beat.models import PeriodicTaskChanged from sqlalchemy_celery_beat.session import SessionManager, session_cleanup from sqlalchemy import update session_manager = SessionManager() session = session_manager.session_factory(beat_dburi) with session_cleanup(session): stmt = update(PeriodicTask).where(PeriodicTask.name == 'task-123').values(enabled=False) session.execute(stmt) # using execute causes no orm event to fire, changes are in the database but the schduler has no idea session.commit() PeriodicTaskChanged.update_from_session(session) # now scheduler reloads the tasks and all is good ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.