4

I have a custom event system handler that sends an email when a publish transaction fails. The event is registered like this:

this.EventSystem.Subscribe<PublishTransaction, SaveEventArgs>    
    (this.SendNotificationEmail, EventPhases.TransactionCommitted);

The SendNotificationEmail method looks like this:

private void SendNotificationEmail(PublishTransaction transaction, 
    SaveEventArgs args, EventPhases phases)
{
    if (transaction.Instruction.ResolveInstruction.Purpose == ResolvePurpose.Publish && 
        transaction.State == PublishTransactionState.Failed)
    {
        // Send email
    }        
}

The issue I'm having is that the SendNotificationEmail method is called twice per publication transaction. This leads to multiple emails per failed transaction.

Any ideas why the event would be triggered more than once?

  • Are you publishing to multiple publishing targets? – Saurabh Gangwar Apr 22 '15 at 17:30
  • When I publish to multiple targets, the event is triggered twice per target. – Andy Wilson Apr 22 '15 at 17:44
3

In general, the Save event is called for any save (change) of the PublishTransaction, not only for state changes. I'm not sure why a second save happens when the state is Failed (perhaps additional info is received from CD transport/deployment).

Anyways, your implementatiom should not rely on a single Save event, but detect the state change. You could do that by hooking into the Initiated phase of the save and, if the state is Failed, load another instance of the PublishTransaction to test if the state before the save event occurred was already Failed or not.

| improve this answer | |
  • This makes sense. I was assuming (without any basis) that each event would only be called once per event phase. To solve my issue, I ended up caching each transaction id, and only sending the email the first time the event is triggered. – Andy Wilson Apr 22 '15 at 17:51
1

While publishing an item, each publishing target results in a transaction. In your code you are subscribing SendNotificationEmail method per transaction.

So If you publish to a multiple publishing targets, the "SendNotificationEmail" method will be called for each target(or transaction) and if it fails, the email will be send per target(or transaction) and will result in multiple emails.

| improve this answer | |
  • I'm getting two emails per target, though. If I publish to one target, I get 2 emails; if I publish to two targets, I get 4 emails, etc. – Andy Wilson Apr 22 '15 at 17:42
  • If you publish to multiple targets, you will get multiple Publish Transactions (one per target). – Rick Pannekoek Apr 22 '15 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.