krishna
krishna

Reputation: 43

Schedule a job in hosted web server

Can some one give me a best way to implement a daily job with .NET technology. I have an asp.net application with the sqlserver database hosted in shared hosting, GODaddy in my instance. My application is used to add / change the data in the database which is performing quite fairly at this time. I got a new requirement to send some email alerts daily based on some data criteria that were stored in the database.

Initially I thought to write a windows service, but godaddy is not allowing to access the database other than its hosted applications. Does someone has any idea to send alerts daily at 1:00AM?

Thanks in advance

Upvotes: 4

Views: 7085

Answers (6)

Pavel Chuchuva
Pavel Chuchuva

Reputation: 22455

See Easy Background Tasks in ASP.NET by Jeff Atwood.

Copy/paste from the link:

private static CacheItemRemovedCallback OnCacheRemove = null;

protected void Application_Start(object sender, EventArgs e)
{
    AddTask("DoStuff", 60);
}

private void AddTask(string name, int seconds)
{
    OnCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved);
    HttpRuntime.Cache.Insert(name, seconds, null,
        DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration,
        CacheItemPriority.NotRemovable, OnCacheRemove);
}

public void CacheItemRemoved(string k, object v, CacheItemRemovedReason r)
{
    // do stuff here if it matches our taskname, like WebRequest
    // re-add our task so it recurs
    AddTask(k, Convert.ToInt32(v));
}

Upvotes: 5

andymeadows
andymeadows

Reputation: 1336

If you can expose a service on the website hosting the application and database -- authenticated service, of course -- then you can hit that service remotely from any box with credentials, pull down the data, and send the mail that way.

This could be an automated process written as a Windows service, an application that is run under the Scheduler, or some button you push at 1:00 AM. Your pick.

Just because the app is the only thing that can access the database doesn't mean you can't expose the data in other ways.

Upvotes: 0

SLaks
SLaks

Reputation: 887415

Many hosting providers can request a URL for you every X minutes. I don't know if GoDaddy does, but if so, you could create an ASMX page that kicks off the job, and tell them to execute it automatically.

If they don't, one solution might be to fire off the job in a background thread at every page request. If you do that, make sure you put in code that limits it to running every X minutes or more (perhaps using a static variable or a database table) - read this story

Upvotes: 0

Aaron Saunders
Aaron Saunders

Reputation: 33345

Use either System.Timers, System.Threading to create a instance that is run at a predetermined time. Have that thread execute whatever the task is that you want... Make sure the code is thread safe!

Upvotes: -1

Srikar Doddi
Srikar Doddi

Reputation: 15599

You can use windows scheduler from the web server to schedule a stored procedure call that can send mail based on particular criteria.

osql.exe -S servername -d database -U username -P password -Q "EXEC spAlertOnCriteria"

References:

Upvotes: 0

goheen
goheen

Reputation: 51

I haven't used GoDaddy for anything other than domain registration, so I have no experience with what you can or cannot do on their hosting platform. I also don't know what their support or knowledge base is like, but I'd say your best option is to ask GoDaddy what they recommend. Otherwise, you might keep implementing something that's technically feasible, but is blocked by the hosting company.

If it's not something that's a prime-time application, one quick and dirty thing to do is to have some kind of external bot calling a (secure) web page on the server that fires off the notification process. Not a real solution, but if this site is just a hobby of yours, it could get you by until you find something the host will allow.

Might also be a good time to find a new host, if this one is not meeting your requirements. There are lots of good ASP.NET hosts available these days.

Upvotes: 0

Related Questions