SAGA icon indicating copy to clipboard operation
SAGA copied to clipboard

Strange compensation behavior

Open xfg opened this issue 5 years ago • 1 comments

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.

xfg avatar Feb 22 '20 13:02 xfg