Asynchronous Tasks

Celery tasks of the account app, and how everything else queues them.

  • billing: record charges, aggregate them, and enforce budgets.

  • contacts: invite account contacts to the Slack workspace.

  • data_exports: users’ data exports, and the deletion of expired ones.

  • llm_prices: keep the LLM prices up to date.

  • repositories: the Apply page’s batch apply of a GitHub repository.

  • queue: the queue_* functions, the one place that queues these tasks (ADR-030, rule 10, issue #1098). Celery Beat sends the scheduled ones by their names.

Every task is queued through those functions and nothing else, so they, and not the tasks, are what this package exports. Each task names itself smarter.apps.account.tasks.<function> (TASK_NAME_PREFIX), whatever its module. Code that runs a task synchronously, or a test, imports the task from its own module.

smarter.apps.account.tasks.queue_apply_repository(user_profile, url)[source]

Start a batch apply of a repository, unless one of the user’s is in progress, and queue apply_repository().

The web console’s Apply page calls it. The task is queued once the run’s row is committed, so that the worker finds it.

Parameters:
  • user_profile (UserProfile) – The user, as whom the manifests are applied.

  • url (str) – The repository’s url.

Return type:

tuple[RepositoryApply, bool]

Returns:

The run, and True if it was started now, or False if one was already in progress.

Raises:

RepositoryApplyRefusedError – If the url isn’t a public GitHub repository’s, or the user started REPOSITORY_APPLIES_PER_DAY runs in the last 24 hours.

smarter.apps.account.tasks.queue_create_charge(resource_locator, charge_type, prompt_tokens, completion_tokens, total_tokens, *, provider=None, model=None)[source]

Queue create_charge(), which records a charge against a resource.

The provider’s chat (ChatDbMixin) and the proxy (smarter.apps.proxy.services.record_charges()) call it, once for each resource that a request is charged to. It is a record, so there is nothing to check: the callers decide what is charged.

Parameters:
  • resource_locator (str) – The record locator of the resource that is charged, e.g. an account’s.

  • charge_type (str) – The kind of charge, e.g. prompt_completion.

  • prompt_tokens (int) – The prompt’s tokens.

  • completion_tokens (int) – The completion’s tokens.

  • total_tokens (int) – All of the tokens.

  • provider (str | None) – The LLM provider’s name, to price the tokens with LLMPrices.

  • model (str | None) – The LLM’s name, to price the tokens with LLMPrices.

Return type:

None

smarter.apps.account.tasks.queue_create_data_export(user)[source]

Start a data export for the user, unless one is already in progress, and queue create_data_export().

The web console’s and the api’s Download My Data call it. The task is queued once the export’s row is committed, so that the worker finds it.

Parameters:

user (User) – The user.

Return type:

tuple[DataExport, bool]

Returns:

The export, and True if it was started now, or False if one was already in progress.

Raises:

DataExportStartRefusedError – If the user has no email address, or started DATA_EXPORTS_PER_DAY exports in the last 24 hours.

Queueing the Tasks

The one place that queues the account app’s Celery tasks.

Every task of the app is queued by a function of this module, never by <task>.delay() or <task>.apply_async() anywhere else: not by the web console’s apis, not by the other apps that charge for what they do, the provider’s chat and the proxy, and not by the tasks themselves (ADR-030, rule 10, issue #1098). So this module is where to look for what queues a task, and the one place that a test patches so that nothing reaches a real worker. Celery Beat sends the account’s scheduled tasks by their names.

Each function checks, with reads alone, what can be checked now, so that a mistake is an error to its caller, records what it queued, e.g. a data export’s row, and queues the task once the current transaction commits, so that the worker finds the row. Outside a transaction, the task is queued at once.

smarter.apps.account.tasks.queue.queue_apply_repository(user_profile, url)[source]

Start a batch apply of a repository, unless one of the user’s is in progress, and queue apply_repository().

The web console’s Apply page calls it. The task is queued once the run’s row is committed, so that the worker finds it.

Parameters:
  • user_profile (UserProfile) – The user, as whom the manifests are applied.

  • url (str) – The repository’s url.

Return type:

tuple[RepositoryApply, bool]

Returns:

The run, and True if it was started now, or False if one was already in progress.

Raises:

RepositoryApplyRefusedError – If the url isn’t a public GitHub repository’s, or the user started REPOSITORY_APPLIES_PER_DAY runs in the last 24 hours.

smarter.apps.account.tasks.queue.queue_create_charge(resource_locator, charge_type, prompt_tokens, completion_tokens, total_tokens, *, provider=None, model=None)[source]

Queue create_charge(), which records a charge against a resource.

The provider’s chat (ChatDbMixin) and the proxy (smarter.apps.proxy.services.record_charges()) call it, once for each resource that a request is charged to. It is a record, so there is nothing to check: the callers decide what is charged.

Parameters:
  • resource_locator (str) – The record locator of the resource that is charged, e.g. an account’s.

  • charge_type (str) – The kind of charge, e.g. prompt_completion.

  • prompt_tokens (int) – The prompt’s tokens.

  • completion_tokens (int) – The completion’s tokens.

  • total_tokens (int) – All of the tokens.

  • provider (str | None) – The LLM provider’s name, to price the tokens with LLMPrices.

  • model (str | None) – The LLM’s name, to price the tokens with LLMPrices.

Return type:

None

smarter.apps.account.tasks.queue.queue_create_data_export(user)[source]

Start a data export for the user, unless one is already in progress, and queue create_data_export().

The web console’s and the api’s Download My Data call it. The task is queued once the export’s row is committed, so that the worker finds it.

Parameters:

user (User) – The user.

Return type:

tuple[DataExport, bool]

Returns:

The export, and True if it was started now, or False if one was already in progress.

Raises:

DataExportStartRefusedError – If the user has no email address, or started DATA_EXPORTS_PER_DAY exports in the last 24 hours.

Billing

Celery tasks that record and aggregate charges, and enforce budgets.

Each is queued by a function of smarter.apps.account.tasks.queue, never with .delay() (ADR-030, rule 10), or sent by Celery Beat.

Account Contacts

Celery tasks for account contacts.

Each is queued by a function of smarter.apps.account.tasks.queue, never with .delay() (ADR-030, rule 10), or sent by Celery Beat.

Data Exports

Celery tasks for users’ data exports: Download My Data in the user menu.

Each is queued by a function of smarter.apps.account.tasks.queue, never with .delay() (ADR-030, rule 10), or sent by Celery Beat.

LLM Prices

Celery tasks that keep the LLM prices up to date.

Each is queued by a function of smarter.apps.account.tasks.queue, never with .delay() (ADR-030, rule 10), or sent by Celery Beat.

Repositories

Celery tasks for the Apply page’s batch apply of a GitHub repository.

Each is queued by a function of smarter.apps.account.tasks.queue, never with .delay() (ADR-030, rule 10), or sent by Celery Beat.