sidekiq-job_monitor 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b1d3e4b21e888c43304c1353a7cd44d9bd9fb7f
4
- data.tar.gz: a4b8740f91c16aeb23fccf94f0c47c980857bb28
3
+ metadata.gz: 319d954ebcdf311eb5fa65d407ab02e466512a1e
4
+ data.tar.gz: 8e5f2abfba3189ec211b5fe2ff0d1dac61b490e0
5
5
  SHA512:
6
- metadata.gz: 3549b4f766d1742c3222f316797c5ba60ac519579beb2b21a05ab752114fce74450ebca64284feeacd5c6d87666fc7415eb9992a016fb88a7a858592f5e22deb
7
- data.tar.gz: b7810de4b7c3030f9feff07e38c28bc6ba68daddb2d3d993c819737ee8d3423ac781cc097c1a47fc5ca3f218f93c69a13dd0cd257ea936f0d1b6335c20e98120
6
+ metadata.gz: 541469c7e9534e2a55214d4f6066b95a01af869feede6bf6230b0a580821fcb6910e03aa64f1bcdafd09f288bd5fb1bd9a99d5dbe0865c11bad756de4c6e655c
7
+ data.tar.gz: e59303b4780c868f54e2b5ad0199bb5923ab2f22ccd7d1af7e6e477cda3081fdb6b06ce53855fc993dddfc4d9e5de0d410376578d64c5c1be287b747a36633a5
data/README.md CHANGED
@@ -29,6 +29,12 @@ Mount the engine in your routes.rb :
29
29
  mount Sidekiq::JobMonitor::Engine => '/job-monitor', as: :job_monitor
30
30
  ```
31
31
 
32
+ Then load the js library by adding the following to your `application.js` :
33
+
34
+ ```javascript
35
+ //= require sidekiq-job_monitor
36
+ ```
37
+
32
38
  ## Usage
33
39
 
34
40
  The supported workflow for the gem is the following :
@@ -5,8 +5,9 @@ class Sidekiq.JobMonitor
5
5
  @$el = $(markup)
6
6
  @monitorURL = @$el.data('monitor-url')
7
7
  @monitor()
8
- options.onStart?(@$el)
8
+ options.onStart?(this)
9
9
  @$el.on('stop', @stopMonitoring)
10
+ @$el.on('cancel', @cancelJob)
10
11
  $('body').trigger('start', [this])
11
12
 
12
13
  monitorProgress: =>
@@ -15,16 +16,15 @@ class Sidekiq.JobMonitor
15
16
  .fail(@jobFailed)
16
17
 
17
18
  onMonitorProgressData: (data) =>
18
- if data.state is 'complete'
19
- @jobComplete(data)
20
- else
21
- @monitor()
19
+ switch data.state
20
+ when 'complete' then @jobComplete(data)
21
+ when 'failed' then @jobFailed(data)
22
+ else @monitor()
22
23
 
23
24
  monitor: ->
24
25
  @monitorTimer = setTimeout(@monitorProgress, 1000)
25
26
 
26
27
  jobComplete: (data) ->
27
- console.log "trigger completion : ", this, data
28
28
  @$el.trigger('complete', [this, data])
29
29
 
30
30
  jobFailed: =>
@@ -33,6 +33,23 @@ class Sidekiq.JobMonitor
33
33
  stopMonitoring: =>
34
34
  clearTimeout(@monitorTimer) if @monitorTimer
35
35
 
36
+ # Canceling job will only work for queued jobs, and not for running ones
37
+ #
38
+ # If the job is running, cancelation will be ignored and no error raised
39
+ #
40
+ cancelJob: =>
41
+ $.get(@remoteURL('cancel')).done(@jobCanceled)
42
+
43
+ jobCanceled: =>
44
+ @$el.trigger('canceled', [this])
45
+
46
+ remoteURL: (action = null) ->
47
+ return @monitorURL unless action
48
+ urlParts = @monitorURL.split('?')
49
+ url = [urlParts[0], action].join('/')
50
+ queryParams = urlParts[1]
51
+ if queryParams then [url, queryParams].join('?') else url
52
+
36
53
  $.fn.sidekiqJobMonitor = (options = {}) ->
37
54
  @each (i, el) ->
38
55
  $(el).on 'ajax:success', (e, response) ->
@@ -1,21 +1,37 @@
1
1
  module Sidekiq
2
2
  module JobMonitor
3
3
  class JobProgressController < ApplicationController
4
+ before_action :load_job
5
+
4
6
  def show
5
- job = Sidekiq::JobMonitor::Job.find(params[:id])
7
+ render json: data
8
+ end
9
+
10
+ def cancel
11
+ @job.cancel
12
+ load_job # Reload job to update state
13
+ render json: data
14
+ end
15
+
16
+ private
17
+
18
+ def load_job
19
+ @job = Sidekiq::JobMonitor::Job.find(params[:id])
6
20
  # Fail and return 404 if no job was found
7
- return head 404 unless job
21
+ head 404 unless @job
22
+ end
23
+
24
+ def data
8
25
  # The only intersting data from the job is its state,
9
26
  # 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)
27
+ { id: @job.attributes['jid'], state: @job.state }.tap do |data|
28
+ # Allow the worker class to hook into data serialization by calling
29
+ # the #monitoring_data instance method with the job arguments and
30
+ # state as arguments
31
+ if (monitoring_data = @job.monitoring_data)
32
+ data.merge!(monitoring_data)
33
+ end
16
34
  end
17
-
18
- render json: data
19
35
  end
20
36
  end
21
37
  end
data/config/routes.rb CHANGED
@@ -1,3 +1,7 @@
1
1
  Sidekiq::JobMonitor::Engine.routes.draw do
2
- resources :job_progress, only: [:show]
2
+ resources :job_progress, only: [:show] do
3
+ member do
4
+ get 'cancel'
5
+ end
6
+ end
3
7
  end
@@ -1,10 +1,11 @@
1
1
  module Sidekiq
2
2
  module JobMonitor
3
3
  class Job
4
- attr_reader :attributes
4
+ attr_reader :attributes, :original_job
5
5
 
6
6
  def initialize(attributes)
7
7
  @attributes = attributes.as_json(only: %w(class jid args state))
8
+ @original_job = attributes[:original_job]
8
9
  end
9
10
 
10
11
  def save
@@ -49,12 +50,18 @@ module Sidekiq
49
50
 
50
51
  def monitoring_data
51
52
  worker_instance = worker.new
53
+ worker_instance.jid = attributes['jid']
52
54
 
53
55
  if worker_instance.respond_to?(:monitoring_data)
54
56
  worker_instance.monitoring_data(*attributes['args'], state)
55
57
  end
56
58
  end
57
59
 
60
+ def cancel
61
+ return unless original_job
62
+ original_job.delete
63
+ end
64
+
58
65
  class << self
59
66
  def find(jid)
60
67
  find_in_queues(jid) || find_in_previous(jid)
@@ -68,7 +75,10 @@ module Sidekiq
68
75
 
69
76
  def find_in_queues(jid)
70
77
  job = Sidekiq::Queue.new.find_job(jid)
71
- new(job.item) if job
78
+ return unless job
79
+
80
+ attributes = { original_job: job }.merge(job.item)
81
+ new(attributes)
72
82
  end
73
83
 
74
84
  def find_in_previous(jid)
@@ -1,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module JobMonitor
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-job_monitor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - vala
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-11 00:00:00.000000000 Z
11
+ date: 2016-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails