• Iguana 6
  • Previous Versions
  • API
  • Sample Code
  • Training
  • Create a Ticket
iNTERFACEWARE Help Center
  • Iguana 6
  • Previous Versions
  • API
  • Sample Code
  • Training
  • Create a Ticket

Code Repository

Home›Code Repository›scheduler.lua
Modules

scheduler.lua

Verified Featured
Added by iNTERFACEWARE

Schedule jobs to run at a specified time of day, very useful for batch processing

Source Code
local scheduler={}
 
local LastRunTime = 0
local ScheduledRunTime = 0

local function NextRunTime(Hour, LastRunTime)
   local T = os.ts.date('*t')
   T.hour = Hour
   T.min = (Hour - math.floor(Hour)) * 60
   T.sec = 0
   local NextTime = os.ts.time(T)
   local LastT = os.ts.date('*t', LastRunTime)
   os.ts.date("%c", LastRunTime)
   if os.ts.difftime(LastRunTime, NextTime) > 0 then
      NextTime = NextTime + 24*60*60
   end
   return NextTime, os.ts.date("%c", NextTime)
end

local function RunFileName()
   return iguana.channelName():gsub('%s','_')..'_LastScheduledTime.txt'
end

local function LastRun()
   if not os.fs.access(RunFileName()) then
      return 0, 'No recorded run'
   end
   local F = io.open(RunFileName(), 'r')
   local T = F:read('*a')
   F:close()
   return tonumber(T), 'Last run at '..os.ts.date('%c', tonumber(T))
end

local function Status(LastRun, ScheduledTime)
   local R
   if LastRun ~= 0 then
      R = 'Last run at '..os.ts.date('%c', LastRun)
   else
      R = 'Has not run yet.'
   end
   R = R..'\nScheduled to run at '..os.ts.date('%c',ScheduledTime)

   iguana.setChannelStatus{color='green', text=R}

   return R
end

local function RecordRun(ScheduledHour)
   if iguana.isTest() then return end
   local R = os.ts.time()
   local F = io.open(RunFileName(), 'w')
   F:write(R)
   F:close()
   LastRunTime = R
   ScheduledRunTime = NextRunTime(ScheduledHour, LastRunTime)
   local R  = Status(LastRunTime, ScheduledRunTime)
   iguana.logInfo(R)
end

local function Init(Time)
   LastRunTime = LastRun()
   ScheduledRunTime = NextRunTime(Time, LastRunTime)
   local R = Status(LastRunTime, ScheduledRunTime)
   iguana.logInfo(R)
   return R
end

function scheduler.runAt(ScheduledHour, Func, Arg)
   local R
   trace(LastRunTime)
   if LastRunTime == 0 or iguana.isTest() then
      -- We need to do one time initialization
      R = Init(ScheduledHour)
   end
   trace(ScheduledRunTime)
   local WouldRun = (os.ts.time() > ScheduledRunTime and LastRunTime <= ScheduledRunTime)
   trace("Would run = "..tostring(WouldRun))

   if WouldRun then
      iguana.logInfo('Kicking off batch process')
      Func(Arg)
      RecordRun(ScheduledHour)
      return R
   end
   if iguana.isTest() then
      Func(Arg)
      return R
   end

   return R
end

local scheduler_runAt = {
   Title="scheduler.runAt";
   Usage="scheduler.runAt(time, function [, argument])",
   SummaryLine="Schedule a function to run at a specified time",
   Desc=[[Schedule a function to run at a specified time, optionally
   you can pass a single argument for the scheduled function.
   ]];
   ParameterTable= true,
   Parameters= {
      {time= {Desc='Scheduling time <u>number</u>.'}},
      {['function']= {Desc='The function to be run <u>function</u>.'}},
      {argument= {Desc='Argument to pass to the function <u>any type</u>.', Opt = true}},
   };
   Returns = {
      {Desc="Information on previous and next scheduled runtime  <u>string</u>."},
   };
   Examples={
      [[   -- Within the editor we the function runs all the time.
   local function DoBatchProcess(Data)
      iguana.logInfo('Processed a lot of X12')
   end

   function main()
      -- schedule the job function to run at 11.30pm 
      scheduler.runAt(23.5, DoBatchProcess, "Some Argument")   
   end
      ]],
   };
   SeeAlso={
      {
         Title="scheduler.lua - in our code repository",
         Link="http://help.interfaceware.com/code/details/scheduler-lua"
      },
      {
         Title="Scheduler example",
         Link="http://help.interfaceware.com/v6/scheduler-example"
      }
   }
}

help.set{input_function=scheduler.runAt, help_data=scheduler_runAt}

return scheduler
Description
Schedule jobs to run at a specified time of day, very useful for batch processing
Attachments
scheduler_From_Translator.zip
Usage Details

A common interface requirement involves running a batch job at a specific time of day (for example, every night at 10pm). Although Iguana does not have this capability built directly into the product, it is simple to create a simple channel that will perform this task. The scheduler.runAt() function simply checks the time and runs the function. It also stores information (in a text file) about when each process was last run, and checks to ensure that each task is only run once at the scheduled time.

The module also does a few other useful things like : Logging useful error messages (in the Iguana log), changing the tooltips for scheduled jobs to show when they were last run, and if is running in the editor (test mode), it always run the job as scheduled (as opposed to live which checks when the job was last run to prevent running it more than once).

How to use scheduler.lua:

  • Create a “Scheduler” channel and set the polling time to sensible period like 5 minutes (300,000 milliseconds)
  • Load the module into a From Translator component
  • Use  scheduler.runAt() to run your function at the desired time

Here is some example code for main():

scheduler = require 'scheduler'

-- This scheduler script can be used to run a script at a given time
-- https://help.interfaceware.com/code/details/scheduler-lua

 
-- Within the editor we the function runs all the time.
local function DoBatchProcess(Data)
   iguana.logInfo('Processed a lot of X12')
end
 
function main()
   -- schedule the job function to run at 11.30pm 
   scheduler.runAt(23.5, DoBatchProcess, "Some Argument") 
end
More Information
Scheduling a channel to run at a specific time
Bookmark
  • Reviews
  • Related Listings
Filter
Sort by: Newest First
  • Oldest First
  • Rating
  • Helpfulness
Write a Review
Rating
Keyword
Filter
Sort by: Title
  • Newest First
  • Oldest First
  • Most Reviews
  • Highest Rated
Rating
iNTERFACEWARE
age.lua
Added by iNTERFACEWARE
Modules
This module calculates age from DOB, it returns years, months and partial years (i.e., 17, 3, 17.296272)
auth.lua
Added by iNTERFACEWARE
Modules
A module that does basic authentication for incoming web requests
batch.lua
Added by iNTERFACEWARE
Modules
A module to help processing batched HL7 messages
codemap.lua
Added by iNTERFACEWARE
Modules
This module is used to map one set of codes to another set of codes, or to validate code membership in a set
csv_parse.lua
Added by iNTERFACEWARE
Modules
A module for parsing well-formed CSV files.
custom_merge.lua
Added by iNTERFACEWARE
Modules
A customizable database merge method for Iguana 5.5.1 and up.
dateparse.lua
Added by iNTERFACEWARE
Modules
A fuzzy date/time parser that is very useful for automatically translating a wide variety of date/time formats.
dup.lua
Added by iNTERFACEWARE
Modules
Duplicate message filter.
edifact.lua
Added by iNTERFACEWARE
Modules
Convert EDI messages to HL7 format so you can process them like an HL7 message (and convert them back to EDI afterwards)
hl7.findSegment.lua
Added by iNTERFACEWARE
Modules
A utility for finding any HL7 segment in a parsed HL7 message node tree.
hl7.serialize.lua
Added by iNTERFACEWARE
Modules
Serializes an HL7 message using specified non-standard delimiters and/or escape characters
hl7.zsegment.lua
Added by iNTERFACEWARE
Modules
Generic Z segment parser. Parses Z segments without needing grammar definitions in the VMD file.
iguanaServer.lua
Added by iNTERFACEWARE
Modules
Provides programmatic access to various operations that can be performed on Iguana channels.
llp.lua
Added by iNTERFACEWARE
Modules
Allows you to use LLP connections from a Translator script
mime.lua
Added by iNTERFACEWARE
Modules
Sends MIME-encoded email attachments using the SMTP protocol. A wrapper around net.smtp.send.
resubmit.lua
Added by iNTERFACEWARE
Modules
Resubmit a logged message to an Iguana channel using the unique reference number (refmsgid).
retry.lua
Added by iNTERFACEWARE
Modules
A module for retrying operations which might periodically fail like database operations.
rtf.lua
Added by iNTERFACEWARE
Modules
A module for converting a RTF file to plain text.
scrub.lua
Added by iNTERFACEWARE
Modules
The “scrub” module given below redacts sensitive information from HL7 messages.
sha1.lua
Added by iNTERFACEWARE
Modules
A pure Lua-based implementation of the popular SHA-1 hashing function.
Showing 1 - 20 of 31 results
«12»

Topics

  • expandGetting Started
  • expandAdministration
    • expandInstallation
    • expandLicensing
    • expandUpgrades
    • expandDeployment
    • expandConfiguration Management
      • expandCustom Configuration
    • expandBackup and Restore
    • expandSecurity
      • expandHIPAA Compliance
    • expandTroubleshooting
  • expandDeveloping Interfaces
    • expandArchitecture
    • expandInterfaces
      • expandHL7
      • expandDatabase
        • expandConnect
      • expandWeb Services
      • expandCDA
      • expandX12
      • expandOther Interfaces
      • expandUtilities
    • expandRepositories
      • expandBuiltin Repositories
        • expandIguana Upgrade
        • expandIguana Tutorials
        • expandIguana Tools
        • expandIguana Protocols
        • expandIguana Files
        • expandIguana Date/Time
        • expandIguana Webservices
        • expandIguana Excel
      • expandRemote Repositories
      • expandCS Team Repositories
        • expandIguana Channels
    • expandSample Code
      • expandModules
      • expandUsing built-in functions
      • expandWorking with XML
    • expandLua Programming
    • expandPerformance
  • expandFAQs and TIPs
    • expandFrequently Asked Questions
      • expandInstalls and Upgrades
      • expandWeb Services
      • expandConfiguration
      • expandChannels
      • expandTranslator
      • expandOther
      • expandDatabase
      • expandAdministration
      • expandLogs
      • expandChameleon
    • expandTips
      • expandChannels
      • expandChameleon
      • expandWeb Services
      • expandSecurity
      • expandProgramming
      • expandOther
      • expandAdministration
  • expandReference
    • expandIguana Enterprise and Professional
    • expandProgram Settings
    • expandChannel Settings
    • expandDashboard
    • expandChannels
    • expandTranslator
    • expandLogs
      • expandLog Encryption
    • expandHTTP API
    • expandCDA API
    • expandError Messages
    • expandChameleon
    • expandIguana Change Log

Other Links

  • Training Center
  • News & Announcements
  • iNTERFACEWARE Blog
  • Older Documention (IGUANA v4 & Chameleon)
Copyright © iNTERFACEWARE Inc.