title: Limit number of instances of an azure webjob when scaling out
body: <p>I would like to limit the number of instances of a particular webjob when our app service plan scales out. </p>
  
  <p>Whilst I am aware of the possibility of having a <a href="https://blogs.msdn.microsoft.com/waws/2014/05/19/how-to-run-a-continuous-webjob-as-a-singleton/" rel="nofollow noreferrer"><em>singleton</em> instance</a> I cannot find any docs to help if we want to limit the number of instances to <em>n</em> where <em>n</em> is a number greater than 1 but less than the <strong>total</strong> number of instances in the app service plan. For example our app service plan could scale out to be running 4 instances but only a maximum of 2 of them would be allowed to be running a given webjob.</p>
  
  <p>I am (of course) aware that we could just have 2 app service plans with different scaling rules but  I'd really just like to handle this within the existing plan if possible.</p>
  
  <p>Background:</p>
  
  <p>We have webapps and webjobs running in an app service plan set to auto-scale out as CPU usage increases above a certain threshold. </p>
  
  <p>Our webjobs do 'background' processing tasks that are not <em>particularly</em> time sensitive but can be quite cpu intensive. If a lot of messages are in the queue these webjobs can consume almost all the available cpu resource and cause a massive scale-out. If we use a singleton webjob then the scale out only adds a single extra instance even at peak times and everything else still stays responsive. This would be fine execept the webjobs are taking too long to clear the queues. We'd really like to be able to balance this better to allow a couple of instances running the webjobs to allow slightly faster processing time but to avoid it adding the cpu-intensive webjob to the 3rd instance. </p>