So far in this book, you have seen the power of ColdFusion in producing sophisticated Web-based applications that can draw on multiple sources of data, including relational databases, e-mail, Web and FrP servers, and local files on the ColdFusion server. However, in all cases, these Web applications and ColdFusion templates have required that a user with a Web browser explicitly request a template file from the server for it to be executed.
In this chapter, you will see that Cold Fusion provides a scheduling mechanism that enables you to create batch-like Cold Fusion templates that run at prescheduled times and do not require a user to explicitly request a template with their browser. You can use these scheduled events to create static Web pages out of dynamic data sources, reducing the time that it takes to serve pages to end users. You can also use them to generate e-rnail-based reports based on dynamic data sources and to maintain up-to-date indexes for the Veri.ty search engine (See Chapter 29, “Implementing a Search Engine”). You will also learn to schedule events by using the ColdFusion Administrator and the CFSCHEDULE tag. And you will learn about saving scheduled-event output to files to serve as static Web pages, and about the finer points of scheduled-event management and logging
Schedule cold fuslon Templates
At its most basic, scheduling a Cold Fusion event is simple. You need to provide three pieces of information when scheduling an event:
The dates and times for execution of the event.
The URL of the ColdFusion template to be executed.
A name for the task so that it can be referred to later.
You can specify this information when creating an event by using the ColdFusion.
Administrator or the CFSCHEDULE tag. You will look at each in turn.
Scheduling an Event with ‘the ColdFusion Administrator
To schedule an event with the Cold Fusion Administrator, open the Coldfusion Administrator and select the Scheduled Tasks page. This page should look like the one.
The Scheduled Tasks page provides a simple form that can be used to start the process of scheduling an event followed by a list of scheduled events; if there are any. To create a new scheduled event, start by assigning a name to the event in the Task Name field, and clicking the AddNew Task button. This should bring up the Add Scheduler Task page, like the one.
The Add Scheduler Task page has three main .sections for specifying the following information:
• When to execute the task, and how often
• The URL to execute and relevant information, such as usemame and password for theURL ‘
• Information about how to save the output to a static file on the server (which we will discuss later in the chapter)
Specifying the Time and Date for a Schedule Event
As the following information needs to be specified when scheduling the time and date for an event:
• The dates during which the event should be executed. This is specified in the Duration section with a start and end date. The start date is required but the end date is optional. Without an end date, the event will execute based on the additional rules specified.
• The frequency with which the event should be e’: ::::uted. The options are:
One-Time By specifying a time, the event will occur once on the start date at the time specified
Recurring By specifying Daily, Weekly, or Monthly, the €’1 nt will first execute at the specified time on the start date and then at the same time on a daily, weekly, or monthly basis until the end date, if specified, is.reached.
Dally Every X Minutes This option enables an event to be scheduled to run at a set interval in minutes between a start time, and end time and to do this every day between the start date and the end date, if specified.
Specifying the URLto Execute
The second section of the new page . Here you specify the URL to execute at-the we specified in the preceding section, and how to execute it.
This section contains five fields that can be filled out as follows:
URL The full URL of-the page to execute at the specified times. At a minimum, this field needs to be filled out in this section.
Username If the specified URL requires a usemame and password to access it, provide the usemame here.
Password If the specified URL requires a usemame and password to access it, provide the password here.
Request Timeout Specifies the number of seconds to wait for a page to execute before assuming the connection has failed. The default is 30 seconds, but if you are requesting a page from a busy server or across a poor network connection, you may want to increase this value to the equivalent of several minutes (such as 200 seconds).
Proxy Server If your ColdFusion server sits behind a corporate firewall, but the URL you are specifying is on a server outside the firewall, then you need to provide the URL for your’ corporate proxy server (including the host name and port number in the format proxy. host. name:port) in this field.
Saving Output to a File
In the final section of the new task page, you can specify that the output generated by a scheduled page should be saved to a file.
Saving the output of a scheduled task to a file is a good idea for several reasons, including:
Improving performance If you have pages that are generated from large query result sets, especially against a slow-changing database, it will probably be more efficient to generate static versions of pages on a regular basis, such as daily, rather than letting queries run every time a user requests one of the pages.
Logging of activity If you output the results of scheduled jobs to static files, you can keep a directory of results of all your scheduled jobs, which can be viewed from a single location by using a Web browser.
Four fields are available when specifying the output file for a scheduled,task. The first field, labeled Publish, is a check box. When checked, it indicates that the output should be saved to a file, based on the settings in the three other fields.
If output is being saved to then the remaining three fields specify how that should be done:
Path Specifies the direetory it., which the output file should be saved. This can be specified as an absolute path, the local system where the ColdFusion server is running (such as c:\data\ 1095«’\ a Windows system) or as a network accessible directory (\ \nostname\sharenaITle’Olll a Windows network).
File Specifies the filename that the output should be saved to. Combined with the path, this fully identifies the name and location where the output will be saved. ResolveURL “Indicates, when selected, that any relative URLs in the output should be converted to absolute URLs so that there will be np broken links in the output. This option is especially ‘useful when you are generating static content from scheduled dynamic pages that sit on a different server or in a difierent directory than the resulting static output files.
Adding the Event
After a task has been fully specified, you can add it by clicking the Create button at the bottom of the page. Next to the Create button is the Cancel button, which causes ColdFusion to discard the information in the form and return to the Scheduled Tasks page.