Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly detect and then remove jobs that cannot be recovered using Quartz.net?

For various valid reasons, some jobs in the job store are old and can no longer be recovered. For instance, when the Job class is no longer part of the .NET assemblies after a refactor. I'm wondering how to gracefully catch these problems when the scheduler starts, and then delete the unrecoverable jobs.

When the app starts, I basically do this (abridged):

IScheduler scheduler = <create a scheduler and a jobstore object>

try{ scheduler.Start() } catch {}
try{ scheduler.Start() } catch {}
try{ scheduler.Start() } catch {}

If I call Start() three times, the scheduler eventually starts. The reason I have to do this hacky thing is because Start() will throw exceptions for unrecoverable, old jobs.

Failure occured during job recovery. and Could not load type 'MyOldClassName' from assembly 'MyAssembly'.

I want to gracefully remove the broken jobs and avoid these exceptions. In my actual code, I log these exceptions.

Is there a better way to do this?

like image 819
101010 Avatar asked Oct 13 '14 21:10

101010


1 Answers

I found one way to do this. Calling this before Start() cures the problem.

    var jobs = this._scheduler.GetJobKeys(GroupMatcher<JobKey>.AnyGroup());
    foreach (var jobKey in jobs)
    {
        try
        {
            // attempt to access the jobType.  If it fails, then we know it's broken
            Type t = _scheduler.GetJobDetail(jobKey).JobType;
        }
        catch (JobPersistenceException ex)
        {
            if (ex.InnerException != null)
            {
                if (ex.InnerException.GetType() == typeof(TypeLoadException))
                {
                    _scheduler.DeleteJob(jobKey);
                }
            }
            else
            {
                // log this
            }
        }
        catch (Exception ex)
        {
            // log this
        }
    }
like image 194
101010 Avatar answered Nov 02 '22 23:11

101010