Class: TorqueBox::ScheduledJob

Inherits:
Object
  • Object
show all
Defined in:
lib/torquebox/scheduled_job.rb

Overview

This class is a Ruby API to manipulating TorqueBox scheduled jobs.

Class Method Summary collapse

Class Method Details

.at(class_name, options = {}) ⇒ java.util.concurrent.CountDownLatch

Note:

This is an asynchronous method.

Creates new ‘at’ job.

Examples:

Run a job every 200 ms for over 5 seconds, from now

TorqueBox::ScheduledJob.at('SimpleJob', :every => 200, :until => Time.now + 5)

Start in 1 second, then every 200 ms for over 4 seconds (5 seconds from now, but start is delayed):

TorqueBox::ScheduledJob.at('SimpleJob', :at => Time.now + 1, :every => 200, :until => Time.now + 5)

Start in 1 second, then every 200 ms for over 4 seconds (5 seconds from now, but start is delayed):

TorqueBox::ScheduledJob.at('SimpleJob', :in => 1_000, :every => 200, :until => Time.now + 5)

Start in 1 second, then repeat te job 10 times, every 200 ms

TorqueBox::ScheduledJob.at('SimpleJob', :in => 1_000, :repeat => 10, :every => 200)

Parameters:

  • class_name (String)

    The class name of the scheduled job to be executed

  • options (Hash) (defaults to: {})

    A hash containing the at job options:

Options Hash (options):

  • :at (Time)
    Time

    The start time of the job

  • :in (Fixnum)

    Specifies when the job execution should start, in ms

  • :repeat (Fixnum)

    Specifies the number of times to execute the job

  • :every (Fixnum)

    Specifies the delay (in ms) between job executions

  • :until (Time)

    The stop time of job execution

  • :name (String)

    The job name unique across the application, by default set to the job class name

  • :description (String)

    Job description

  • :timeout (String)

    The time after the job execution should be interrupted. By default it’ll never interrupt the job execution. Example: ‘2s’, ‘1m’

  • :config (Hash)

    Data that should be injected to the job constructor

  • :singleton (Boolean)

    Flag to determine if the job should be executed on every node (set to true) in the cluster or only on one node (set to false, default).

Returns:

  • (java.util.concurrent.CountDownLatch)

    The latch to wait for the task completion

See Also:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/torquebox/scheduled_job.rb', line 107

def at(class_name, options = {})
  raise "No job class name provided" if class_name.nil?
  raise "Invalid options for scheduling the job" if options.nil? or !options.is_a?(Hash)
  raise "Invalid type for :in, should be a Fixnum" if !options[:in].nil? and !options[:in].is_a?(Fixnum)
  raise "You can't specify both :at and :in" if options.has_key?(:at) and options.has_key?(:in)
  raise "You can't specify :repeat without :every" if options.has_key?(:repeat) and !options.has_key?(:every)
  raise "You can't specify :until without :every" if options.has_key?(:until) and !options.has_key?(:every)

  options = {
      :singleton => false,
      :name => class_name,
      :timeout => "0s",
      :repeat => 0,
      :every => 0,
      :at => Time.now,
      :async => true
  }.merge(options)

  if options.has_key?(:in)
    start = Time.now + options[:in] / 1000.0
  else
    start = options[:at]
  end

  with_schedulizer do |schedulizer|
    schedulizer.create_at_job(class_name.to_s, start, options[:until], options[:every], options[:repeat], options[:timeout], options[:name], options[:description], options[:config], options[:singleton])
  end
end

.at_sync(class_name, options = {}) ⇒ Boolean

Note:

This is a synchronous method.

Note:

This method accepts the same parameters as available in the at method.

Creates new ‘at’ job.

Returns:

  • (Boolean)

    true if the job was successfully created, false otherwise

See Also:



142
143
144
145
# File 'lib/torquebox/scheduled_job.rb', line 142

def at_sync(class_name, options = {})
  latch = at(class_name, options)
  wait_for_latch(latch)
end

.listArray<org.torquebox.jobs.ScheduledJob>

List all scheduled jobs of this application.

Returns:

  • (Array<org.torquebox.jobs.ScheduledJob>)

    the list of ScheduledJob instances - see lookup for more details on these instances



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/torquebox/scheduled_job.rb', line 180

def list
  prefix = job_service_name.canonical_name
  service_names = TorqueBox::MSC.service_names.select do |service_name|
    name = service_name.canonical_name
    name.start_with?(prefix) && !name.end_with?('mbean')
  end
  service_names.map do |service_name|
    service = TorqueBox::MSC.get_service(service_name)
    service.nil? ? nil : service.value
  end.select { |v| !v.nil? }
end

.lookup(name) ⇒ org.torquebox.jobs.ScheduledJob

Note:

The ScheduledJob instances returned by this and the list methods are not instances of this class but are instead Java objects of type org.torquebox.jobs.ScheduledJob. There are more methods available on these instances than what’s shown in the example here, but only the methods shown are part of our documented API.

Lookup a scheduled job of this application by name.

Examples:

Stop a scheduled job

job = TorqueBox::ScheduledJob.lookup('my_job')
job.name => 'my_job'
job.started? => true
job.status => 'STARTED'
job.stop
job.status => 'STOPPED'

Parameters:

  • name (String)

    the scheduled job’s name (as given in torquebox.rb or torquebox.yml)

Returns:

  • (org.torquebox.jobs.ScheduledJob)

    The ScheduledJob instance.



214
215
216
217
218
# File 'lib/torquebox/scheduled_job.rb', line 214

def lookup(name)
  service_name = job_service_name.append(name)
  service = TorqueBox::MSC.get_service(service_name)
  service.nil? ? nil : service.value
end

.remove(name) ⇒ java.util.concurrent.CountDownLatch

Removes a scheduled job.

This method removes the job asynchronously.

Parameters:

  • name (String)

    The job name.

Returns:

  • (java.util.concurrent.CountDownLatch)

    The latch to wait for the task completion

See Also:



154
155
156
157
158
159
160
161
# File 'lib/torquebox/scheduled_job.rb', line 154

def remove(name)
  raise "No job name provided" if name.nil?
  raise "Couldn't find a job with name '#{name}''" if TorqueBox::ScheduledJob.lookup(name).nil?

  with_schedulizer do |schedulizer|
    schedulizer.remove_job(name)
  end
end

.remove_sync(name) ⇒ Boolean

Note:

This is a synchronous method.

Note:

This method accepts the same parameters as available in the remove method.

Removes a scheduled job.

Returns:

  • (Boolean)

    true if the job was successfully removed, false otherwise

See Also:



169
170
171
172
# File 'lib/torquebox/scheduled_job.rb', line 169

def remove_sync(name)
  latch = remove(name)
  wait_for_latch(latch)
end

.schedule(class_name, cron, options = {}) ⇒ java.util.concurrent.CountDownLatch

Note:

This is an asynchronous method.

Creates a new scheduled job.

Examples:

A simple job

TorqueBox::ScheduledJob.schedule('SimpleJob', "*/10 * * * * ?")

A simple job with custom name

TorqueBox::ScheduledJob.schedule('SimpleJob', "*/10 * * * * ?", :name => "simple.job")

Schedule a job with data to be injected to the job constructor

ScheduledJob.schedule('SimpleJob', "*/10 * * * * ?", :name => "simple.config.job", :config => {:text => "text", :hash => {:a => 2}})

Schedule a job stopped after creation

TorqueBox::ScheduledJob.schedule('SimpleJob', "*/10 * * * * ?", :stopped => true)

Parameters:

  • class_name

    The scheduled job implementation class name

  • cron

    The cron expression defining when the job should run

  • options (defaults to: {})

    Optional parameters (a Hash), including:

Options Hash (options):

  • :name (String)

    The job name unique across the application, if not provided set to the class name

  • :description (String)

    Job description

  • :timeout (String)

    The time after the job execution should be interrupted. By default it’ll never interrupt the job execution. Example: ‘2s’, ‘1m’

  • :config (Hash)

    Data that should be injected to the job constructor

  • :stopped (Boolean)

    If the job should be stopped after installation (default: false)

  • :singleton (Boolean)

    Flag to determine if the job should be executed on every node (set to true, default) in the cluster or only on one node (set to false).

Returns:

  • (java.util.concurrent.CountDownLatch)

    The latch to wait for the task completion

See Also:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/torquebox/scheduled_job.rb', line 51

def schedule(class_name, cron, options = {})
  raise "No job class name provided" if class_name.nil?
  raise "No cron expression provided" if cron.nil?

  options = {
      :name => class_name.to_s,
      :singleton => true,
      :stopped => false,
      :timeout => "0s"
  }.merge(options)

  with_schedulizer do |schedulizer|
    schedulizer.create_job(class_name.to_s, cron, options[:timeout], options[:name], options[:description], options[:config], options[:singleton], options[:stopped])
  end
end

.schedule_sync(class_name, cron, options = {}) ⇒ Boolean

Note:

This is a synchronous method.

Note:

This method accepts the same parameters as available in the schedule method.

Creates a new scheduled job.

Returns:

  • (Boolean)

    true if the job was successfully created, false otherwise

See Also:



73
74
75
76
# File 'lib/torquebox/scheduled_job.rb', line 73

def schedule_sync(class_name, cron, options = {})
  latch = schedule(class_name, cron, options)
  wait_for_latch(latch)
end