Class: Lazylead::Schedule

Inherits:
Object
  • Object
show all
Defined in:
lib/lazylead/schedule.rb

Overview

The tasks schedule

Author

Yurii Dubinka ([email protected])

Copyright

Copyright © 2019-2020 Yurii Dubinka

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(log: Log.new, cling: true, trigger: Rufus::Scheduler.new) ⇒ Schedule

Returns a new instance of Schedule.



37
38
39
40
41
# File 'lib/lazylead/schedule.rb', line 37

def initialize(log: Log.new, cling: true, trigger: Rufus::Scheduler.new)
  @log = log
  @cling = cling
  @trigger = trigger
end

Instance Method Details

#joinObject



65
66
67
# File 'lib/lazylead/schedule.rb', line 65

def join
  @trigger.join if @cling
end

#psObject

TODO:

#/DEV inspect the current execution status. This method should support several format for output, by default is ‘json`.



61
62
63
# File 'lib/lazylead/schedule.rb', line 61

def ps
  @log.debug "#{self}#ps"
end

#register(task) ⇒ Object

TODO:

#/DEV error code is required for each ‘raise’ statement within the application. Align the naming of existing one, the error code should be like ll-xxx.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/lazylead/schedule.rb', line 46

def register(task)
  raise "ll-002: task can't be a null" if task.nil?
  @trigger.method(task.type).call(task.unit) do
    ActiveRecord::Base.connection_pool.with_connection do
      t = task
      t = ORM::Verbose.new(t, @log) unless t.props.key? "no_logs"
      t = ORM::Retry.new(t, @log)
      t.exec
    end
  end
  @log.debug "Task scheduled: #{task}"
end

#stopObject

TODO:

#/DEV stop the execution of current jobs (shutdown?). The test is required.



71
72
73
# File 'lib/lazylead/schedule.rb', line 71

def stop
  @trigger.shutdown(:kill)
end