nodecosmos / charybdis

Rust ORM for ScyllaDB and Apache Cassandra
MIT License
128 stars 8 forks source link

feat: init saga feature (experiment) #9

Closed GoranBrkuljan closed 6 months ago

GoranBrkuljan commented 6 months ago

Within our callbacks we could add single CallbackContext argument that holds references to session & extensions, along with Saga data structure. Within callbacks user can add saga steps that in case of failure will revert those actions from last step.

Here is sample from nodecosmos:

impl Callbacks for Node {
    type Extension = RequestData;
    type Error = NodecosmosError;

    async fn before_insert(&mut self, ctx: &CallbackContext<'_, Self>) -> Result<(), NodecosmosError> {
        self.set_defaults(ctx.session).await?;

        ctx.saga.add_step(
            self.append_to_ancestors(ctx.session),
            Some(self.remove_from_ancestors(ctx.session)),
        );

        Ok(())
    }

    async fn after_insert(&mut self, ctx: &CallbackContext<'_, Self>) -> Result<(), NodecosmosError> {
        ctx.saga.add_step(
            self.update_branch(ctx.extension),
            Some(self.revert_update_branch(ctx.extension)),
        );
    }
 }

Each step has action field and optional compensating_action in case of failure. They are both futures that are evaluated once callbacks action is triggered.

Current Problems: