Class: Ruboty::Cron::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/ruboty/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/ruboty/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/ruboty/cron/job.rb', line 4

def attributes
  @attributes
end

#threadObject (readonly)

Returns the value of attribute thread.



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

def thread
  @thread
end

Instance Method Details

#bodyObject



53
54
55
# File 'lib/ruboty/cron/job.rb', line 53

def body
  attributes["body"]
end

#descriptionObject



41
42
43
# File 'lib/ruboty/cron/job.rb', line 41

def description
  %<%5s: (%s) "%s" %s> % [id, suspended? ? "suspended" : "active", schedule, body]
end

#idObject



45
46
47
# File 'lib/ruboty/cron/job.rb', line 45

def id
  attributes["id"]
end

#resume(robot) ⇒ Object



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

def resume(robot)
  start(robot)
  attributes.delete("suspended") if attributes.has_key?("suspended")
end

#scheduleObject



49
50
51
# File 'lib/ruboty/cron/job.rb', line 49

def schedule
  attributes["schedule"]
end

#start(robot) ⇒ Object



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

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

#stopObject



27
28
29
# File 'lib/ruboty/cron/job.rb', line 27

def stop
  thread.kill
end

#suspendObject



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

def suspend
  stop
  attributes["suspended"] = true
end

#suspended?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/ruboty/cron/job.rb', line 57

def suspended?
  !!attributes["suspended"]
end

#to_hashObject



23
24
25
# File 'lib/ruboty/cron/job.rb', line 23

def to_hash
  attributes
end