Module: Threasy

Defined in:
lib/threasy.rb,
lib/threasy/work.rb,
lib/threasy/config.rb,
lib/threasy/version.rb,
lib/threasy/schedule.rb,
lib/threasy/schedule/entry.rb

Defined Under Namespace

Classes: Config, Schedule, Work

Constant Summary collapse

VERSION =
"0.5.0"

Class Method Summary collapse

Class Method Details

.config {|@@config| ... } ⇒ Object

Returns default instance of ‘Threasy::Config`.

Can be used with a block for changing multiple configs.

Threasy.config do |c|
  c.max_sleep   = 10.minutes
  c.max_overdue = 1.hour
end

Parameters

  • ‘&block` - Optional block that will be yielded the config object

Returns

  • ‘Threasy::Config` instance

Yields:



29
30
31
32
33
# File 'lib/threasy.rb', line 29

def self.config
  @@config ||= Config.new
  yield @@config if block_given?
  @@config
end

.enqueue(*args, &block) ⇒ Object

Shortcut to enqueue work into default ‘Threasy::Work` instance.

Examples

# Enqueue blocks
Threasy.enqueue { do_some_background_work }

# Enqueue objects that respond to `perform` or `call`
Threasy.enqueue BackgroundJob.new(some: data)

# Enqueue strings that can be evals to an object
Threasy.enqueue("BackgroundJob.new")


62
63
64
# File 'lib/threasy.rb', line 62

def self.enqueue(*args, &block)
  work.enqueue *args, &block
end

.loggerObject

Shortcut for ‘Threasy::Config#logger`



36
37
38
# File 'lib/threasy.rb', line 36

def self.logger
  config.logger
end

.schedule(*args, &block) ⇒ Object

Shortcut to schedule work with the default ‘Threasy::Schedule` instance.

Examples

# Schedule blocks
Threasy.schedule(in: 5.min) { do_some_background_work }

# Schedule job objects that respond to `perform` or `call`
Threasy.schedule(BackgroundJob.new(some: data), every: 1.hour)

# Schedule strings that can be evals to a job object
Threasy.schedule("BackgroundJob.new", every: 1.day)

Parameters

  • ‘job` - Job object which responds to `perform` or `call`

  • ‘options`

    • ‘every: n` - If present, job is repeated every `n` seconds

    • ‘in: n` - `n` seconds until job is executed

    • ‘at: Time` - Time to execute job at

  • ‘&block` - Job block

Must have either a ‘job` object or job `&block` present.

Returns

  • ‘Threasy::Schedule::Entry` if job was successfully added to schedule

  • ‘nil` if job was for the past



103
104
105
# File 'lib/threasy.rb', line 103

def self.schedule(*args, &block)
  schedules.add *args, &block
end

.schedulesObject

Shortcut for default ‘Threasy::Schedule` instance.

Returns

  • ‘Threasy::Schedule` instance



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

def self.schedules
  config.schedule ||= Schedule.new(work)
end

.workObject

Shortcut for default ‘Threasy::Work` instance.

Returns

  • ‘Threasy::Work` instance



45
46
47
# File 'lib/threasy.rb', line 45

def self.work
  config.work ||= Work.new
end