Skip to content

Strange compensation behavior #2

Description

@xfg

I created the saga

module.exports = {
  id: 'createTournamentSaga',
  flow: [
    {
      id: "create tournament",
      transaction: async ({ initial, transactionValues }) => {
        const command = new CreateTournamentCommand(
          initial.playerId,
        );

        transactionValues.tournamentId = await tournamentWriteService.create(command);
        return transactionValues;
      },
      compensation: async ({ fullTransactionValues }) => {
        if (fullTransactionValues.tournamentId) {
          const command = new RemoveTournamentCommand(
            fullTransactionValues.tournamentId,
          );

          await tournamentWriteService.remove(command);
        }
      },
    },
    {
      id: "add warrior to tournament",
      transaction: async ({ initial, transactionValues, fullTransactionValues }) => {
        const command = new CreateWarriorCommand(
          initial.playerId,
          fullTransactionValues.tournamentId
        );

        transactionValues.warriorId = await warriorWriteService.create(command);
        return transactionValues;
      },
      compensation: async ({ fullTransactionValues }) => {
        if (fullTransactionValues.warriorId) {
          const command = new RemoveWarriorCommand(
            fullTransactionValues.warriorId
          );

          await warriorWriteService.remove(command);
        }
      },
    },
    {
      id: "change the tournament status to wait",
      transaction: async ({ fullTransactionValues }) => {
        const command = new TournamentWaitCommand(
          fullTransactionValues.tournamentId
        );

        await tournamentWriteService.wait(command);
      },
      compensation: async ({ initial }) => {

      }
    }
  ]
};

When I run this saga I sometimes get the next saga logs

{
	"_id" : ObjectId("5e506a95156a9e007063f99a"),
	"createdDate" : ISODate("2020-02-21T23:41:09.139Z"),
	"items" : [
		{
			"transaction" : true,
			"isBegin" : true
		},
		{
			"transaction" : true,
			"isStep" : true,
			"stage" : "STAGE_PENDING",
			"step" : 0
		},
		{
			"transaction" : true,
			"isStep" : true,
			"stage" : "STAGE_COMPLETED",
			"step" : 0,
			"result" : {
				"tournamentId" : "6636766627465527296"
			}
		},
		{
			"transaction" : true,
			"isStep" : true,
			"stage" : "STAGE_PENDING",
			"step" : 1
		},
		{
			"transaction" : true,
			"isStep" : true,
			"stage" : "STAGE_COMPLETED",
			"step" : 1,
			"result" : {
				"tournamentId" : "6636766627465527296",
				"warriorId" : "6636766627524247552"
			}
		},
		{
			"transaction" : true,
			"isStep" : true,
			"stage" : "STAGE_PENDING",
			"step" : 2
		},
		{
			"transaction" : true,
			"isStep" : true,
			"stage" : "STAGE_COMPLETED",
			"step" : 2,
			"result" : null
		},
		{
			"compensation" : true,
			"isBegin" : true
		},
		{
			"compensation" : true,
			"isStep" : true,
			"stage" : "STAGE_PENDING",
			"step" : 2
		},
		{
			"compensation" : true,
			"isStep" : true,
			"stage" : "STAGE_COMPLETED",
			"step" : 2,
			"result" : null
		},
		{
			"compensation" : true,
			"isStep" : true,
			"stage" : "STAGE_PENDING",
			"step" : 1
		},
		{
			"compensation" : true,
			"isStep" : true,
			"stage" : "STAGE_COMPLETED",
			"step" : 1,
			"result" : null
		},
		{
			"compensation" : true,
			"isStep" : true,
			"stage" : "STAGE_PENDING",
			"step" : 0
		},
		{
			"compensation" : true,
			"isStep" : true,
			"stage" : "STAGE_COMPLETED",
			"step" : 0,
			"result" : null
		},
		{
			"compensation" : true,
			"isEnd" : true
		}
	]
}

I am confused why compensation steps are ran when there are no errors. I can not debug this behavior because it is floating. Sometimes there is, sometimes not. I read this code and I don't understand how can it get a compensation scenario.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions