resque-serial 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MWIxNDQwYzM3MDYwNDc4MjUyZGNjYmJiYjQxNGY4NDZiZmFmNmY3Mg==
5
+ data.tar.gz: !binary |-
6
+ NGNiYzAzOTA0Yjg1YTI0YjQ2YjhmODRmZTIzZGMxNTNiYWViMmE5ZQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ OWIxNmQ2ZjQ5NmRkZjUwOThlZjJlZWU0ZTRkZWJjM2MzYjQ4OGEzMWIyZDAx
10
+ Mzg0Y2YxMGE4NjNiN2RiMzliZmNlNzVkZjBjNzMyYTU4OThhZTA0MmRmNGQw
11
+ ZjIyMDg0NTEwMTQ3MDUwZmQwM2YzZmYxNGQ4MTViNzEzMjQ3YmI=
12
+ data.tar.gz: !binary |-
13
+ M2FkMWY0NzE5OGNmM2NiNmMxNmQzNjUwNmQwNGZlNjdjOGQ4ZTA3YjRiMjEw
14
+ Y2VmMWUxN2E2YzUzMTVkMzYwYTVkMjc3Njk0Mzg0OTQwMjM4OTY0M2MyY2Zl
15
+ Yzg3ZDZmYWMxODE5MWE2OWExNmFhNTE2NjMzZGI5ZDI1YjA4NjU=
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in resque-serial.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Max Kazarin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Resque::Serial
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'resque-serial'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install resque-serial
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require './lib/resque-serial/server'
@@ -0,0 +1,34 @@
1
+ module ResqueSerial
2
+ class AsyncJob
3
+ class << self
4
+ def queue
5
+ :async
6
+ end
7
+
8
+ # Add a job to queue. Queue name is a class module name
9
+ def create(target, queue, *args)
10
+ Resque.enqueue self, enqueue_payload(target, queue, *args)
11
+ end
12
+
13
+ def enqueue_payload(target, queue, *args)
14
+ method = args.shift()
15
+ options = {
16
+ class: target.class.to_s,
17
+ id: target.id.to_s,
18
+ method: method,
19
+ args: args,
20
+ }
21
+ options[:scope] = target.scope.id.to_s if target.scope
22
+
23
+ options
24
+ end
25
+
26
+ def perform(options)
27
+ options.symbolize_keys!
28
+ model = options[:class].constantize.unscoped.find(options[:id])
29
+ model.scope = options.delete(:scope)
30
+ model.send options[:method], *options[:args]
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,40 @@
1
+ module ResqueSerial
2
+ module Extender
3
+ class SyncProxy
4
+ def initialize(target, queue, async)
5
+ @queue = queue
6
+ @target = target
7
+ @async = async
8
+ end
9
+
10
+ def method_missing(*args)
11
+ if @async
12
+ AsyncJob.create @target, @queue, *args
13
+ else
14
+ SyncJob.create @target, @queue, *args
15
+ end
16
+ end
17
+ end
18
+
19
+ def delay(options = {})
20
+ SyncProxy.new self, options[:queue] || self.queue, options[:async]
21
+ end
22
+
23
+ def delay_size(queue = nil)
24
+ SyncJob.size_of(queue || self.queue)
25
+ end
26
+
27
+ module ClassMethods
28
+ def priorities
29
+ @priorities ||= {}
30
+ end
31
+
32
+ def priority(name, *args)
33
+ args.each { |arg| self.priorities[arg] = name }
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ Object.send(:include, ResqueSerial::Extender)
40
+ Module.send(:include, ResqueSerial::Extender::ClassMethods)
@@ -0,0 +1,49 @@
1
+ module ResqueSerial
2
+ module Lockable
3
+
4
+ def lock_timeout
5
+ 3600
6
+ end
7
+
8
+ def lock_key(*args)
9
+ raise NotImplementedError
10
+ end
11
+
12
+ def get_lock(*args)
13
+ "lock:"+lock_key(*args).to_s
14
+ end
15
+
16
+ def requeue_perform_delay
17
+ 1.0
18
+ end
19
+
20
+ def before_perform_lock(*args)
21
+ if lock_key *args
22
+ if Resque.redis.setnx get_lock(*args), true
23
+ Resque.redis.expire get_lock(*args), lock_timeout
24
+ else
25
+ sleep requeue_perform_delay
26
+
27
+ Resque.enqueue self, *args
28
+
29
+ raise Resque::Job::DontPerform
30
+ end
31
+ end
32
+ end
33
+
34
+ def clear_lock(*args)
35
+ Resque.redis.del(get_lock(*args))
36
+ end
37
+
38
+ def around_perform_lock(*args)
39
+ yield
40
+ ensure
41
+ clear_lock(*args)
42
+ end
43
+
44
+ def on_failure_lock(exception, *args)
45
+ clear_lock(*args)
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,18 @@
1
+ <h1>Pending jobs on <span class="hl"><%= @serial_name %></span></h1>
2
+ <form method="POST" action="/serials/<%= @serial_name %>/remove" class="remove-queue">
3
+ <input type="submit" name="" value="Remove Serial" onclick="return confirm('Are you absolutely sure? This cannot be undone');">
4
+ </form>
5
+ <p class="sub">Showing <b><%= @count %></b> jobs</p>
6
+ <table class="jobs">
7
+ <tbody><tr>
8
+ <th>Class</th>
9
+ <th>Args</th>
10
+ </tr>
11
+ <% @jobs.each do |job| %>
12
+ <tr>
13
+ <td class="class"><%= "#{job[:class]}:#{job[:id]}:#{job[:method]}" %></td>
14
+ <td class="args"><%= job[:args] %></td>
15
+ </tr>
16
+ <% end %>
17
+ </tbody>
18
+ </table>
@@ -0,0 +1,16 @@
1
+ <h1 class="wi">Serials</h1>
2
+ <p class="intro">The list below contains all the registered serials queues with the number of jobs currently in the queue. Select a queue from above to view all jobs currently pending on the queue.</p>
3
+ <table class="queues">
4
+ <tbody><tr>
5
+ <th>Name</th>
6
+ <th>Jobs</th>
7
+ </tr>
8
+ <% @serials.each do |serial| %>
9
+ <tr>
10
+ <td class="queue"><a class="queue" href="/serials/<%= serial[:name] %>"><%= serial[:name]%></a></td>
11
+ <td class="size"><%= serial[:size] %></td>
12
+ </tr>
13
+ <% end %>
14
+ </tbody>
15
+ </table>
16
+
@@ -0,0 +1,46 @@
1
+ require 'resque-serial'
2
+ require 'resque/server'
3
+ require 'yaml'
4
+
5
+ module ResqueSerial
6
+ module Server
7
+ def self.registered(app)
8
+
9
+ app.class_eval do
10
+
11
+ helpers do
12
+ def render_view(filename)
13
+ erb File.read(File.join(File.dirname(__FILE__), 'server', 'views', "#{filename}.erb"))
14
+ end
15
+ end
16
+
17
+ get '/serials' do
18
+ @serials = Redis.current.keys 'resque:syncjobs:*'
19
+ @serials.map! { |serial| { name: serial.split(':').last, size: Redis.current.llen(serial) } }
20
+ render_view :serials
21
+ end
22
+
23
+ get '/serials/:id' do
24
+ @serial_name = params[:id]
25
+ serial = "resque:syncjobs:#{@serial_name}"
26
+ @count = Redis.current.llen serial
27
+ @jobs = Redis.current.lrange serial, 0, @count
28
+
29
+ @jobs.map! { |job| YAML.load job }
30
+ render_view :serial
31
+ end
32
+
33
+ post '/serials/:id/remove' do
34
+ serial = "resque:syncjobs:#{params[:id]}"
35
+ Redis.current.del serial
36
+ redirect u(:serials)
37
+ end
38
+ end
39
+ end
40
+
41
+ Resque::Server.tabs << 'Serials'
42
+
43
+ end
44
+ end
45
+
46
+ Resque::Server.register ResqueSerial::Server
@@ -0,0 +1,52 @@
1
+ module ResqueSerial
2
+ class SyncJob
3
+ extend Lockable
4
+
5
+ attr_reader :options
6
+
7
+ class << self
8
+ def queue
9
+ :sync
10
+ end
11
+
12
+ def lock_key(queue)
13
+ queue
14
+ end
15
+
16
+ # Add a job to queue. Queue name is a class module name
17
+ def create(target, queue, *args)
18
+ enqueue_payload target, queue, *args
19
+
20
+ Resque.enqueue(self, queue)
21
+ end
22
+
23
+ def enqueue_payload(target, queue, *args)
24
+ method = args.shift()
25
+ options = {
26
+ class: target.class.to_s,
27
+ id: target.id.to_s,
28
+ method: method,
29
+ args: args,
30
+ }
31
+ options[:scope] = target.scope.id.to_s if target.scope
32
+
33
+ Resque.redis.rpush "syncjobs:#{queue}", options.to_yaml
34
+ end
35
+
36
+ def dequeue_payload(queue)
37
+ YAML.load Resque.redis.lpop "syncjobs:#{queue}"
38
+ end
39
+
40
+ def perform(queue)
41
+ options = dequeue_payload queue
42
+ model = options[:class].constantize.unscoped.find(options[:id])
43
+ model.scope = options.delete(:scope)
44
+ model.send options[:method], *options[:args]
45
+ end
46
+
47
+ def size_of(queue)
48
+ Resque.redis.llen("syncjobs:#{queue}")
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,5 @@
1
+ module Resque
2
+ module Serial
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'resque'
3
+ require 'active_support/core_ext/string/inflections'
4
+
5
+ require 'resque-serial/version'
6
+ require 'resque-serial/extender'
7
+ require 'resque-serial/lockable'
8
+ require 'resque-serial/sync_job'
9
+ require 'resque-serial/async_job'
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'resque-serial/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "resque-serial"
8
+ gem.version = Resque::Serial::VERSION
9
+ gem.authors = ["Max Kazarin"]
10
+ gem.email = ["maxkazargm@gmail.com"]
11
+ gem.description = %q{Resque serial jobs}
12
+ gem.summary = %q{Resque serial jobs}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency('activesupport')
21
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-serial
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Max Kazarin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Resque serial jobs
28
+ email:
29
+ - maxkazargm@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - Gemfile
36
+ - LICENSE.txt
37
+ - README.md
38
+ - Rakefile
39
+ - init.rb
40
+ - lib/resque-serial.rb
41
+ - lib/resque-serial/async_job.rb
42
+ - lib/resque-serial/extender.rb
43
+ - lib/resque-serial/lockable.rb
44
+ - lib/resque-serial/server.rb
45
+ - lib/resque-serial/server/views/serial.erb
46
+ - lib/resque-serial/server/views/serials.erb
47
+ - lib/resque-serial/sync_job.rb
48
+ - lib/resque-serial/version.rb
49
+ - resque-serial.gemspec
50
+ homepage: ''
51
+ licenses: []
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.0.3
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Resque serial jobs
73
+ test_files: []
74
+ has_rdoc: