Command Palette

Search for a command to run...

UnylyUnyly
Назад к скиллам

cqrs-saga

БесплатноБез исполняемых скриптовНе проверен

Orchestrates a python-cqrs saga whose finished steps compensate on failure, with persistent state and recovery. Use when a flow crosses services or databases…

vadikko2от vadikko2313 установок в других каталогах

Об этом скилле

Saga orchestration

Read cqrs-setup before writing steps. Use a saga when several steps cross services or databases and a finished step has a compensate. A single UPDATE in one service stays a database transaction. Publishing one message reliably is the outbox inside cqrs-eda, not a saga.

The package is already installed. Do not add an install step.

Steps

  1. Put a context object in app/sagas.py. It carries the ids later steps and compensations need.
  2. One class per step, subclassing cqrs.SagaStepHandler[Context, cqrs.Response]. act does the work and returns self._generate_step_result(...). compensate undoes that step.
  3. Subclass cqrs.Saga[Context] and set steps to those classes, in forward order.
  4. Register with mapper.bind(Context, SagaClass) inside sagas_mapper.
  5. Build cqrs.bootstrap.saga.bootstrap(..., sagas_mapper=sagas_mapper, saga_storage=storage) once. When a step takes ports, register the step instances on cqrs.SimpleContainer, or wrap a raw di.Container in cqrs.DIContainer (python-cqrs[di]). Pass that container to bootstrap and the same object to recover_saga. A raw di.Container has no resolve that recovery can call. Production storage is SqlAlchemySagaStorage or SqlModelSagaStorage. MemorySagaStorage does not survive a restart.
  6. Run async for step in mediator.execute(context). Pass the same scope_strategy the application uses for recover_saga.
  7. After a crash, call cqrs.saga.recovery.recover_saga with that storage and the same scope_strategy. Do not start a second saga for the same business id.

Do not

  • Invent OrchestratedSaga, SagaLog, or a Command base.
  • Compensate a step that has not finished act.
  • Call the broker next to session.commit() inside act. A step that publishes writes the outbox.
  • Open a fresh session inside act when ScopeStrategy.SEND already provides one.

Short shape: assets/order_saga.py. Recovery: references/recovery.md. Full example: examples/saga/saga.py. Recovery example: examples/saga/saga_recovery.py.

Context

Subclass cqrs.saga.models.SagaContext and keep the subclass a dataclass. Storage round-trips it with to_dict and from_dict. SagaContext is not an attribute of cqrs. Put the business id and every id a later step or a compensation reads: reservation id, payment id, shipment id. Steps mutate that one instance. They do not return a new context.

cqrs.Saga[Context] checks steps when the subclass is created. Every step must use that same context type. Order is forward order. Compensation runs completed steps in reverse.

A step takes its ports in __init__. The container resolves the step. Do not construct the step inside execute.

Run and resume

Pass a stable saga_id into mediator.execute(context, saga_id=saga_id) when the run must be resumed. The same id is the argument to recover_saga. One business id maps to one saga id. A second execute for that id starts a second history.

recover_saga takes an instance of the saga class, not the class. Pass the original storage, the original container, and the original scope_strategy. Completed steps are skipped. A saga left in compensation continues compensation. Do not call it for a saga that is still running in another worker.

Import it from cqrs.saga.recovery. It is not an attribute of cqrs.

MemorySagaStorage is cqrs.saga.storage.memory.MemorySagaStorage. It is for tests and for a flow that may be lost. Production recovery uses cqrs.saga.storage.sqlalchemy.SqlAlchemySagaStorage (python-cqrs[sqlalchemy]) or cqrs.saga.storage.sqlmodel.SqlModelSagaStorage (python-cqrs[sqlmodel]). Saga storage takes an async_sessionmaker, not the request session the outbox uses.

Compensation

compensate undoes a step whose act already finished. It must be safe to run again with the same context. Read the ids act wrote. Do not start a new forward action inside compensate.

A step that has not finished act is not compensated. The failure itself is the stop. Earlier finished steps are compensated in reverse.

A fallback (cqrs.saga.fallback.Fallback) is a degraded path when act fails. It is not compensation. Compensation undoes a step that already succeeded.

A step that publishes does not call the broker beside session.commit(). It writes the outbox in cqrs-eda, in the session the scope already opened.

How to test

Assert act and compensate on invocations. After a full successful run the saga is completed and running_count is 0. Compensation leaves the saga failed and the step compensated. A fallback is a different path: the failed step is not compensated. harness.sagas.recover resumes without entering a finished act again. Detail is cqrs-testing.

Files and scope

app/sagas.py holds the context, the step classes, the saga class, and sagas_mapper. Do not send that context through RequestMediator.

Pass

Полный SKILL.md на ClawHub

Установить cqrs-saga в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Без ограничений — скилл может использовать любой инструмент.

FAQ

Что делает скилл cqrs-saga?

Orchestrates a python-cqrs saga whose finished steps compensate on failure, with persistent state and recovery. Use when a flow crosses services or databases and a completed step must be undone.

Как установить скилл cqrs-saga?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл cqrs-saga запускает скрипты?

Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.

Похожие скиллы

Сравнить cqrs-saga с