开发者

What alternative do I have to run a scheduled task

I have hacked together a fix for running a scheduled job on my web server that has worked well enough up until now.

I had the need to run a job every x minutes on my IIS 7 box. To accomplish this, I created a webpage (runcronjob.aspx) that contains all of the logic that I want to run, checks job times in the database, last run time, send emails, query active directory, etc. I then used Task Scheduler on my workstation (not the web server) to call the page I made. I no longer have a dedicated workstation that will always be on the network, so I'd like to move the scheduled task to the web server and this is where I'm getting stuck.

The web server is a Windows 2008 x64 machine running IIS 7. I tried to create the scheduled task on the web server the same way as I did on my local machine but it does not work. The scheduled task calls IEXPLORE.EXE with the webpage as the 开发者_如何学Goparameter "http://mydomain.com/cron/runcronjob.aspx". When IE 7 launches on the web server, it prompts for a username and password. I also tried running a vbs script locally on the server:

runjob.vbs

Call LogEntry()

Sub LogEntry()

        'Force the script to finish on an error.
        On Error Resume Next

        'Declare variables
        Dim objRequest
        Dim URL

        Set objRequest = CreateObject("Microsoft.XMLHTTP")

        'Put together the URL link appending the Variables.
        URL = "http://mydomain.com/cron/runcronjob.aspx"

        'Open the HTTP request and pass the URL to the objRequest object
        objRequest.open "GET", URL , false

        'Send the HTML Request
        objRequest.Send

        'Set the object to nothing
        Set objRequest = Nothing

End Sub

The problem that I've run into with this scenario is that I have forms authentication setup on website. So when this script is ran, it redirects to the log in page instead of running the code on runcronjob.aspx. To get around this, I added this code to the code behind of the Page_Load secton of the login page:

If Request.ServerVariables("LOCAL_ADDR") = Request.ServerVariables("REMOTE_ADDR") Then
    FormsAuthentication.RedirectFromLoginPage("AD-ENT\accountToRunAs", False)
End If

The login page executes the code, but it never redirects back to the runcronjob.aspx for that code to run.

I'm starting to run out of ideas.

EDIT: The reason why I'm doing it in such a 'hacky' method is because I have very limited access to the web server and it was like pulling teeth even getting permission to let me run a scheduled task on the server. That being said, options like creating a console app or a Windows service to run could prove to be difficult.


Your scheduled task should not need to access the web application at all if all you're doing is database maintenance.

Create a console application that performs the necessary database work, and use a scheduled task to run the console application at specified intervals.

EDIT

"options like creating a console app or a Windows service to run could prove to be difficult"

If a console app is not a possibility, use a scheduled task to run a script which updates the database. The main point here is that whatever solution you choose, you should be accessing the database directly rather than using the web application to interface with the database.

EDIT

"It does more than just work with the database. It also sends out emails, does some things with Active Directory - not just database actions."

You can also use the script(s) to perform other actions, such as sending out emails, updating active directory, etc.


It's unfortunate that it requires such a hack to use IIS as opposed to a Windows Service for scheduling...but the best answer I've found in the past is still this one:

http://www.codeproject.com/KB/aspnet/ASPNETService.aspx

As for handling authentication - you could expose the AuthenticationService in your IIS website and then use wininet to SetCookie using the response from the authentication service...that would make your GET request succeed without redirection to the logon page.., References:

http://msdn.microsoft.com/en-us/library/aa385326(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/system.web.applicationservices.authenticationservice.aspx


You should try to move all your logic to a console application and put it in an infinite while loop.

Once this becomes stable you should rewrite it as a windows service and set it start up with windows (i.e. without login).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜