class TaskScheduler::WindowsScheduler

Attributes

import_at[R]
rake_task[R]
repeat_time[R]
repeat_type[R]
task_name[R]

Public Class Methods

new(task_name, rake_task) click to toggle source
# File lib/task_scheduler/windows_scheduler.rb, line 5
def initialize(task_name, rake_task)
  @task_name = task_name
  @rake_task = rake_task
end

Public Instance Methods

create_task(import_at, import_time, repeat_type, repeat_time, username, password, bat_params = nil) click to toggle source

creates a windows scheduled task via command line

# File lib/task_scheduler/windows_scheduler.rb, line 20
def create_task(import_at, import_time, repeat_type, repeat_time, username, password, bat_params = nil)
  io = StringIO.new
  io << 'SchTasks /Create '
  io << "/RU #{username} /RP #{password} " 
  if repeat_type && repeat_time
    io << "/SC #{get_schedule_type(repeat_type)} " # schedule frequency
    io << "/MO #{repeat_time} " # time between runs
  end
  io << "/TN '#{task_name}' " # name of the task
  io << "/TR '#{build_bat_params(bat_params)}' " # what to execute when task runs
  io << "/ST '#{import_time.strftime('%H:%M')}' " if import_time # start time
  io << "/SD '#{import_at.strftime('%m/%d/%Y')}'" if import_at # start date
  io
end
delete_task() click to toggle source
# File lib/task_scheduler/windows_scheduler.rb, line 42
def delete_task
  io = StringIO.new
  io << "SchTasks /Delete /TN '#{task_name}' /F"
end
run_task(system_ip = nil) click to toggle source
# File lib/task_scheduler/windows_scheduler.rb, line 10
def run_task(system_ip = nil)
  io = StringIO.new
  io << 'SchTasks /Run '
  io << "/S #{system_ip} " if system_ip # run a task on a different system
  io << "/TN '#{task_name}' "
  io
end
update_task(import_at, import_time, repeat_type, repeat_time, username, password, bat_params = nil) click to toggle source

updates a windows scheuled task via the command line

# File lib/task_scheduler/windows_scheduler.rb, line 37
def update_task(import_at, import_time, repeat_type, repeat_time, username, password, bat_params = nil)
  delete_task&.run
  create_task(import_at, import_time, repeat_type, repeat_time, username, password, bat_params)
end

Private Instance Methods

build_bat_params(bat_params) click to toggle source
# File lib/task_scheduler/windows_scheduler.rb, line 49
def build_bat_params(bat_params)
  io = StringIO.new
  io << File.join(File.expand_path('..', File.dirname(__FILE__)), 'task_runner.bat')
  io << " #{Rails.root} " # first param so the bat file knows where to cd into
  io << "TASKNAME=#{rake_task} "
  
  return io.string unless bat_params
  
  bat_params.each do |param_name, value|
    io << "#{param_name.upcase}=#{value} "
  end
  io.string
end
get_schedule_type(type) click to toggle source
# File lib/task_scheduler/windows_scheduler.rb, line 63
def get_schedule_type(type)
  if type == 'minutes'
    'MINUTE'
  elsif type == 'hours'
    'HOURLY'
  elsif type == 'days'
    'DAILY'
  end
end