title: Azure Function and Service bus - conditionally add a new message
body: <p>When my app needs to send an email, it creates a db entry and puts it into the database and then puts a message on a Service Bus that is monitored by an Azure Function running as a WebJob.</p>
  <p>When the WebJob process finishes sending the email, I would like it to conditionally put a new scheduled message on the Service Bus queue - really, I want the WebJob to be woken up in 5 minutes to see if there is an email in the db that needs to be sent.</p>
  <p>But if there is already a message on the queue to that the WebJob has to process, then I don't want to put this scheduled message on the queue, I just want to finish processing because I know that the queue is going to wake up the WebJob immediately again, so there is no need to add the 5 minute scheduled message on the queue this time, it will be put on the queue after the next WebJob completion.</p>
  <p>I am also using Scheduled messages from the App because there are times when I need to send a message in the future, so I add a scheduled message to the service bus queue to wake up the sender at a specific time.</p>
  <p>I would like to do a peekBatch on the queue to see if there is already a message that will cause the WebJob to be woken up before the 5 minutes elapse. If there is, I want to skip putting a new message on the queue, but if there isn't, I would like to add a message to the queue.</p>
  <p>To be clear, this peekBatch needs to happen inside an Azure Function inside a WebJob that uses a &quot;ServiceBusTrigger&quot;.</p>
  <p>What I can't figure out is how to get access to something to do the PeekBatch on. I have found <a href="https://stackoverflow.com/questions/59239218/how-to-push-a-message-onto-azure-service-bus-within-a-function">examples</a> of how to send the message from within the ServiceBusTrigger function.</p>