Module: WarpCore

Defined in:
lib/warpcore/version.rb,
lib/warpcore/cache.rb,
lib/warpcore/config.rb,
lib/warpcore/sidekiq.rb,
lib/warpcore/dispatch.rb

Overview

This module provides a set of classes and components to build large scale, scalable and easier to maintain ruby-based applications with Parse.

Defined Under Namespace

Classes: Cache, Env, ParallelDispatchQueue, RedisPoolInstance, SerialDispatchQueue

Constant Summary collapse

VERSION =

Version number

"0.2.2"

Class Method Summary collapse

Class Method Details

.async(type = :serial) ⇒ Object

Helper method to run a block on a background thread.

Parameters:



29
30
31
32
33
34
35
36
# File 'lib/warpcore/dispatch.rb', line 29

def self.async(type = :serial)
  raise "You need to pass a block to async." unless block_given?
  if type == :parallel
    WarpCore::ParallelDispatchQueue.perform_async Proc.new
  else
    WarpCore::SerialDispatchQueue.perform_async Proc.new
  end
end

.cache(key, opts = {}) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/warpcore/cache.rb', line 7

def self.cache(key,opts = {})
  if block_given?
    WarpCore::Cache.get( key, opts, Proc.new )
  else
    WarpCore::Cache.get( key, opts )
  end
end

.delay_by(seconds, type = :serial) ⇒ Object

Helper method to run a block on a background thread.

Parameters:



42
43
44
45
46
47
48
49
# File 'lib/warpcore/dispatch.rb', line 42

def self.delay_by(seconds, type = :serial)
  raise "You need to pass a block to delay_by." unless block_given?
  if type == :parallel
    WarpCore::ParallelDispatchQueue.perform_in seconds.to_i, Proc.new
  else
    WarpCore::SerialDispatchQueue.perform_in seconds.to_i, Proc.new
  end
end

.dispatch(worker_name, *args) ⇒ Object

Asynchronously dispatch a Sidekiq worker with the provided arguments. This is equivalent to calling the worker directly with ‘perform_async`.

Examples:

WarpCore.dispatch 'Workers::MyWorker', argument1, argument2.....

Parameters:

  • worker_name (String)

    the full class path of the worker (ex. ‘Workers::MyWorker`)

  • args (Array)

    the arguments to pass to the worker.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/warpcore/sidekiq.rb', line 19

def self.dispatch(worker_name, *args)
  klass = worker_name.constantize
  if klass.respond_to?(:perform_async)
    puts "=> [Dispatch:Async] #{worker_name}"
    return klass.perform_async(*args)
  else
    warn "Dispatched #{worker_name} not a valid Sidekiq worker."
    nil
  end
rescue NameError => e
  puts "Worker [#{worker_name}] not defined."
  nil
end

.dispatch_in(worker_name, *args) ⇒ Object

Asynchronously dispatch a Sidekiq worker at a later time with the provided arguments. This is equivalent to calling the worker directly with ‘perform_in`.

Examples:

WarpCore.dispatch_in 'Workers::MyWorker', 10.seconds, argument1, argument2.....

Parameters:

  • args (Array)

    the arguments to pass to the worker. The first argument should be the amount of time to wait before dispatching the worker.

  • worker_name (String)

    the full class path of the worker (ex. ‘Workers::MyWorker`)



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/warpcore/sidekiq.rb', line 40

def self.dispatch_in(worker_name, *args)
  klass = worker_name.constantize
  if klass.respond_to?(:perform_async)
    puts "=> [Dispatch:Delay] #{worker_name}"
    return klass.perform_in(*args)
  else
    warn "Dispatched #{worker_name} not a valid Sidekiq worker."
    nil
  end
rescue NameError => e
  puts "Worker [#{worker_name}] not defined."
  nil
end

.dispatch_sync(worker_name, *args) ⇒ Object

Synchronously dispatch a Sidekiq worker with the provided arguments. This is equivalent to instantiating the worker instance directly and calling ‘perform` on it.

Examples:

WarpCore.dispatch_sync 'Workers::MyWorker', argument1, argument2.....

Parameters:

  • args (Array)

    the arguments to pass to the worker.

  • worker_name (String)

    the full class path of the worker (ex. ‘Workers::MyWorker`)



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/warpcore/sidekiq.rb', line 61

def self.dispatch_sync(worker_name, *args)
  klass = worker_name.constantize
  object = klass.new
  if object.respond_to?(:perform)
    puts "=> [Dispatch:Sync] #{worker_name}"
    return object.perform(*args)
  else
    warn "Perform #{worker_name} not a valid Sidekiq worker."
    nil
  end
rescue NameError => e
  puts "Worker [#{worker_name}] not defined."
  nil
end

.redisRedisPoolInstance

Returns a usable redis instance shared by Sidekiq.

Returns:



9
10
11
# File 'lib/warpcore/sidekiq.rb', line 9

def self.redis
  WarpCore::RedisPoolInstance.new
end