celery / jumpstarter

MIT License
7 stars 3 forks source link

Actor Tasks #7

Open thedrow opened 3 years ago

thedrow commented 3 years ago

An actor repeatedly runs tasks to fulfill its purpose. Using tasks, the user implements the business logic of the Actor. A task can be synchronous or asynchronous. If the task is synchronous, the task is run in a thread pool. If it is asynchronous, the task runs using the event loop.

Currently, there are three types of tasks. There may be more in the future.

Continuous Tasks

These types of tasks continually run after start() until the actor is stopping -> tasks_stopped. After each iteration they yield control back to the event loop so that other co-routines may run.

The tasks are run as long as the cancel scope is not canceled which happens just before reaching the stopping -> tasks_done state. This allows us to either cancel after an iteration was made or if we we're in the midst of an I/O operation.

Periodic Tasks

These type of tasks run every specified interval until the actor is stopping -> tasks_stopped.

The tasks are scheduled using APScheduler 4.0 which is not ready for production at the time being. Follow https://github.com/agronholm/apscheduler/issues/465 for updates.

The tasks are run as long as the scheduler is running and the cancel scope is not canceled which happens just before reaching the stopping -> tasks_done state.

Conditional Tasks

These type of tasks run only when the actor is in a certain state(s). A conditional task has its own shielded cancel scope which is canceled whenever the actor transitions to a state which prevents the task from running.

If we cancel the uppermost cancel scope, we must cancel these type of tasks manually.

A conditional task can also be combined with a periodic task or with a continuous task. This is useful for later on when we introduce circuit breakers and health checks.