progress_job 0.0.3 → 0.0.4

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: 2cc32459446aa8a5db2648427b7491dd70a84a1e
4
- data.tar.gz: edfb357081f7a956b8b7b35977024ae2c07d4a14
3
+ metadata.gz: 95e2d424d4fd78ff6b26586fd61e50dd7059f3c3
4
+ data.tar.gz: c3584f41f002dbc03af02c789f4df633a153b773
5
5
  SHA512:
6
- metadata.gz: c7c85d6a4c09a49671a0f7e418690b5eb734ced0556cc9b02a41d20c4c029dd1445c20764e1075003ec2b2c54346d2d65496fc5eecceffa0c8ed511f494596a2
7
- data.tar.gz: d4d02f82ca6d8251db750102edceafe1c3ab7f3e80b8fbd8433fc922a4105d4c4737195d8447dff553cc0f101aec77cfd6775f1b0fb4ac5b7d58dfee0a6c19ce
6
+ metadata.gz: 8f030db9cf0f21d62e9ff37a38d660db4c6a63e2320efc6e4204ef4951917782538681dd8c39b71fffcb6ec336bea57e7eb4660111680350cc15e37c72fab6c7
7
+ data.tar.gz: f39df1dea7ee77e722674f748af57f6ad94c8ca02dfb72eaec886f9c84fb4a1dc18aa296012171581437c0e1e4702953ecbbf52988bf87bedccb95969bb4ec59
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  This gem add a couple of colums to delayed job table, and gives u a basic class for working with progress
4
4
 
5
+ ![progress_job](https://s3.amazonaws.com/infinum.web.production/repository_items/files/000/000/435/original/progress_job.gif?1414140810)
6
+
5
7
  ## Installation
6
8
 
7
9
  You need to have https://github.com/collectiveidea/delayed_job in you gem file
@@ -16,7 +18,7 @@ And then execute:
16
18
 
17
19
  Run generator (run delayed job generators first!)
18
20
 
19
- $ rake progress_job:install
21
+ $ rails generate progress_job:install
20
22
 
21
23
  ## Usage
22
24
 
@@ -25,50 +27,135 @@ Create a new class that extends ProgressJob::Base
25
27
  class NewJob < ProgressJob::Base
26
28
 
27
29
  def perform
28
- 10.times do |i|
29
- sleep(1.seconds)
30
- update_progress(step: 10)
31
- end
30
+ # some actions
32
31
  end
33
32
 
34
33
  end
35
34
 
36
- Inside perform method you can use 'update_progress(step)' method to update the job progress.
35
+ Inside perform method you can use:
36
+
37
+ update_progress(step: 10) # default step is 1
38
+ update_stage('name of stage')
39
+ update_stage_progress('name of stage', step: 11)
40
+ update_progress_max(progress_max)
41
+
42
+ methods to update the job progress.
43
+
37
44
 
38
45
  To create a new job use Delayed job enqueue method, and pass the progress_max value
39
46
 
40
- job = Delayed::Job.enqueue NewJob.new(100)
47
+ job = Delayed::Job.enqueue NewJob.new(progress_max: 100) # default progress_max is 100
41
48
 
42
49
  There is also a controller which returns the delayed job with calculated percentage
43
50
 
44
51
  GET 'progress-jobs/:job_id/'
45
52
 
46
- ## Ajax usage
53
+ ## Examples
54
+
55
+ ### Progress job class
56
+
57
+ class NewJob < ProgressJob::Base
58
+
59
+ def perform
60
+ handler = Handler.new
61
+
62
+ update_stage('Handling ports')
63
+ handler.handle_ports
47
64
 
48
- Example of ajax calls:
65
+ update_stage_progress('Handling cruise', step: 10)
66
+ handler.handle_cruise
49
67
 
50
- $('.button').click(function(){
68
+ update_stage_progress('Handling days', step: 10)
69
+ handler.handle_days
51
70
 
52
- var interval;
71
+ update_stage_progress('Handling pick up times', step: 10)
72
+ handler.handle_pick_up_times
73
+
74
+ update_stage_progress('Handling users', step: 10)
75
+ handler.handle_users
76
+
77
+ update_stage_progress('Handling item categories', step: 10)
78
+ handler.handle_item_categories
79
+
80
+ update_stage_progress('Handling items', step: 10)
81
+ handler.handle_items
82
+ handler.handle_other_items
83
+
84
+ update_stage_progress('Handling event types', step: 10)
85
+ handler.handle_event_types
86
+
87
+ update_stage_progress('Handling events', step: 10)
88
+ handler.handle_events
89
+ end
90
+
91
+ end
92
+
93
+ ### HAML
94
+
95
+ = simple_form_for :import, url: [:import], remote: true do |f|
96
+ .row
97
+ .col-xs-10
98
+ = f.input :file, as: :file
99
+ .col-xs-2
100
+ = f.button :submit, "Import", class: "btn btn-success"
101
+
102
+ %br
103
+ .well{style: "display:none"}
104
+ .row
105
+ .col-xs-12
106
+ .progress-status.text-primary
107
+ .row
108
+ .col-xs-12
109
+ .progress.progress-striped.active
110
+ .progress-bar
111
+ .text-primary
112
+ 0%
113
+
114
+ ### Ajax usage
115
+
116
+ Example of ajax call (this is a .html.haml remote: true response):
117
+
118
+ var interval;
119
+ $('.hermes-import .well').show();
120
+ interval = setInterval(function(){
53
121
  $.ajax({
54
- url: '/start',
122
+ url: '/progress-job/' + #{@job.id},
55
123
  success: function(job){
56
- interval = setInterval(function(){
57
- $.ajax({
58
- url: '/progress-jobs/' + job.id,
59
- success: function(job){
60
- $('.progress-bar').css('width', job.progress + '%').text(job.progress + '%')
61
- },
62
- error: function(){
63
- $('.progress-bar').css('width', '100%').text('100%')
64
- clearInterval(interval);
65
- }
66
- })
67
- },1000)
124
+ var stage, progress;
125
+
126
+ // If there are errors
127
+ if (job.last_error != null) {
128
+ $('.progress-status').addClass('text-danger').text(job.progress_stage);
129
+ $('.progress-bar').addClass('progress-bar-danger');
130
+ $('.progress').removeClass('active');
131
+ clearInterval(interval);
132
+ }
133
+
134
+ // Upload stage
135
+ if (job.progress_stage != null){
136
+ stage = job.progress_stage;
137
+ progress = job.progress_current / job.progress_max * 100;
138
+ } else {
139
+ progress = 0;
140
+ stage = 'Uploading file';
141
+ }
142
+
143
+ // In job stage
144
+ if (progress !== 0){
145
+ $('.progress-bar').css('width', progress + '%').text(progress + '%');
146
+ }
147
+
148
+ $('.progress-status').text(stage);
149
+ },
150
+ error: function(){
151
+ // Job is no loger in database which means it finished successfuly
152
+ $('.progress').removeClass('active');
153
+ $('.progress-bar').css('width', '100%').text('100%');
154
+ $('.progress-status').text('Successfully imported!');
155
+ clearInterval(interval);
68
156
  }
69
- });
70
- });
71
-
157
+ })
158
+ },100);
72
159
 
73
160
  ## Contributing
74
161
 
@@ -1,7 +1,6 @@
1
1
  module ProgressJob
2
2
  class Base
3
-
4
- def initialize(progress_max)
3
+ def initialize(progress_max: 100)
5
4
  @progress_max = progress_max
6
5
  end
7
6
 
@@ -24,8 +23,12 @@ module ProgressJob
24
23
  update_progress(step: step)
25
24
  end
26
25
 
26
+ def update_progress_max(progress_max)
27
+ @job.update_column(:progress_max, progress_max)
28
+ end
29
+
27
30
  def error(job, exception)
28
31
  job.update_column(:progress_stage, exception.message)
29
32
  end
30
33
  end
31
- end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module ProgressJob
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: progress_job
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stjepan Hadjic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-16 00:00:00.000000000 Z
11
+ date: 2014-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler