Hangfire Discussion

Error creating a Recurring Job

hangfire-pro
recurring
sql-server
Tags: #<Tag:0x00007f84f99b5bb0> #<Tag:0x00007f84f99b5a70> #<Tag:0x00007f84f99b5930>

#1

I am having a weird issue. I think this must be something on my end since I was able to create the recurring job before and now I am updating it ti have the cancellation token.

I have a console app that is creating a recurring job

       public  static void Main(string[] args)
         {
             GlobalConfiguration.Configuration.UseSqlServerStorage("Hangfire");
             RecurringJob.AddOrUpdate<PathExistsBatch>("DO Backend Job - Path Exists", x => x.RunBatch(JobCancellationToken.Null), "0 1 * * *", "default".ToLower());
 
 }

I get the following Errors and the x.RunBatch(JobCancellationToken.Null) is highlighted.
• Cannot implicitly convert type ‘void’ to ‘System.Threading.Tasks.Task’
• Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type

Path Exists code

    public class PathExistsBatch : DNABatchJob 
        {

            public override void RunBatch(IJobCancellationToken cancellationToken)
            {
                string sqlCommandTxt = string.Format(@" Select ShareId, Path from SomeTable  order by 1");
                SqlConnection sqlConnection = new SqlConnection(ConnectionString);

                sqlConnection.Open();
                SqlCommand sqlCommand = new SqlCommand()
                {
                    Connection = sqlConnection
                };

                sqlCommand.CommandText = sqlCommandTxt;

                SqlDataAdapter adp = new SqlDataAdapter(sqlCommandTxt, sqlConnection);
                System.Data.DataTable dt = new System.Data.DataTable();
                adp.Fill(dt);

                var hangFireClient = new BackgroundJobClient();
                string queueName = GetSpecificQueue();

                var jobState = new EnqueuedState(queueName);

                string batchId = Hangfire.BatchJob.StartNew(x =>
                {

                    Parallel.ForEach(dt.AsEnumerable(), drow =>
                    {
                        var shareId = (long)drow["shareId"];
                        var path = drow["path"].ToString();
                        // Send to HangFire
                        hangFireClient.Create<PathExistsJob>(j => j.CheckPath(shareId, path), jobState);
                    });
                });

                BatchJob.AwaitBatch(batchId, y =>
                {
                    y.Enqueue(() => EmailUtils.Send("Path Exists Job Done", "Path Exists Job Done"));
                }
                );
            }
        }

#2

You are getting compile-time errors, because argument is missing for the TimeZoneInfo parameter, you can fix the problem as shown below.

// Add name for the `queue` argument
RecurringJob.AddOrUpdate<PathExistsBatch>(
    "DO Backend Job - Path Exists", 
    x => x.RunBatch(JobCancellationToken.Null), 
    "0 1 * * *", 
    queue: "default".ToLower());

// Or add the TimeZoneInfo argument
RecurringJob.AddOrUpdate<PathExistsBatch>(
    "DO Backend Job - Path Exists", 
    x => x.RunBatch(JobCancellationToken.Null), 
    "0 1 * * *", 
    TimeZoneInfo.Utc,
    "default".ToLower());

#3

awesome. That did the trick.