Class: CrocodileProcess
- Inherits:
-
Object
- Object
- CrocodileProcess
- Defined in:
- lib/crocodile/crocodile_process.rb
Instance Method Summary collapse
- #clean ⇒ Object
- #constantize(name) ⇒ Object
- #daemonize! ⇒ Object
-
#initialize(job_name, pids_dir) ⇒ CrocodileProcess
constructor
A new instance of CrocodileProcess.
- #schedule(launch_immediately = false) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(job_name, pids_dir) ⇒ CrocodileProcess
4 5 6 7 8 |
# File 'lib/crocodile/crocodile_process.rb', line 4 def initialize(job_name, pids_dir) @name = job_name @job_path = File.("jobs/#{job_name}") @pid_path = File.("#{job_name}.pid", pids_dir) end |
Instance Method Details
#clean ⇒ Object
54 55 56 57 58 |
# File 'lib/crocodile/crocodile_process.rb', line 54 def clean if File.exists?(@pid_path) File.delete(@pid_path) end end |
#constantize(name) ⇒ Object
60 61 62 |
# File 'lib/crocodile/crocodile_process.rb', line 60 def constantize(name) Kernel.const_get(name.split("_").push("job").map(&:capitalize).join) end |
#daemonize! ⇒ Object
49 50 51 52 |
# File 'lib/crocodile/crocodile_process.rb', line 49 def daemonize! Process.daemon(true) File.open(@pid_path, File::RDWR|File::EXCL|File::CREAT, 0600) { |io| io.write(Process.pid) } end |
#schedule(launch_immediately = false) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/crocodile/crocodile_process.rb', line 14 def schedule(launch_immediately=false) require @job_path job_class = constantize(@name) Signal.trap("INT") { EventMachine.stop } Signal.trap("TERM") { EventMachine.stop } EventMachine.run do EventMachine.next_tick do job_class.logger.info job_class. job_class.run EventMachine.stop if job_class.one_run_only end if launch_immediately timer = EventMachine::PeriodicTimer.new(job_class.interval || 60) do job_class.logger.info job_class. job_class.run if job_class.one_run_only timer.cancel EventMachine.stop end end end end |
#start ⇒ Object
10 11 12 |
# File 'lib/crocodile/crocodile_process.rb', line 10 def start schedule(launch_immediately=true) end |
#stop ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/crocodile/crocodile_process.rb', line 39 def stop if File.exists?(@pid_path) Process.kill :TERM, File.open(@pid_path).read.strip.to_i File.delete(@pid_path) true else false end end |