Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ogom committed Apr 22, 2013
0 parents commit ff1b8be
Show file tree
Hide file tree
Showing 17 changed files with 228 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/config/*.yml
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
gem 'sidekiq', '2.10.0'
gem 'slim'
gem 'sinatra', '>= 1.3.0', :require => nil

gem 'sidekiq-status'
gem 'sidekiq-unique-jobs'
gem 'sidekiq-failures'

gem 'clockwork'
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Redmine Sidekiq
===============

Background jobs will use the [Sidekiq](https://github.com/mperham/sidekiq) on Redmine.

## Features

* Administrator can use the Sidekiq Web UI from the top menu.
* Add to autoload_paths of the Plugin '/app/workers'.
* Add Middleware of 'sidekiq-status'.

## Installation

```
$ git clone https://github.com/ogom/redmine_sidekiq ./plugins/redmine_sidekiq
```

## Example

Example of Sidekiq worker.

```
./plugins/redmine_sidekiq/workers/sandbox_worker.rb
```

## Web UI
Enqueue from the Web UI.

```
http://[redmine]/sidekiq/sandbox
```

## CLI

Enqueue from the command line.

```
$ script/rails runner 'SandboxWorker.perform_async'
```

## License

* MIT
30 changes: 30 additions & 0 deletions app/controllers/sidekiq_sandbox_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class SidekiqSandboxController < ApplicationController
unloadable
before_filter :require_admin

def index
@stats = Sidekiq::Stats.new
end

def perform_async
name = params['name']
count = params['count']
jid = SandboxWorker.perform_async(name, count)
flash[:notice] = "Enqueued job id: #{jid}" if jid
redirect_to :action => 'index'
end

def perform_in
interval = params['interval'].to_i ||= 2
jid = SandboxWorker.perform_in(interval.minute)
flash[:notice] = "Enqueued job id: #{jid}" if jid
redirect_to :action => 'index'
end

def perform_at
interval = params['interval'].to_i ||= 2
jid = SandboxWorker.perform_at(interval.minute.from_now)
flash[:notice] = "Enqueued job id: #{jid}" if jid
redirect_to :action => 'index'
end
end
2 changes: 2 additions & 0 deletions app/helpers/sidekiq_sandbox_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module SidekiqSandboxHelper
end
36 changes: 36 additions & 0 deletions app/views/sidekiq_sandbox/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<h2>Sidekiq Sandbox</h2>

<hr>

<h3>Stats</h3>

<p>stats = Sidekiq::Stats.new</p>

<h4>The number of jobs that have been processed.</h4>
<p>stats.processed # => <%= @stats.processed %></p>

<h4>The number of jobs that have failed.</h4>
<p>stats.failed # => <%= @stats.failed %></p>

<h4>The queues with name and number enqueued.</h4>
<p>stats.queues # => <%= @stats.queues %></p>

<h4>The number of jobs enqueued in all queues (does NOT include retries and scheduled jobs).</h4>
<p>stats.enqueued # => <%= @stats.enqueued %></p>

<hr>

<h3>Jobs</h3>

<h4>The standard.</h4>
<p><%= link_to('perform_async(*args)', sidekiq_sandbox_perform_async_path, method: :post) %></p>

<h4>The Scheduled Jobs by interval.</h4>
<p><%= link_to('perform_in(interval, *args)', sidekiq_sandbox_perform_in_path(interval: 1), method: :post) %></p>

<h4>The Scheduled Jobs by timestamp.</h4>
<p><%= link_to('perform_at(timestamp, *args)', sidekiq_sandbox_perform_at_path(interval: 1), method: :post) %></p>

<h4>The job will fail.</h4>
<p><%= link_to('perform_async()', sidekiq_sandbox_perform_async_path(name: :failed), method: :post) %></p>

10 changes: 10 additions & 0 deletions app/workers/sandbox_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class SandboxWorker
include Sidekiq::Worker
include Sidekiq::Status::Worker
sidekiq_options retry: 2, unique: true

def perform(name=nil, count=nil)
puts 'Doing sandbox work'
raise 'Failed sandbox work' if name == 'failed'
end
end
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
en:
Sidekiq: 'Sidekiq'
Failures: 'Failures'
3 changes: 3 additions & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ja:
Sidekiq: 'Sidekiq'
Failures: 'Failures'
11 changes: 11 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'sidekiq/web'
require 'redmine_sidekiq/admin_constraint'

RedmineApp::Application.routes.draw do
mount Sidekiq::Web => '/sidekiq', :constraints => RedmineSidekiq::AdminConstraint.new

match '/sidekiq/sandbox', to: 'sidekiq_sandbox#index', via: :get
match '/sidekiq/sandbox/perform_async', to: 'sidekiq_sandbox#perform_async', via: :post
match '/sidekiq/sandbox/perform_in', to: 'sidekiq_sandbox#perform_in', via: :post
match '/sidekiq/sandbox/perform_at', to: 'sidekiq_sandbox#perform_at', via: :post
end
13 changes: 13 additions & 0 deletions config/sidekiq.yml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
development:
redis:
url: redis://localhost:6379/8
namespace: mynamespace
status:
expiration: 30

production:
redis:
url: redis://localhost:6379/8
namespace: mynamespace
status:
expiration: 30
13 changes: 13 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'redmine_sidekiq/configure'
require 'redmine_sidekiq/rails'

Redmine::Plugin.register :redmine_sidekiq do
name 'Redmine Sidekiq plugin'
description 'This is a Sidekiq plugin for Redmine'
version '0.0.1'
url 'https://github.com/ogom/redmine_sidekiq'
author_url 'mailto:[email protected]'
author 'ogom'

menu :top_menu, :sidekiq, '/sidekiq', :if => Proc.new {User.current.admin}
end
7 changes: 7 additions & 0 deletions lib/redmine_sidekiq/admin_constraint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module RedmineSidekiq
class AdminConstraint
def matches?(request)
User.current.admin
end
end
end
25 changes: 25 additions & 0 deletions lib/redmine_sidekiq/configure.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'sidekiq'
require 'sidekiq-status'

module RedmineSidekiq
class Configure
file = File.join(Rails.root, 'plugins/redmine_sidekiq/config/sidekiq.yml')
config = YAML.load_file(file)[Rails.env]
redis_conf = config['redis'].symbolize_keys
st_expire = config['status']['expiration'].to_i

Sidekiq.configure_server do |config|
config.redis = redis_conf
config.server_middleware do |chain|
chain.add Sidekiq::Status::ServerMiddleware, expiration: st_expire.minutes
end
end

Sidekiq.configure_client do |config|
config.redis = redis_conf
config.client_middleware do |chain|
chain.add Sidekiq::Status::ClientMiddleware
end
end
end
end
12 changes: 12 additions & 0 deletions lib/redmine_sidekiq/rails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module RedmineSidekiq
class Rails
Dir.glob(File.join(Redmine::Plugin.directory, '*')).sort.each do |directory|
if File.directory?(directory)
workers = File.join(directory, "app", "workers")
if File.directory?(workers)
ActiveSupport::Dependencies.autoload_paths += [workers]
end
end
end
end
end
8 changes: 8 additions & 0 deletions test/functional/sidekiq_sandbox_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require File.expand_path('../../test_helper', __FILE__)

class SidekiqSandboxControllerTest < ActionController::TestCase
# Replace this with your real tests.
def test_truth
assert true
end
end
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Load the Redmine helper
require File.expand_path(File.dirname(__FILE__) + '/../../../test/test_helper')

0 comments on commit ff1b8be

Please sign in to comment.