17

I'm new to Azure WebJobs, I've run a sample where a user uploads an image to blob storage and inserts a record into the Queue, then the job retrieves that from the queue as a signal to do something like resizing the uploaded image. Basically in the code the job uses QueueTrigger attribute on a public static method to do all that.

Now I need a job that just does something like inserting a record into a database table every hour, it does not have any type of trigger, it just runs itself. How do I do this?

I tried to have a static method and in it I do the insert to db, the job did start but I got a message saying:

No functions found. Try making job classes public and methods public static.

What am I missing?

Edit After Victor's answer I tried the following,

static void Main()
{
    JobHost host = new JobHost();
    host.Call(typeof(Program).GetMethod("ManualTrigger"));
}

[NoAutomaticTrigger]
public static void ManualTrigger()
{
    // insert records to db
}

but this time I got InvalidOperationException,

'Void ManualTrigger()' can't be invoked from Azure WebJobs SDK. Is it missing Azure WebJobs SDK attributes?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Ray
  • 12,101
  • 27
  • 95
  • 137

2 Answers2

17

If you don't use any input/output attributes from the WebJobs SDK (QueueTrigger, Blob, Table, etc), you have to decorate the job with the NoAutomaticTrigger Attribute to be recognized by the SDK.

effy
  • 410
  • 8
  • 20
Victor Hurdugaci
  • 28,177
  • 5
  • 87
  • 103
  • Thanks Victor for your answer, I tried NoAutomaticTrigger and got an InvalidOperationException, I've updated my question with the code I have, could you help to see what I'm doing wrong please. Thanks a lot! – Ray Sep 30 '14 at 02:55
  • 3
    Ah you are right, I just declared "public" on the "class Program", it runs however it shows the msg below for a couple seconds and then the console window just crashes away. Msg: "Found the following function: WebJob1.Program.ManualTrigger Executing: 'Program.ManualTrigger' because This was function waas programmatically called via the host APIs." – Ray Sep 30 '14 at 06:00
  • The process will exit unless you call `host.RunAndBlock();`. – Anthony Chu Sep 30 '14 at 06:04
  • Host.Call is similar to invoking a function and then continuing. RunAndBlock will (surprise!) block and keep the program alive – Victor Hurdugaci Sep 30 '14 at 15:43
  • Thanks Victor and Anthony for your input. I ended up using a vanilla console app to do what I needed without using the WebJobs SDK and scheduled it with the scheduler. Seems like a method decorated with NoAutomaticTriggerAttribute can only be called with host.Call() in which case it exists immediately afterward, and RunAndBlock() does not work with NoAutomaticTriggerAttribute. I feel the value the SDK brings is to make working with Azure Storage/Table/Queue very easy, but in this particular case I don't need them. Thanks. – Ray Sep 30 '14 at 21:26
  • 2
    There is one advantage is using the SDK even with no automatic trigger. While the host is running (aka RunAndBlock or host.Start), you see the function in the WebJobs dashboard. That allows you to invoke the function directly from there. Think of host.Call as a fancy MethodInfo.call that gives you dashboard logging – Victor Hurdugaci Oct 03 '14 at 23:41
  • Though the code in the above post works, doesn't it also require a call to host.Start() before the call to host.Call()? – LeonZandman Jul 21 '16 at 14:27
3

You could use the latest WebJobs SDK, which supports triggering job functions on schedule, based on the same CRON expression format. You can use it to schedule your job every hour:

[Disable("DisableMyTimerJob")]
public static void TimerJob([TimerTrigger("00:01:00")] TimerInfo timerInfo, TextWriter log)
{
    log.WriteLine("Scheduled job fired!");
}

Moreover, the WebJobs SDK also has a DisableAttribute that can be applied to functions, that allows you to enable/disable functions based on application settings. If you change the app setting in the Azure Management Portal, the job will be restarted (https://azure.microsoft.com/en-us/blog/extensible-triggers-and-binders-with-azure-webjobs-sdk-1-1-0-alpha1/).

Thomas
  • 24,234
  • 6
  • 81
  • 125