1

I am writing a Translation Manager Event Handler to write some custom business logic on Start and Finish of the translation job. I have tried below code to analyse the different states of the translation job :

[TranslationManagerPlugIn]
public class TMPlugin
{
public TMPlugin()
    {
        //Subscribe to Translation Manager events
        TranslationJobManager.TranslationJobCreated += JobCreated;
        TranslationJobManager.TranslationJobLoaded += JobCreated;           
    }
    private void JobCreated(object sender, TranslationJobEventArgs e)
    {         
        TranslationJob job = e.TranslationJob;

        job.StateChanged += State_Changed;          

    }

    private void State_Changed(object sender, EventArgs e)
    {
        var job = (TranslationJob)sender;
        if (job.State == TranslationJobState.Completed)
        {
             log.Debug("Transalation Job Completed ");
        }
        else if(job.State == TranslationJobState.Canceled)
        {
            log.Debug("Transalation Job Canceled ");
        }
        else if (job.State == TranslationJobState.Definition)
        {
            log.Debug("Transalation Job Definition ");
        }
        else if (job.State == TranslationJobState.InTranslation)
        {
           log.Debug("Transalation Job InTranslation " );
        }
    }                           
}

The code works well, but the problem is that the code is being invoked several times for a single Translation Job and that too for a single state.

Let’s say I have initiated a job with ID 123, then I am getting the debug msg for “Definition” state but after few minutes I am getting the same Debug message for the same job. The same thing happened with “In Translation” state, it printed somewhere around 15 times the same debug message for the that job ID.

Also I have noticed that if I am initiating a new job say 456, the code runs and print the debug logs for all the previous jobs which are in queue with their current states.

My understanding was that for a particular job ID the code will execute only once & when it will change the state it will print the Debug msg for the next state, in that way I can write my Start logic on “Definition” or “In Translation” state and Finish logic on “Completed” state.

Can someone suggest if I am doing something wrong or subscribing to the wrong events?

Thanks

  • side-note: var job = (TranslationJob)sender; would be better as TranslationJob job = (TranslationJob)sender; – Dylan .. Mark Saunders Mar 14 '16 at 23:58
2

Logically your code is fine. But Instead of EventArgs I suggest you to use TranslationJobStateChangeEventArgs. Hopefully It will resolve your problem..

Here is the code for reference -

   public void TranslationJobManagerInitiated(object sender, TranslationJobEventArgs e)
     {
       e.TranslationJob.StateChanged += OnStateChanged;
     }

    private void OnStateChanged(object sender, TranslationJobStateChangeEventArgs e)
    {
       TranslationJob job = (TranslationJob)sender;
  • Thanks to all of you for providing your inputs, the solution suggested by Sayantan worked well. – Yash Mar 17 '16 at 8:43
4

The events in Translation Manager versions before SDL Web 8 are basic .NET events.

Most likely the reason you see the event raised multiple times is because the job has been loaded more than once - and you subscribe to the event each time.

Unfortunately there isn't a clean way to unsubscribe as neither TranslationJob nor the session implements IDisposable. An option would be to use Weak Events, that will help somewhat, but not completely eliminate the problem (it is still a good idea to do from a memory management consideration, bu translation jobs are by nature not the most frequent beasts around so you probably won't really notice).

To ensure you only process a state change once, you can create a Dictionary linking job id to the last known state. On state change you can then compare the value in the dictionary with the new state to see if it is a new state. Remember to use locking, Translation Manager is multithreaded.

Or you can just upgrade to SDL Web 8 and use the new API :)

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.