Class: Beehive::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/beehive/client.rb

Overview

The client class that can be used to add and manage jobs in the queue.

Author:

  • Yorick Peterse

Since:

  • 0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Creates a new instance of the client and sends the specified options to Redis.

Redis#initialize for more information.

Parameters:

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

    A hash containing options to use for Redis. See

Author:

  • Yorick Peterse

Since:

  • 0.1



20
21
22
# File 'lib/beehive/client.rb', line 20

def initialize(options = {})
  @connection = ::Redis.new(options)
end

Instance Attribute Details

#connectionObject (readonly)

Instance of the class “Redis”

Since:

  • 0.1



10
11
12
# File 'lib/beehive/client.rb', line 10

def connection
  @connection
end

Instance Method Details

#disconnectObject

Closes the Redis connection.

Author:

  • Yorick Peterse

Since:

  • 0.1



66
67
68
# File 'lib/beehive/client.rb', line 66

def disconnect
  @connection.quit
end

#get(job) ⇒ Array

Retrieves the last job and removes it from the storage.

Parameters:

  • job (String)

    The name of the type of job to retrieve.

Returns:

  • (Array)

Author:

  • Yorick Peterse

Since:

  • 0.1



51
52
53
54
55
56
57
58
# File 'lib/beehive/client.rb', line 51

def get(job)
  job = "beehive.jobs.#{job}"
  job = @connection.lpop(job)
  
  if !job.nil?
    return JSON.load(job)
  end
end

#queue(job, params = {}) ⇒ Object

Queues a given job in the storage.

Examples:

client = Beehive::Client.new
client.queue('video', :title => 'Hello world')

Parameters:

  • job (String)

    The name of the job to queue.

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

    The parameters to send to the job.

Author:

  • Yorick Peterse

Since:

  • 0.1



36
37
38
39
40
41
# File 'lib/beehive/client.rb', line 36

def queue(job, params = {})
  job    = "beehive.jobs.#{job}"
  params = JSON.dump(params)

  @connection.rpush(job, params)
end