Class: Ellen::Cron::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/ellen/cron/job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Job

Returns a new instance of Job.



6
7
8
# File 'lib/ellen/cron/job.rb', line 6

def initialize(attributes)
  @attributes = attributes.stringify_keys
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



4
5
6
# File 'lib/ellen/cron/job.rb', line 4

def attributes
  @attributes
end

#threadObject (readonly)

Returns the value of attribute thread.



4
5
6
# File 'lib/ellen/cron/job.rb', line 4

def thread
  @thread
end

Instance Method Details

#bodyObject



40
41
42
# File 'lib/ellen/cron/job.rb', line 40

def body
  attributes["body"]
end

#descriptionObject



28
29
30
# File 'lib/ellen/cron/job.rb', line 28

def description
  %<%5s: "%s" %s> % [id, schedule, body]
end

#fromObject



44
45
46
# File 'lib/ellen/cron/job.rb', line 44

def from
  attributes["from"]
end

#idObject



32
33
34
# File 'lib/ellen/cron/job.rb', line 32

def id
  attributes["id"]
end

#scheduleObject



36
37
38
# File 'lib/ellen/cron/job.rb', line 36

def schedule
  attributes["schedule"]
end

#start(robot) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/ellen/cron/job.rb', line 10

def start(robot)
  @thread = Thread.new do
    Chrono::Trigger.new(schedule) do
      Message.new(
        attributes.symbolize_keys.except(:body, :id, :schedule).merge(robot: robot)
      ).reply(body)
    end.run
  end
end

#stopObject



24
25
26
# File 'lib/ellen/cron/job.rb', line 24

def stop
  thread.kill
end

#toObject



48
49
50
# File 'lib/ellen/cron/job.rb', line 48

def to
  attributes["to"]
end

#to_hashObject



20
21
22
# File 'lib/ellen/cron/job.rb', line 20

def to_hash
  attributes
end