Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make compensation if something went wrong during current saga step execution #49

Open
imissyouso opened this issue Dec 25, 2019 · 3 comments

Comments

@imissyouso
Copy link

imissyouso commented Dec 25, 2019

Hi.
For example, on step 3 of my saga I sent a new command 'createTicket' but ticket microservice rejected it and thrown Error, I logged it and now want to undo all previous saga steps (by sending compensation commands like makeRefund, deleteMessage etc). How to implement such scenario?

by the way: your pack of libraries is awesome, it wonders me why it has so small amount of stars.

@imissyouso
Copy link
Author

imissyouso commented Dec 25, 2019

My current implementation:
I created Saga step named commandReject with the code

module.exports = require('cqrs-saga').defineSaga({// event to match..
    existing: true, // if true it will check if there is already a saga in the db and only if there is something it will continue...
    payload: 'payload', // if not defined it will pass the whole event...
    id: 'payload.command.payload.transactionId', // if not defined it will generate an id
    priority: 2 // optional, default Infinity, all sagas will be sorted by this value
}, function (evt, saga, callback) {
    console.log('saga compensation start');

    // I put my first compensation command here and execute all next by chain.

    saga.commit(callback);
});

Is that way is correct?

@imissyouso
Copy link
Author

imissyouso commented Dec 25, 2019

Or another way:
I execute all compensation command in parallel, pay attention on switch breaks.

module.exports = require('cqrs-saga').defineSaga({// event to match..
    existing: true, // if true it will check if there is already a saga in the db and only if there is something it will continue...
    payload: 'payload', // if not defined it will pass the whole event...
    id: 'payload.command.payload.transactionId', // if not defined it will generate an id
    priority: 2 // optional, default Infinity, all sagas will be sorted by this value
}, function (evt, saga, callback) {
    console.log('saga compensation start');

    switch (evt.command.command) {
        case 'attachMessageToTicket':
            saga.addCommandToSend({
                command: 'deleteTicket',
                payload: {
                    transactionId: saga.id,
                    id: saga.get('ticketId'),
                }
            });
        case 'createTicket':
            console.log(evt.command.payload);
            saga.addCommandToSend({
                command: 'deleteContact',
                payload: {
                    transactionId: saga.id,
                    id: saga.get('contactId'),
                }
            });
        case 'createContact':
            saga.addCommandToSend({
                command: 'deleteMessage',
                payload: {
                    transactionId: saga.id,
                    id: saga.get('messageId'),
                }
            });
            break;
        default:
            callback("No compensation event found for "+evt.command.command);
            return;
    }

    saga.destroy();
    saga.commit(callback);
});

@adrai
Copy link
Contributor

adrai commented Dec 26, 2019

Yes, I would recommend to handle a failed command as a new event...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants