GeXiaoguo / Hangfire.MAMQSqlExtension

Enables running multiple Hangfire servers as multiple applications against a single SQLServer
MIT License
51 stars 12 forks source link

Console app server to process one queue doesn't work? #4

Closed ghost closed 2 years ago

ghost commented 2 years ago

Should this not work? I'm not seeing anything on the console. Same issue with/without the MAMQ extension. Ultimate goal is to implement multiple servers each processing a specific queue using HangFire and MS SQL. Each queue can have scheduled jobs and non-scheduled jobs.

public class Export
    {
        [Queue("export")]
        public void AddToQueue()
        {
            BackgroundJob.Enqueue(() => Console.WriteLine("export!"));
        }
    }

public class Import
    {
        [Queue("import")]
        public void AddToQueue()
        {
            BackgroundJob.Enqueue(() => Console.WriteLine("import!"));
        }
    }

class Program
    {
        static void Main()
        {
            GlobalConfiguration.Configuration
                .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
                .UseColouredConsoleLogProvider()
                .UseSimpleAssemblyNameTypeSerializer()
                .UseRecommendedSerializerSettings()
                .UseMAMQSqlServerStorage("Server=.\\SQLEXPRESS;Database=HangfireTest; Integrated Security=True;", new SqlServerStorageOptions
                {
                    CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
                    SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
                    QueuePollInterval = TimeSpan.Zero,
                    UseRecommendedIsolationLevel = true, 
                }, new[] { "import" });

            GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 0 });

            RecurringJob.RemoveIfExists("ImportJob");
            RecurringJob.RemoveIfExists("ExportJob");
            RecurringJob.AddOrUpdate("ImportJob", () => new Import().AddToQueue(), Cron.Minutely, queue: "import");
            RecurringJob.AddOrUpdate("ExportJob", () => new Export().AddToQueue(), Cron.Minutely, queue: "export");

            var options = new BackgroundJobServerOptions
            {
                Queues = new[] { "import" }
            };
            var server = new BackgroundJobServer(options);
            Console.ReadLine();
            server.Dispose();
        }
    }
GeXiaoguo commented 2 years ago

Not sure why it does not work, but just to give some hints to start the investigation:

  1. instead of calling BackgroundJob.Enqueue(() => Console.WriteLine("export!")); in the job. Try writing to the console directly.
  2. try making public void AddToQueue() static. This removes the need to deserialize public class Export and eliminates the possible class deserialization error.
  3. in addition to writing to the console, try putting a breakpoint in the job. This eliminates the possible console writing error.
  4. both RecurringJob.AddOrUpdate and public void AddToQueue() have specified the queue attribute. Not sure if this would cause any problem, but worthing removing the duplicate
  5. try to enable logging and investigating what's in it
  6. try starting up also the dashboard, which will usually show the error for jobs.
  7. if nothing works. the last resort is to download the hangfire source code and run this console app as part of the hangfire test and do live debugging into hangfire.
ghost commented 2 years ago

Thanks! Stupid of me not realizing I had the BackgroundJob.Enqueue in the job as well.