0

So i'm trying to get a small project of mine going that I want to host on azure, it's a web app which works fine and I've recently found webjobs which I now want to use to have a task run which does data gathering and updating, which I have a Console App for.
My problem is that I can't set a schedule, since it is published to the web app which dosen't support scheduling, so I tried using the Azure Webjobs SDK and using a timer but it wont run without a AzureWebJobsStorage connection string which I cannot get since my Azure account is a Dreamspark account and I cannot make a Azure Storage Account with it.
So I was wondering if there is some way to get this webjob to run on a time somehow (every hour or so). Otherwise if I just upgraded my account to "Pay-As-You-Go"? would I still retain my free features? namely SQL Server.

Im not sure if this is the right palce to ask but I tried googling for it without success.

Update: Decided to just make the console app run oin a infinate loop and ill just monitor it through the portal, the code below is what I am using to made that loop.

class Program
{
    static void Main()
    {
        var time = 1000 * 60 * 30;
        Timer myTimer = new Timer(time);
        myTimer.Start();
        myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);

        Console.ReadLine();
    }

    public  static void myTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        Functions.PullAndUpdateDatabase();
    }
}
Toxicable
  • 1,639
  • 2
  • 21
  • 29
  • 1
    Not the most elegant solution, but you could always have your webjob run an infinte loop, sleeping the thread for x minutes at a time. Or you could calculate the timespan until your next scheduled run and then use System.Timers.Timer to trigger an event. – Martin Apr 01 '16 at 23:36
  • @Martin Oh right, that's not a too bad idea, I think I'll give that a shot, I'm thinking just a while loop that never evaluates to false, anything wrong with this except the non elegance of it? – Toxicable Apr 01 '16 at 23:41
  • 1
    Shouldn't really be a problem, allthough there are mixed opinions on this, see http://stackoverflow.com/questions/8815895/why-is-thread-sleep-so-harmful Make sure you add "Thread.Sleep(timeInMilliseconds)" or some other mechanism, not JUST while(true){..} in your loop. – Martin Apr 01 '16 at 23:56
  • @Martin I actually just looked into the System.Timers.Timer, I thought it would be harder but from some other posts I got the code I posted above which I think is a bit better than `Thread.Sleep` – Toxicable Apr 01 '16 at 23:58
  • Indeed, good luck :) – Martin Apr 02 '16 at 00:00

1 Answers1

0

The simplest way to get your Web Job on a schedule is detailed in Amit Apple's blog titled "How to add a schedule to a triggered WebJob".

It's as simple as adding a JSON file called settings.job to your console application and in it describing the schedule you want as a cron expression like so:

{"schedule": "the schedule as a cron expression"}

For example, to run your job every 30 minutes you'd have this in your settings.job file:

{"schedule": "0 0,30 * * * *"}

Amit's blog also goes into details on how to write a cron expression.

Caveat: The scheduling mechanism used in this method is hosted on the instance where your web application is running. If your web application is not configured as Always On and is not in constant use it might be unloaded and the scheduler will then stop running.

To prevent this you will either need to set your web application to Always On or choose an alternative scheduling option - based on the Azure Scheduler service, as described in a blog post titled "Hooking up a scheduler job to a WebJob" written by David Ebbo.

urig
  • 16,016
  • 26
  • 115
  • 184
  • Because it is a webjob published on a web app I cannot set a scheduled as I detailed in my post, I have tried this but when i deploy it just fails because of that reason. If it was not published to a web app and a independent webjob then this would not be the case but I do not have access to that on my account – Toxicable Apr 02 '16 at 21:22
  • Web Jobs are never independent. They are always running in the context of a Web App even if that Web App is "empty". Perhaps you can share the method in which you deployed the Web Job and the exact response received from Azure? – urig Apr 02 '16 at 21:32
  • Sorry, I thought they were independent, I've never use the other type of one so I don't know. As I outlined in my question I am on a DreamSpark plan so a lot of things are locked out for me, including the Always On under app settings in my Web App and the other forms of web jobs. The one I am doing I have published from VS with "Publish as Webjob" with the same publishing settings as my web app. When I use it continuous mode I'll eventually get aborted and it wont restart – Toxicable Apr 02 '16 at 21:39
  • I've no experience with publishing this way. Can you compare publishing with the settings.job file and then without it? Maybe the fault is not with the settings.job file? ALSO: Feel free to edit your question above and add more details. This will help you get better answers. – urig Apr 02 '16 at 21:40
  • When publishing this way it creates a `webjob-publish-settings.json` file which it will not publish without. Also note I also cannot use Azure scheduler and if I don't set it to Continuous I still get aborted eventually – Toxicable Apr 02 '16 at 21:42