Using ColdFusion Scheduled Tasks

Why Use Scheduled Tasks?
They why in scheduled tasks always comes from an application’s requirements. There will be something that is required to happen on a regular basis, whether generating and emailing a weekly report or connecting to a remote server nightly to download a data set. These kinds of tasks would be difficult without having a built in task scheduler, you’d probably have to write a batch script and schedule it with Windows or use a cron job on Linux otherwise.

Scheduling Through the ColdFusion Administrator

The ColdFusion Administrator provides a decent interface to create your scheduled tasks from, you can see an overview of the screen above. Below is an overview of what each option does:

  • Task Name: something to easily identify the scheduled task.
  • Duration: this allows you to set a start and end date for a task. All tasks require a start date, leaving the end date blank will allow your task to run forever (depending on the Frequency chosen).
  • Frequency: allows you to choose how often in your duration this task should run.
    • One-Time: pretty obvious, ColdFusion will only run your task once on the day and time provided.
    • Recurring: allows you to select daily, weekly, or monthly recurrence at a specific time.
    • Daily Every: if you need a task to run every hour or every five minutes, this is your ticket. NOTE: the Start Time is when during the day your want you task to begin and the end time is when you want it to stop. If you do not provide a stop time, the task will run from your start time until midnight. If you want something to run all day, set the start time to the first occourence after 12 am.
  • URL: the URL of the page you want run.
  • User Name: if your URL is protected with http authentication provide the user name here.
  • Password: if your URL is protected with http authentication provide the password for the user entered above.
  • Timeout (sec): this is a pretty powerful field, if you have some database updates that run each night, but you know they will take longer than your default page timeout, you can extend the timeout for this task by entering the override in seconds.
  • Proxy Server: used if you want this task to be run from another IP address that is running proxy software.
  • Publish: allows you to save any output from the task to a file.
  • File: if your checked Publish you’ll need to provide a file name to write to.
  • Resolve URL: this is related to the Publish option, if any links are created in the output document, you can have ColdFusion create absolute links so the file can be viewed offline and still link correctly.

That’s pretty much it for the Administrator screen, however here are some notes from LiveDocs on weekly and monthly scheduling (pretty powerful/smart stuff):

If you schedule a job to run monthly on any date in the range 28-31, the scheduler does the following:

    • If
      you schedule a monthly job to run on the last day of a month, the
      scheduled job will run on the last day of each month. For example, if
      you schedule a monthly job to start on January 31, it will run on
      January 31, February 28 or 29, March 31, April 30, and so on.
    • If you schedule a monthly job to run on the 29th or 30th
      of the month, the job will run on the specified day of each month for
      30 or 31-day months, and the last day of February. For example, if you
      schedule a monthly job to start on January 30, the job will run on
      January 30, February 28 or 29, March 30, April 30, and so on.

Using the cfschedule Tag
Some great news for everyone on a shared server, or who just doesn’t have access to the Administrator for some bureaucratic reasons you can use the cfschedule tag to do everything the Administrator can. Rather than recreate what LiveDocs already has, go give them a read and come back.
Ok, so how exactly do you use this tag. You would create a page that does one of the five actions available (delete, update, run, pause, resume). If you are creating a new task, you need to use the update action and run your page once. This will setup the initial task and you should be good from there on. Remember you are scheduling a URL, not specific code to run. I have seen some newer developers try to embed code between their cfschedule tags, this will never work.
There will be a follow-up post sometime this weekend with some examples of tasks to schedule and their corresponding cfschedule tags and what the ColdFusion Administrator looks like later.

2 thoughts on “Using ColdFusion Scheduled Tasks”

Leave a Reply

Your email address will not be published. Required fields are marked *