Class: Moon::Scheduler

Inherits:
Object show all
Includes:
Activatable
Defined in:
lib/moon/packages/scheduler/scheduler.rb,
lib/moon/packages/scheduler/jobs/base.rb,
lib/moon/packages/scheduler/jobs/timeout.rb,
lib/moon/packages/scheduler/jobs/process.rb,
lib/moon/packages/scheduler/jobs/interval.rb,
lib/moon/packages/scheduler/jobs/time_base.rb,
lib/moon/packages/scheduler/jobs/timed_process.rb

Overview

A Scheduler is used to manage Job objects, any object that implements a #done? and #update can be used. Adding a new job is done via #add, when a job is #done?, it will be removed from the list of jobs, no further operations are done on the done job.

Defined Under Namespace

Modules: Jobs

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Activatable

#activate, #active?, #deactivate

Constructor Details

#initializeScheduler

Returns a new instance of Scheduler.


27
28
29
30
31
32
# File 'lib/moon/packages/scheduler/scheduler.rb', line 27

def initialize
  @jobs = []
  @active = true
  @ticks = 0
  @uptime = 0.0
end

Instance Attribute Details

#activeBoolean

Returns active is the object active?.

Returns:

  • (Boolean)

    active is the object active?


13
14
15
# File 'lib/moon/packages/scheduler/scheduler.rb', line 13

def active
  @active
end

#jobsArray<Moon::Scheduler::Job::Base*>

Returns:

  • (Array<Moon::Scheduler::Job::Base*>)

17
18
19
# File 'lib/moon/packages/scheduler/scheduler.rb', line 17

def jobs
  @jobs
end

#ticksObject (readonly)

Returns the value of attribute ticks


25
26
27
# File 'lib/moon/packages/scheduler/scheduler.rb', line 25

def ticks
  @ticks
end

#uptimeObject (readonly)

Returns the value of attribute uptime


21
22
23
# File 'lib/moon/packages/scheduler/scheduler.rb', line 21

def uptime
  @uptime
end

Instance Method Details

#add(job) ⇒ self

Add a job to the Scheduler

Parameters:

Returns:

  • (self)

38
39
40
41
# File 'lib/moon/packages/scheduler/scheduler.rb', line 38

def add(job)
  @jobs.push job
  job
end

#clearself

Clears all jobs, this will ignore the state of the job.

Returns:

  • (self)

46
47
48
49
# File 'lib/moon/packages/scheduler/scheduler.rb', line 46

def clear
  @jobs.clear
  self
end

#every(duration, &block) ⇒ Moon::Scheduler::Jobs::Interval

Creates a new Interval job

Parameters:

Returns:


87
88
89
# File 'lib/moon/packages/scheduler/scheduler.rb', line 87

def every(duration, &block)
  add Jobs::Interval.new(duration, &block)
end

#in(duration, &block) ⇒ Moon::Scheduler::Jobs::Timeout

Creates a Timeout job

Parameters:

Returns:


95
96
97
# File 'lib/moon/packages/scheduler/scheduler.rb', line 95

def in(duration, &block)
  add Jobs::Timeout.new(duration, &block)
end

#killVoid

Kill all active jobs.

Returns:

  • (Void)

78
79
80
81
# File 'lib/moon/packages/scheduler/scheduler.rb', line 78

def kill
  return unless @jobs
  @jobs.each(&:kill)
end

#remove(job) ⇒ Object

Removes a job.

Parameters:

  • job (Moon::Scheduler::Job::Base*)

Returns:

  • (Object)

    the job removed


55
56
57
# File 'lib/moon/packages/scheduler/scheduler.rb', line 55

def remove(job)
  @jobs.delete(job)
end

#remove_by_id(id) ⇒ Object

Removes a job by id.

Parameters:

Returns:

  • (Object)

    the job removed


63
64
65
# File 'lib/moon/packages/scheduler/scheduler.rb', line 63

def remove_by_id(id)
  @jobs.delete { |job| job.id == id }
end

#remove_by_key(key) ⇒ Array

Removes jobs by key.

Parameters:

Returns:

  • (Array)

    the jobs removed


71
72
73
# File 'lib/moon/packages/scheduler/scheduler.rb', line 71

def remove_by_key(key)
  @jobs.reject! { |job| job.key == key }
end

#run(&block) ⇒ Moon::Scheduler::Jobs::Process

Creates a Process job


110
111
112
# File 'lib/moon/packages/scheduler/scheduler.rb', line 110

def run(&block)
  add Jobs::Process.new(&block)
end

#run_for(duration, &block) ⇒ Moon::Scheduler::Jobs::TimedProcess

Creates a TimedProcess job

Parameters:

Returns:


103
104
105
# File 'lib/moon/packages/scheduler/scheduler.rb', line 103

def run_for(duration, &block)
  add Jobs::TimedProcess.new(duration, &block)
end

#update(delta) ⇒ Object

Frame update

Parameters:


117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/moon/packages/scheduler/scheduler.rb', line 117

def update(delta)
  return unless active?
  dead = []
  @jobs.each do |job|
    if job.done?
      dead << job
      next
    end
    job.update delta
  end
  @jobs -= dead unless dead.empty?
  @uptime += delta
  @ticks += 1
end