Domanda

Ho un lavoro in Quartz.Net che si avvia abbastanza frequentemente e talvolta funziona a lungo, come posso annullare il trigger se il lavoro è già in esecuzione?

È stato utile?

Soluzione

Il modo più standard è usare IInterruptableJob, vedi http://quartznet.sourceforge.net /faq.html#howtostopjob . Ovviamente questo è solo un altro modo per dire se (! JobRunning) ...

Altri suggerimenti

Non potresti semplicemente impostare una sorta di variabile globale (jobRunning = true) quando il lavoro viene avviato e riportarlo su false una volta terminato?

Quindi quando si attiva il trigger, esegui il codice if (jobRunning == false)

La tua app potrebbe rimuoversi dall'elenco dei lavori all'avvio e inserirsi all'arresto.

Al giorno d'oggi puoi utilizzare " WithMisfireHandlingInstructionIgnoreMisfires " nel trigger e utilizzare l'attributo [DisallowConcurrentExecution] nel proprio lavoro.

Questa è stata la mia implementazione (usando i suggerimenti sul link che MarkoL ha dato in precedenza).

Sto solo cercando di salvare un po 'di battitura.

Sono abbastanza nuovo su Quartz.NET, quindi prendi il seguito con un treno di sale.

public class AnInterruptableJob : IJob, IInterruptableJob
{

    private bool _isInterrupted = false;

    private int MAXIMUM_JOB_RUN_SECONDS = 10;

    /// <summary> 
    /// Called by the <see cref="IScheduler" /> when a
    /// <see cref="ITrigger" /> fires that is associated with
    /// the <see cref="IJob" />.
    /// </summary>
    public virtual void Execute(IJobExecutionContext context)
    {


        /* See http://aziegler71.wordpress.com/2012/04/25/quartz-net-example/ */

        JobKey key = context.JobDetail.Key;

        JobDataMap dataMap = context.JobDetail.JobDataMap;

        int timeOutSeconds = dataMap.GetInt("TimeOutSeconds");
        if (timeOutSeconds <= 0)
        {
            timeOutSeconds = MAXIMUM_JOB_RUN_SECONDS;
        }

        Timer t = new Timer(TimerCallback, context, timeOutSeconds * 1000, 0);


        Console.WriteLine(string.Format("AnInterruptableJob Start : JobKey='{0}', timeOutSeconds='{1}' at '{2}'", key, timeOutSeconds, DateTime.Now.ToLongTimeString()));


        try
        {
            Thread.Sleep(TimeSpan.FromSeconds(7));
        }
        catch (ThreadInterruptedException)
        {
        }


        if (_isInterrupted)
        {
            Console.WriteLine("Interrupted.  Leaving Excecute Method.");
            return;
        }

        Console.WriteLine(string.Format("End AnInterruptableJob (should not see this) : JobKey='{0}', timeOutSeconds='{1}' at '{2}'", key, timeOutSeconds, DateTime.Now.ToLongTimeString()));

    }


    private void TimerCallback(Object o)
    {
        IJobExecutionContext context = o as IJobExecutionContext;

        if (null != context)
        {
            context.Scheduler.Interrupt(context.FireInstanceId);
        }
    }

    public void Interrupt()
    {
        _isInterrupted = true;
        Console.WriteLine(string.Format("AnInterruptableJob.Interrupt called at '{0}'", DateTime.Now.ToLongTimeString()));
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top