How to make a standalone script using Global.asa

6

I want to run a script that writes information to the database every 24 hours. I am not the administrator of the server, so I do not have access to Scheduled Tasks , I read in some articles that I can use Global.asa for every time the site is visited (using Session_OnStart), count 24 hours and run the script. / p>

I tried to apply this form but it did not work; Here is the code in classic asp:

<!-- #include file="etc/conexao.asp" -->

<script language="vbscript" runat="server">

Sub Session_OnStart
   'Runs on application start or after 15 minutes
   If Application("LastRun") = "" Or DateDiff(n, Application("LastRun"), Now()) > 15 Then

    set RS = server.CreateObject ("ADODB.Recordset")
    set RS2 = server.CreateObject ("ADODB.Recordset")

'exemplo de query que irá roda:
    RS2.Open "SELECT numero from PI_TESTES WHERE ID = 5",conn,1
    numero = RS2("numero")

    numero = numero + 2

    RS.Open "UPDATE PI_TESTES SET numero = '"&numero&"' "+_ 
    "WHERE ID = '5' ",conn,1

       Application("LastRun") = Now()
   End If
End Sub


</SCRIPT>

If anyone has any other solution, I am also willing to adopt.

    
asked by anonymous 28.05.2015 / 14:43

3 answers

3

The problem with using Global.asa is that it does not guarantee that your script will run every time, if it is not constantly in use, IIS can stop the process after a while so it is not guaranteed that you will run the script.

If you are using Windows , you can use the task scheduler or cmd the schtasks command.

Example:

schtasks /Create /SC DAILY /ST 10:00:00 /ET 10:10 /K /TN rotina /TR "'C:\Program Files\Internet Explorer\iexplore.exe' \"http://ip_intranet/aplicacao/rotina.asp""
  • /Create - Create the task.
  • /SC - Frequency that will execute the task, in case DAILY = Daily
  • /ST - Time to execute the task, in case 10h.
  • /ET - Time to finish the task, in case 10h10.
  • /K - Ends the task according to ET .
  • /TN - Task name, in case 'routine'.
  • /TR - Command to be executed, in case it opens the browser of IExplorer and opens url "http://ip_intranet/aplicacao/rotina.asp"

What's the catch here?

You create this page rotina.asp by doing all the SELECT/UPDATE you need. Then every time it executes the task it will open the page, making a request since it is in the same network intranet and open rotina.asp .

    
11.06.2015 / 14:59
3

If it were ASP.Net, it would be easier to use quartz.net but in classic asp it's more complicated. I suggest to you three "gambiarras" that you can analyze and choose the one that best suits you.

First:

  • Schedule your personal machine to run the asp or vbs page that does the service daily.
  • The problem here is that you have to be allowed to schedule the task and if your machine is shut down for some reason or if it is not on the network it will not start.

    Second:

  • Create a free account on newrelic
  • Add a monitor in synthetics ( synthetics.newrelic.com ) that runs every 24 hours by calling your page routine.asp
  • This solution only works on sites that are public. That is, they can be accessed over the internet, not working on intranets, and if your server is out at the scheduled time it may not run.

    Third:

  • Ask the support team to schedule the server recycle to be done every day at a certain time
  • Place your code in global.asa's application_start
  • This solution will always run on the first access after the recycle, so the time may vary. Another problem is that it can run more than once a day and you will have to control this via code. But despite varying the schedule and running more than once a day, this solution is the one that most guarantees that your code will run every day. If it is a public site you can create a monitor on the newrelic, running every 1 minute by accessing any page of the site, ensuring that as soon as the recycle is done your code will run. And for breaking down, you'll get an uptime / downtime report and performance of your system.

    Sorry to give strange solutions but classic asp is complicated. I hope I have helped.

        
    15.06.2015 / 04:05
    2

    I think a better solution would be to use a scheduler in the Application_Start event of your application. This scheduler would be triggered only once and executed every 24 hours.

    Quartz is a good example of a good tool for this.

    You can also implement a scheduler, but it would be "reinventing the wheel", yet a very interesting exercise.

        
    11.06.2015 / 13:56