Create an image on the server every 5 minutes
Hi,
I need to create an image from some data in my database on the server every 5 minutes. The image path is Images/top5.jpg, so that users can place a link to that imagine on some forum or whatever and it will update every 5 minutes.
I am doing this by using a System.Threading.Timer in Global.asax:
vb.net Code:
Public Class Global_asax
Inherits System.Web.HttpApplication
Dim path As String
Dim timer As System.Threading.Timer
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
path = Server.MapPath("~/Images/top5.jpg")
timer = New System.Threading.Timer(AddressOf TimerTick)
timer.Change(1000, 300000)
End Sub
Sub TimerTick(ByVal status As Object)
Try
Top5Image.UpdateTop5(path)
Catch ex As Exception
Debug.WriteLine(ex)
End Try
End Sub
End Class
The Top5Image.UpdateTop5 method will create and save the image.
Locally, this seems to work OK. The image is created every 5 minutes in the Images folder.
I uploaded the website, but it does not seem to work on the server. What could be causing this?
I thought maybe I don't have write permission on the server or something. What I tried:
- Placed a random image on the server just so one is there already (maybe I could change an image but not create a new one?)
- Gave Write permissions to the Image folder and the image in there.
Doesn't seem to work, it's now just displaying the random image but it should have generated the new image already.
As a side question: how can I debug stuff like this? I am using a Try/Catch block but I'm finding it well... impossible to see any exception. Debug.WriteLine works fine in the IDE but now that it's on the server I've no idea how I could view the error, if any.
Thanks!
Re: Create an image on the server every 5 minutes
This doesn't seem like a task you would want to put in a web site. Seems like a background process like a service or a scheduled task would be more appropriate. But I guess it depends how much access you have to the server too.
Re: Create an image on the server every 5 minutes
I agree, but I have no idea how to create a service or scheduled task on the server. I doubt I even can, it's not like I have the server in my basement, I rented it.
By the way, it seems to work now suddenly... It took maybe 10 minutes but then suddenly the image popped up. That's strange because
1) The delay of the timer is only 5 minutes
2) The timer should fire 1 second after it's created, so the image should have popped up almost immediately after restarting the server.
So it seems to work, but with a delay.. :/ Again this is hard to test for me, because the image will probably not change (as the data will probably not change anymore) and even if it did I would have to notice that within 5 minutes of the data changing to be able to test it...
Re: Create an image on the server every 5 minutes
Quote:
Originally Posted by
NickThissen
I agree, but I have no idea how to create a service or scheduled task on the server. I doubt I even can, it's not like I have the server in my basement, I rented it.
By the way, it seems to work now suddenly... It took maybe 10 minutes but then suddenly the image popped up. That's strange because
1) The delay of the timer is only 5 minutes
2) The timer should fire 1 second after it's created, so the image should have popped up almost immediately after restarting the server.
So it seems to work, but with a delay.. :/ Again this is hard to test for me, because the image will probably not change (as the data will probably not change anymore) and even if it did I would have to notice that within 5 minutes of the data changing to be able to test it...
Running it on a web page in general I think will lead to unusual results as to when the timer fires.
Re: Create an image on the server every 5 minutes
I'm running the timer in the Global.asax file. I've read that that was the most appropriate place, and the System.Threading.Timer was the most appropriate timer to use (contrary to the two other times that I know of). Is it really that much of a problem?
Re: Create an image on the server every 5 minutes
Quote:
Originally Posted by
NickThissen
As a side question: how can I debug stuff like this? I am using a Try/Catch block but I'm finding it well... impossible to see any exception. Debug.WriteLine works fine in the IDE but now that it's on the server I've no idea how I could view the error, if any.
You could use Trace, and then enable Trace on your web page/application.
The other alternative would be to use logging, you can easily add logging to your web application using something like log4net.
Gary
Re: Create an image on the server every 5 minutes
Quote:
Originally Posted by
NickThissen
I'm running the timer in the Global.asax file. I've read that that was the most appropriate place, and the System.Threading.Timer was the most appropriate timer to use (contrary to the two other times that I know of). Is it really that much of a problem?
I have always tended to shy away from timers in my web applications, as this sort of breaks the web paradigm, about being stateless. In your situation, global.asax would be the "best" place to do this work, although, you are at risk of the application pool being recycled, and things getting "messed" up.
An alternative solution would be to put the necessary work in a web service method, and then from another machine, or the server you are running it on, call the web service method and the required interval. Most hosting companies allow you to set this up, although, you might need to pay a little extra.
Gary
Re: Create an image on the server every 5 minutes
Thanks. I am still using the timer, and it hasn't stopped working yet. It's not a critical part of my website, just an image that shows the top-5 drivers in a formula one game competition. I think I'll just keep it this way, but if it ever does stop working I'll get back to this :) Thanks.
And in the mean-time I've built my own 'logging system' (basically just a dump of the exception to a log file on the server), but I haven't encountered an exception yet so the file is still empty :p
Re: Create an image on the server every 5 minutes
No exceptions, woo hoo!!
In case you need it in the future, take a look here:
http://www.vbforums.com/showthread.php?t=595883
10 simple steps to getting log4net working across your entire application.
Gary
Re: Create an image on the server every 5 minutes
That looks pretty easy, thanks!
Re: Create an image on the server every 5 minutes
It really is. Once it is set up, you never have to worry about it again, just do:
Code:
log.Debug("This is a debug statement");
log.Error("Error", ex); //passing in the exception, and log4net does everything else.
There are other frameworks out there, but log4net is the one that I have settled on for now, although elmah is starting to look very interesting:
http://code.google.com/p/elmah/
Gary