Giter Site home page Giter Site logo

labor's Introduction

Labor

A wrapper for "gearman-ruby" which provides an easy mechanism for managing your jobs. Jobs are just Ruby classes which respond to the perform method.

Install

gem install labor

Get Started

Basic Rakefile:

require 'labor'
require 'labor/tasks'

class TestJob < Labor::Ability
  def perform
    puts "Hello, world!"

    true
  end
end

class AnotherTest < Labor::Ability
  def perform
    puts "Hello, world... again!"

    true
  end
end

Labor.servers = ["10.0.0.1:4370", "10.0.0.2:4370"]

Let's run the worker:

$ export ABILITIES=test-job,another-test
$ rake labor:work

The ABILITIES environment variable can accept multiple job names; each seperated by a comma.

Job names can also carry meta data associated with that particular worker. Meta data is any information that shouldn't affect what you name the job class in your system, but indicate to Gearman that this worker is unique. You can do this by putting your meta data inside [ and ] in the ability name. For example: email[123]. Everything within and including the [] symbols will be removed when referencing your worker class' perform method.

Configuration

Labor also allows you to load in a configuration file along with your worker. This configuration file is written in Ruby and offers a simple DSL (similar to the one present in Capistrano) for setting variables.

Configs look like this:

# settings.rb

set :foo, "bar"
set :i_am, "at the #{bar}"

group :credentials do
  set :username, "murkturgler"
  set :password, "supersecret"
end

The config file would then be loaded in like this:

Labor.config File.join(File.dirname(__FILE__), 'settings.rb')

When the worker is started up, Labor will load this config into the @config instance variable of your job class/module. You'll then be able to retrieve keys from the config within your job.

class TestJob < Labor::Ability
  def perform
    puts "Hey! I'm #{@config[:i_am]}"
    #=> "Hey! I'm at the bar"

    puts "Here's my username: #{@config.credentials.username}"
    #=> "Here's my username: murkturgler"

    true
  end
end

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.