title: Get Singleton behavior on WebJobs with custom queue processors
body: <p>My team has been trying to get some of our WebJobs <a href="https://github.com/Azure/azure-webjobs-sdk-samples/blob/master/BasicSamples/MiscOperations/CustomQueueProcessorFactory.cs" rel="nofollow noreferrer">custom queue processors</a> to have Singleton behavior, but we haven't really gotten that behavior with the <code>[Singleton(Mode = SingletonMode.Listener)]</code> attribute, nor with setting <code>QueueProcessorFactoryContext.BatchSize = 1</code>. This is causing nightly processes to slam the database all at once — with many of them timing out — and has become a bit of a headache.</p>
  
  <p>This is more or less what our CustomQueueProcessorFactory looks like:</p>
  
  <pre><code>    public class CustomQueueProcessorFactory : IQueueProcessorFactory
      {
          public QueueProcessor Create(QueueProcessorFactoryContext context)
          {
              if (context == null)
                  throw new ArgumentNullException(nameof(context));
  
              if (context.Queue.Name == Constants.UploadQueueName 
                  || context.Queue.Name == Constants.BuildQueueName)
              {
                  context.BatchSize = 1;
              }
  
              return new QueueProcessor(context);
          }
      }
  </code></pre>
  
  <p>This is referenced when configuring our JobHost:</p>
  
  <pre><code>    var config = new JobHostConfiguration();
      config.Queues.QueueProcessorFactory = new CustomQueueProcessorFactory();
  </code></pre>
  
  <p>We have also set up some functions with <code>QueueTrigger</code>s like:</p>
  
  <pre><code>    public static async Task ExecuteBackgroundRequest([QueueTrigger(Constants.BackgroundQueueName)] BackgroundRequest background, TextWriter logger)
      {
          await ExecuteRequest(background, logger);
      }
  
      [Singleton(Mode = SingletonMode.Listener)]
      public static async Task ExecuteUploadRequest([QueueTrigger(Constants.UploadQueueName)] BackgroundRequest background, TextWriter logger)
      {
          await ExecuteRequest(background, logger);
      }
  
      [Singleton(Mode = SingletonMode.Listener)]
      public static async Task ExecuteBuildRequest([QueueTrigger(Constants.BuildQueueName)] BackgroundRequest background, TextWriter logger)
      {
          await ExecuteRequest(background, logger);
      }
  </code></pre>
  
  <p>We are using the packages <code>Microsoft.Azure.WebJobs</code> (+ <code>.Core</code>, <code>.Extensions</code>) v2.0.0 and <code>WindowsAzure.Storage</code> v8.0.0, which are somewhat dated, so one potential solution I've been exploring is an update to the latest stable version of WebJobs (v3.0.4). This has opened a whole new can of worms, since the configuration has been completely redone, and all the classes have been moved around. The documentation seems sparse/scattered, so I have yet to determine where (or even if) I can customize properties on a per-QueueProcessor basis, like setting BatchSize to 1 for some queues while higher for others.</p>
  
  <p>Is there some version of WebJobs where I can use the aforementioned CustomQueueProcessorFactory logic to limit BatchSize? Or where the Singleton attribute will actually ensure that only one background process is accessing a specific queue at a time? Can QueueProcessorFactories be configured in the most recent version of WebJobs?</p>
  
  <p>Help on any of these questions would be appreciated immensely!</p>