progress_job 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f2075478f5997a10b069908397644e72d9e0f068
|
4
|
+
data.tar.gz: e1769c55e457629231b824c4536c442b15c1660a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: beac40fe9872df8871d394a866083cb820736d0ed89ac69e70a276a37463519708f5961db0c7b469ec6ce8956ece435d2580468ebd5cf16878d1b7c0538ca5c9
|
7
|
+
data.tar.gz: 36270aee384016cc3555f94ec2868108ed6c009f1af4e0873dab7b8029f41dc531068d37fe80e0ee5c416c17cd2e4b269e621c0ed9198fd2ab7849f22428be24
|
data/README.md
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
# ProgressJob
|
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
5
|
## Installation
|
6
6
|
|
7
|
+
You need to have https://github.com/collectiveidea/delayed_job in you gem file
|
8
|
+
|
7
9
|
Add this line to your application's Gemfile:
|
8
10
|
|
9
11
|
gem 'progress_job'
|
@@ -12,13 +14,34 @@ And then execute:
|
|
12
14
|
|
13
15
|
$ bundle
|
14
16
|
|
15
|
-
|
17
|
+
Run generator (run delayed job generators first!)
|
16
18
|
|
17
|
-
$
|
19
|
+
$ rake progress_job:install
|
18
20
|
|
19
21
|
## Usage
|
20
22
|
|
21
|
-
|
23
|
+
Create a new class that extends ProgressJob::Base
|
24
|
+
|
25
|
+
class NewJob < ProgressJob::Base
|
26
|
+
|
27
|
+
def perform
|
28
|
+
10.times do |i|
|
29
|
+
sleep(1.seconds)
|
30
|
+
update_progress(step: 10)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
Inside perform method you can use 'update_progress(step)' method to update the job progress.
|
37
|
+
|
38
|
+
To create a new job use Delayed job enqueue method, and pass the progress_max value
|
39
|
+
|
40
|
+
job = Delayed::Job.enqueue NewJob.new(100)
|
41
|
+
|
42
|
+
There is also a controller which returns the delayed job with calculated percentage
|
43
|
+
|
44
|
+
GET 'progress-jobs/:job_id/'
|
22
45
|
|
23
46
|
## Contributing
|
24
47
|
|
@@ -1,13 +1,10 @@
|
|
1
1
|
module ProgressJob
|
2
2
|
class ProgressController < ActionController::Base
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
percentage = current_job.current_progress / current_job.max_progress.to_f * 100
|
9
|
-
@current_job[:percentage] = percentage
|
10
|
-
render json: @current_job
|
4
|
+
def show
|
5
|
+
@delayed_job = Delayed::Job.find(params[:job_id])
|
6
|
+
percentage = !@delayed_job.progress_max.zero? ? @delayed_job.progress_current / @delayed_job.progress_max.to_f * 100 : 0
|
7
|
+
render json: @delayed_job.attributes.merge!(percentage: percentage).to_json
|
11
8
|
end
|
12
9
|
|
13
10
|
end
|
data/config/routes.rb
CHANGED
@@ -2,8 +2,8 @@ class AddProgressToDelayedJobs < ActiveRecord::Migration
|
|
2
2
|
def change
|
3
3
|
change_table :delayed_jobs do |t|
|
4
4
|
t.string :progress_stage, required: true
|
5
|
-
t.
|
6
|
-
t.
|
5
|
+
t.integer :progress_current, required: true, default: 0
|
6
|
+
t.integer :progress_max, required: true, default: 0
|
7
7
|
end
|
8
8
|
|
9
9
|
end
|
data/lib/progress_job/base.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
module ProgressJob
|
2
2
|
class Base
|
3
|
-
def initialize(
|
4
|
-
@
|
3
|
+
def initialize(progress_max)
|
4
|
+
@progress_max = progress_max
|
5
5
|
super()
|
6
6
|
end
|
7
7
|
|
8
8
|
def before(job)
|
9
9
|
@job = job
|
10
|
-
@job.update_column(:
|
10
|
+
@job.update_column(:progress_max, @progress_max)
|
11
11
|
end
|
12
12
|
|
13
13
|
def update_progress(step: 1)
|
14
|
-
@job.update_column(:
|
14
|
+
@job.update_column(:progress_current, @job.progress_current + step)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
data/lib/progress_job/version.rb
CHANGED