<commit_before># Insque

Instant queue. Background processing and message driven communication tool. Faster and simplier alternative to Resque.

## Installation

Add this line to your application's Gemfile:

    gem 'insque', :git => 'https://github.com/Gropher/Insque.git'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install insque

## Usage

At first you need to generate initializer and redis config file. Pass your sender name as parameter. 
Sender name is the unique identifier of your instance of insque. You can use several instances of insque to create message driven communication system 

    $ rails g insque:initializer somesender

### Sending

To broadcast message use:
```ruby
Insque.broadcast :message_name, {:params => {:some => :params}}
```
There is an easy way to use insque as background jobs processing. You can use `send_later` method to call any method of your rails models in background
. You still need listener running to make this work.
```ruby
@model = MyModel.first
@model.send_later :mymethod, 'some', 'params'
```

### Recieving

To start recieving messages you need to:

1. Create handler method in Insque module. First part of handler name is the name of the message sender.
```ruby
def somesender_message_name message
  #TODO: Handle message somehow
end
```

2. Call `listen` method in some background process or rake task:
```ruby
Insque.listen
```

   or just run `bundle exec rake insque:listener` from your console.

3. Call `janitor` method in some background process or rake task. Janitor will reissue failed messages or report error if message fails again. Janitor treats message as failed if it was not processed for an hour after broadcast or reissue.
```ruby
Insque.janitor
```

   or just run `bundle exec rake insque:janitor` from your console.

### Daemonizing

If you want to run insque listener as a daemon consider using [foreman](https://github.com/ddollar/foreman) for this.

Add foreman to your `Gemfile`:

    gem 'foreman', :git => 'git://github.com/danielfarrell/foreman.git', :branch => 'capistrano'

Install it:

    $ bundle install  

Create `Procfile`:

    listener: bundle exec rake insque:listener
    janitor: bundle exec rake insque:janitor

Run foreman from your console:
    
    $ bundle exec foreman start

For production use modify your capistrano deploy script somewhat like this:
    
    set :default_environment, {'PATH' => "/sbin:$PATH"}  # Optional. Useful if you get erros because start or restart command not found
    default_run_options[:pty] = true                     # Needed for RVM to work<commit_msg>Readme updated<commit_after># Insque

Instant queue. Background processing and message driven communication tool. Faster and simplier alternative to Resque.

## Installation

Add this line to your application's Gemfile:

    gem 'insque', :git => 'https://github.com/Gropher/Insque.git'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install insque

## Usage

At first you need to generate initializer and redis config file. Pass your sender name as parameter. 
Sender name is the unique identifier of your instance of insque. You can use several instances of insque to create message driven communication system 

    $ rails g insque:initializer somesender

### Sending

To broadcast message use:
```ruby
Insque.broadcast :message_name, {:params => {:some => :params}}
```
There is an easy way to use insque as background jobs processing. You can use `send_later` method to call any method of your rails models in background
. You still need listener running to make this work.
```ruby
@model = MyModel.first
@model.send_later :mymethod, 'some', 'params'
```

### Recieving

To start recieving messages you need to:

1. Create handler method in Insque module. First part of handler name is the name of the message sender.
```ruby
def somesender_message_name message
  #TODO: Handle message somehow
end
```

2. Call `listen` method in some background process or rake task:
```ruby
Insque.listen
```

   or just run `bundle exec rake insque:listener` from your console.

3. Call `janitor` method in some background process or rake task. Janitor will reissue failed messages or report error if message fails again. Janitor treats message as failed if it was not processed for an hour after broadcast or reissue.
```ruby
Insque.janitor
```

   or just run `bundle exec rake insque:janitor` from your console.

### Daemonizing

If you want to run insque listener as a daemon consider using [foreman](https://github.com/ddollar/foreman) for this.

Add foreman to your `Gemfile`:

    gem 'foreman', :git => 'git://github.com/danielfarrell/foreman.git', :branch => 'capistrano'

Install it:

    $ bundle install  

Create `Procfile`:

    listener: bundle exec rake insque:listener
    janitor: bundle exec rake insque:janitor

Run foreman from your console:
    
    $ bundle exec foreman start

For production use modify your capistrano deploy script somewhat like this:
    
    set :default_environment, {'PATH' => "/sbin:$PATH"}  # Optional. Useful if you get errors because start or restart command not found
    default_run_options[:pty] = true                     # Needed for RVM to work
{'fraction_non_alphanumeric': 0.0768348623853211, 'fraction_numerical': 0.0011467889908256881, 'mean_word_length': 3.9743346007604563, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5021387', 'n_tokens_mistral': 1504, 'n_tokens_neox': 1431, 'n_words': 678}