sidekiq-job_monitor 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e3c554ad234720879c74de12d2cf115cb1708012
4
+ data.tar.gz: cc071f88a7897b1b81e0bd4969b0100b3e046544
5
+ SHA512:
6
+ metadata.gz: f2a7a616be721bb98f986d7e730a8417fc4aa4a92807dd2b9dcf4825299f18b68f47d822d55cd76aed0604821e8e8db9861e80a4e0fb5f5bd92aaf3a3e5b6d24
7
+ data.tar.gz: f20aed8dca5422ad05d0b541ba158b214b4c55e9dc9b0333cfc67cad8fddbf86ddd2186e4a391b458167a1f4fa9337a8cee9a75fdf5917176a54335999e9d5e0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 vala
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Sidekiq::JobMonitor'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
@@ -0,0 +1,36 @@
1
+ @Sidekiq ?= {}
2
+
3
+ class Sidekiq.JobMonitor
4
+ constructor: (markup, options = {}) ->
5
+ @$el = $(markup)
6
+ @monitorURL = @$el.data('monitor-url')
7
+ setTimeout(@monitorProgress, 1000)
8
+ $('body').trigger('start', [this])
9
+ options.onStart?(@$el)
10
+
11
+ monitorProgress: =>
12
+ $.getJSON(@monitorURL)
13
+ .done(@onMonitorProgressData)
14
+ .fail(@jobFailed)
15
+
16
+ onMonitorProgressData: (data) =>
17
+ if data.state is 'complete'
18
+ @jobComplete(data)
19
+ else
20
+ setTimeout(@monitorProgress, 1000)
21
+
22
+ jobComplete: (data) ->
23
+ console.log "trigger completion : ", this, data
24
+ @$el.trigger('complete', [this, data])
25
+
26
+ jobFailed: =>
27
+ @$el.trigger('failed', [this])
28
+
29
+ trigger: (eventName, args...) ->
30
+ args = [this].concat(args)
31
+ @$el.trigger(eventName, args)
32
+
33
+ $.fn.sidekiqJobMonitor = (options = {}) ->
34
+ @each (i, el) ->
35
+ $(el).on 'ajax:success', (e, response) ->
36
+ new Sidekiq.JobMonitor(response, options)
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,6 @@
1
+ module Sidekiq
2
+ module JobMonitor
3
+ class ApplicationController < ActionController::Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,22 @@
1
+ module Sidekiq
2
+ module JobMonitor
3
+ class JobProgressController < ApplicationController
4
+ def show
5
+ job = Sidekiq::JobMonitor::Job.find(params[:id])
6
+ # Fail and return 404 if no job was found
7
+ return head 404 unless job
8
+ # The only intersting data from the job is its state,
9
+ # arguments shouldn't be returned to the client
10
+ data = { id: job.attributes['jid'], state: job.state }
11
+ # Allow the worker class to hook into data serialization by calling
12
+ # the #monitoring_data instance method with the job arguments and state
13
+ # as arguments
14
+ if (monitoring_data = job.monitoring_data)
15
+ data.merge!(monitoring_data)
16
+ end
17
+
18
+ render json: data
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,6 @@
1
+ module Sidekiq
2
+ module JobMonitor
3
+ module ApplicationHelper
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Sidekiq::JobMonitor</title>
5
+ <%= stylesheet_link_tag "sidekiq/job_monitor/application", media: "all" %>
6
+ <%= javascript_include_tag "sidekiq/job_monitor/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Sidekiq::JobMonitor::Engine.routes.draw do
2
+ resources :job_progress, only: [:show]
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'sidekiq/api'
2
+
3
+ require 'sidekiq/job_monitor/job'
4
+ require 'sidekiq/job_monitor/middleware'
5
+
6
+ require 'sidekiq/job_monitor/engine'
7
+
8
+ module Sidekiq
9
+ module JobMonitor
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ module Sidekiq
2
+ module JobMonitor
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace Sidekiq::JobMonitor
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,84 @@
1
+ module Sidekiq
2
+ module JobMonitor
3
+ class Job
4
+ attr_reader :attributes
5
+
6
+ def initialize(attributes)
7
+ @attributes = attributes.as_json(only: %w(class jid args state))
8
+ end
9
+
10
+ def save
11
+ Sidekiq.redis do |conn|
12
+ conn.set(store_key, serialize)
13
+ # Expire key in 1 hours to avoid garbage keys
14
+ conn.expire(store_key, 3_600) if conn.ttl(store_key) == -1
15
+ end
16
+ end
17
+
18
+ def store_key
19
+ @store_key ||= self.class.store_key_for(attributes['jid'])
20
+ end
21
+
22
+ def serialize
23
+ attributes.to_json
24
+ end
25
+
26
+ def state
27
+ @state ||= attributes['state'] || 'pending'
28
+ end
29
+
30
+ # Add #processing!, #complete! and #failed! methods
31
+ %w(processing complete failed).each do |key|
32
+ define_method(:"#{ key }!") do
33
+ self.state = key
34
+ save
35
+ end
36
+ end
37
+
38
+ def state=(value)
39
+ @state = attributes['state'] = value
40
+ end
41
+
42
+ def as_json(*args)
43
+ attributes.as_json(*args)
44
+ end
45
+
46
+ def worker
47
+ attributes['class'].constantize
48
+ end
49
+
50
+ def monitoring_data
51
+ worker_instance = worker.new
52
+
53
+ if worker_instance.respond_to?(:monitoring_data)
54
+ worker_instance.monitoring_data(*attributes['args'], state)
55
+ end
56
+ end
57
+
58
+ class << self
59
+ def find(jid)
60
+ find_in_queues(jid) || find_in_previous(jid)
61
+ end
62
+
63
+ def store_key_for(jid)
64
+ ['sidekiq-job_monitor', jid].join(':')
65
+ end
66
+
67
+ private
68
+
69
+ def find_in_queues(jid)
70
+ job = Sidekiq::Queue.new.find_job(jid)
71
+ new(job.item) if job
72
+ end
73
+
74
+ def find_in_previous(jid)
75
+ data = Sidekiq.redis do |conn|
76
+ conn.get(store_key_for(jid))
77
+ end
78
+
79
+ new(JSON.parse(data)) if data
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,27 @@
1
+ module Sidekiq
2
+ module JobMonitor
3
+ class Middleware
4
+ # Wrap Sidekiq default job execution with completion and failure handling
5
+ # to make previous jobs tracking easy
6
+ def call(worker, msg, queue)
7
+ job = Sidekiq::JobMonitor::Job.new(msg)
8
+
9
+ job.processing!
10
+
11
+ begin
12
+ yield
13
+ job.complete!
14
+ rescue
15
+ job.failed!
16
+ raise
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ Sidekiq.configure_server do |config|
24
+ config.server_middleware do |chain|
25
+ chain.add Sidekiq::JobMonitor::Middleware
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ module Sidekiq
2
+ module JobMonitor
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :sidekiq_job_monitor do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-job_monitor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - vala
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sidekiq
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ description: Monitor currently running Sidekiq jobs to give user feedback
42
+ email:
43
+ - vala@glyph.fr
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - Rakefile
50
+ - app/assets/javascripts/sidekiq-job_monitor.coffee
51
+ - app/assets/javascripts/sidekiq_job_monitor/application.js
52
+ - app/assets/stylesheets/sidekiq_job_monitor/application.css
53
+ - app/controllers/sidekiq/job_monitor/application_controller.rb
54
+ - app/controllers/sidekiq/job_monitor/job_progress_controller.rb
55
+ - app/helpers/sidekiq/job_monitor/application_helper.rb
56
+ - app/views/layouts/sidekiq/job_monitor/application.html.erb
57
+ - config/routes.rb
58
+ - lib/sidekiq/job_monitor.rb
59
+ - lib/sidekiq/job_monitor/engine.rb
60
+ - lib/sidekiq/job_monitor/job.rb
61
+ - lib/sidekiq/job_monitor/middleware.rb
62
+ - lib/sidekiq/job_monitor/version.rb
63
+ - lib/tasks/sidekiq_job_monitor_tasks.rake
64
+ homepage: https://github.com/glyph-fr/sidekiq-job_monitor
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.5.1
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Monitor currently running Sidekiq jobs to give user feedback
88
+ test_files: []