Managing backgroundjobserver life cycle on multi tenant app


#1

Hello,
i am still doin trials on my own while tryin to find the recommended practical life cycle for backgroundjobserver, few background on my app :
each tenant have their own database, and the connectionstring for their account is stored in one master database,
and what i am trying to do is :

  1. after logging in, i create several BackgroundJobServer that have unique queues for their own type of jobs
  2. when i am enqueueing a job, i do :
    • var sqlStorage = new SqlServerStorage(storedConnString);
    • var client = new BackgroundJobClient(sqlStorage);
    • client.enqueue the job
  3. for recurring jobs, i do :
    • RecurringJobManager recurJobM = new RecurringJobManager(sqlStorage);
    • RecurringJobOptions recurJobOpt = new RecurringJobOptions()
      {
      QueueName = “queue_name_for_recurrin_server”
      };
    • recurJobM.AddOrUpdate(“recurring_name”,method,recurJobOpt)
  4. now, my problem is, the recurring is intended to stop working once the user logout, and i tried to do this by removing the server by doing this :
    • var sqlStorage = new SqlServerStorage(storedConnString);
    • var monitoringApi = sqlStorage.GetMonitoringApi();
    • var serverList = monitoringApi.Servers();
    • foreach (var server in serverList)
      {
      var serverConnection = sqlStorage.GetConnection();
      serverConnection.RemoveServer(server.Name);
      }

from hangfire.server table i found out that :
the server is removed, but then reappear again in seconds and continues the recurring jobs,
why did this happen ?
i am also open for other recommended ways to handle BackgroundJobServer life cycle,
as i am new in this


#2

this is how i do it :

  • var sqlStorage = new SqlServerStorage(storedConnString);
  • var optionsForCertainJobServer = new BackgroundJobServerOptions
    {
    ServerName = “ServerName”,
    Queues = new[] { “1_critical”, “2_certainJob” },
    WorkerCount = 1,
    };
  • var CertainJobServer = new BackgroundJobServer(optionsForCertainJobServer,
    sqlStorage);

#3

When the user logs out can you cancel all the jobs? You’ll also have to use the cancellation token inside the job to stop the running jobs.