Last Execution time of Hangfie job

I have scheduled job using Hangfire from Startup.cs file using Asp.net MVC. Whenever I try to get last execution time of job, it gives current date-time. Can anyone help me out how to get last execution time of job. 0 0 */7 * * is my cron expression.

@karpit_patel, did you find a solution, I have the same issue.

Hey,
Use this class. This worked for me

///


/// This class used for fetching all the required data from Hangfire database only.
///

public abstract class RecurringJobBase
{
#region private variable

    private ILoggingProvider _log { get; set; }
    private const string JobNotfoundWithJobIdError = "Background job does not exist with jobid: {0}";

    /// <summary>
    /// The previous job date identifier is being used to set the last job id which will match the Hash table of the
    /// Hangfire Database.
    /// </summary>
    private const int ThePreviousJobDateId = 1;

    #endregion private variable

    #region ctor

    public RecurringJobBase(ILoggingProvider log)
    {
        _log = log;
    }

    #endregion ctor

    /// <summary>
    /// This method is used for retrieving data transfer object for recurring job besed on the job key from
    /// Hangfire database.
    /// </summary>
    /// <param name="jobId">The job identifier.(constant class: HangFireJobKeys).</param>
    /// <returns></returns>
    /// <exception cref="System.Exception"></exception>
    protected RecurringJobDto GetJob(string jobId)
    {
        using (var connection = JobStorage.Current.GetConnection())
        {
            var currentJob = connection.GetRecurringJobs().FirstOrDefault(p => p.Id == jobId);
            if (currentJob == null)
            {
                _log.Error(string.Format(JobNotfoundWithJobIdError, jobId));
                throw new Exception(string.Format(JobNotfoundWithJobIdError, jobId));
            }
            return currentJob;
        }
    }

    /// <summary>
    /// This method returns all the job data of previous execution of job
    /// based on the lastJobId.
    /// </summary>
    /// <param name="lastJobid">The last jobid.(integer value belongs to Hangfire database).</param>
    /// <returns></returns>
    protected JobData GetPreviousJob(string lastJobid)
    {
        using (var connection = JobStorage.Current.GetConnection())
        {
            var previousJob = connection.GetJobData((Convert.ToInt32(lastJobid) - ThePreviousJobDateId).ToString());
            return previousJob;
        }
    }

    /// <summary>
    /// This function will return last execution time of job based on the job key.
    /// If it runs for the first time then it will return min value of date time.
    /// </summary>
    /// <param name="jobId">The job key is the identifier (constant class: HangFireJobKeys).</param>
    /// <returns></returns>
    protected DateTime GetLastExecutionTime(string jobId)
    {
        var currentJob = GetJob(jobId);
        var currentjobLastData = GetPreviousJob(currentJob.LastJobId);
        if (currentjobLastData != null)
        {
            return currentjobLastData.CreatedAt;
        }

        return (DateTime)SqlDateTime.MinValue;
    }
}
1 Like

Has this solution worked for you?