Class: TaskManager

Inherits:
Object
  • Object
show all
Defined in:
lib/task_manager.rb,
lib/task_manager/version.rb

Constant Summary collapse

VERSION =
"0.1.0"
DATE =
File.mtime(__FILE__)
SUMMARY =
'A simple wrapper around the Rufus::Scheduler to have a more configurable setup.'
DESCRIPTION =
<<-EOT
  A simple wrapper around the Rufus::Scheduler to have a more configurable setup.

  The Task to be scheduled should be defined in a subclass.
EOT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = 'staging', path = '.') ⇒ TaskManager

create the necessary scheduler/job-stores and store a basepath and a environment

STDOUT is set to sync=true so that the log is not held back by some output-buffering



14
15
16
17
18
19
20
21
# File 'lib/task_manager.rb', line 14

def initialize(env = 'staging', path = '.')
  @path = Pathname.new(path)
  @env = env.to_sym
  @jobs = {}
  @schedulers = {}

  $stdout.sync = true
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/task_manager.rb', line 7

def config
  @config
end

#envObject (readonly)

Returns the value of attribute env.



7
8
9
# File 'lib/task_manager.rb', line 7

def env
  @env
end

#jobsObject

Returns the value of attribute jobs.



6
7
8
# File 'lib/task_manager.rb', line 6

def jobs
  @jobs
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/task_manager.rb', line 7

def path
  @path
end

Instance Method Details

#apply_configurationObject

apply configuration



87
88
89
90
91
# File 'lib/task_manager.rb', line 87

def apply_configuration
  config.each do |method, args|
    send(method, *args)
  end
end

#execute_task(cmd_string) ⇒ Object

system-call



53
54
55
56
57
58
59
60
61
# File 'lib/task_manager.rb', line 53

def execute_task(cmd_string)
  shortened_cmd = cmd_string.gsub(/--trace/, '').strip
  say   "starting #{shortened_cmd}"
  if system(full_command_string(cmd_string))
    say "finished #{shortened_cmd}"
  else
    say "ERROR in #{shortened_cmd}"
  end
end

#full_command_string(cmd_string) ⇒ Object

overwritable command_string which will be executed in the system



64
65
66
# File 'lib/task_manager.rb', line 64

def full_command_string(cmd_string)
  "cd #{path}; RAILS_ENV=#{env} #{cmd_string} >>#{path}/log/#{env}.scheduler.task_output.log 2>>#{path}/log/#{env}.scheduler.log"
end

#persistObject

prevent RubyVM from quitting



40
41
42
43
# File 'lib/task_manager.rb', line 40

def persist
  say "Scheduler will be persisted now, all definitions should be loaded now."
  scheduler(:internal).join
end

#say(msg) ⇒ Object

output a message with Time



69
70
71
# File 'lib/task_manager.rb', line 69

def say(msg)
  $stdout.puts "#{Time.now} - #{msg}"
end

#schedule(key) ⇒ Object

create a new job with a separate scheduler

Raises:

  • (ArgumentError)


29
30
31
32
# File 'lib/task_manager.rb', line 29

def schedule(key)
  raise ArgumentError unless block_given?
  jobs[key] = yield scheduler(key)
end

#scheduled?(key) ⇒ Boolean

check wether a job is scheduled

Returns:

  • (Boolean)


35
36
37
# File 'lib/task_manager.rb', line 35

def scheduled?(key)
  !jobs[key].nil?
end

#scheduler(key) ⇒ Object

return a named scheduler



24
25
26
# File 'lib/task_manager.rb', line 24

def scheduler(key)
  @schedulers[key] ||= Rufus::Scheduler.start_new
end

#unschedule(key) ⇒ Object

unschedule/stop a job



46
47
48
49
50
# File 'lib/task_manager.rb', line 46

def unschedule(key)
  jobs[key].unschedule
  jobs[key] = nil
  say "Job #{key} stopped"
end