active_job_status 0.0.2

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: 5bacba651822dd7ae2550f7700b4553a05c586f9
4
+ data.tar.gz: 7e27e7ad71b23346ca96bc5adcabf57dce04c619
5
+ SHA512:
6
+ metadata.gz: 8111412adf2087716c7e6844b55e40f465d884ecdbad0f03ab54765a1bbdddc1ec5babb076ea74d490b6e8866cdbd6e041a0c364d938e63d422c60396d35298c
7
+ data.tar.gz: 7b9ec2dcd014c181d9e2035b21b7ac9cf798f7dde6458fee093c0879547875afec56aa57c10a8f99a7e040cc421cbf6df10ddf986db01495c3b471fb1213b5cf
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .DS_Store
16
+ *.swp
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.2.0
5
+ script: bundle exec rake
6
+ notifications:
7
+ email:
8
+ recipients:
9
+ - cdale77@gmail.com
10
+ on_failure: change
11
+ on_success: never
12
+ env:
13
+ global:
14
+ secure: MK+CWQzMGTGzDRGfwtkJoAySnjcmmbEF4Fl9DDRaUD0q/LGTEV3TrOo+/QAeT945Vbs+CRnSMdGmVdBsLLiGlU56SrLQs8cEI/m7KyDcMjHJfuSkV5hdoUxSGgvvDlhLAZHBhmBnDnuIB7s0ExCyyJGamPOoZbVNgLYpF0XnxsY=
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_job_status.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Brad Johnson
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,67 @@
1
+ [![Code Climate](https://codeclimate.com/github/cdale77/active_job_status/badges/gpa.svg)](https://codeclimate.com/github/cdale77/active_job_status)
2
+ [![Test Coverage](https://codeclimate.com/github/cdale77/active_job_status/badges/coverage.svg)](https://codeclimate.com/github/cdale77/active_job_status)
3
+ [![Build Status](https://travis-ci.org/cdale77/active_job_status.svg?branch=master)](https://travis-ci.org/cdale77/active_job_status)
4
+
5
+ # ActiveJobStatus
6
+
7
+ Uses Redis to provide simple job status information for ActiveJob.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'active_job_status'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install active_job_status
24
+
25
+ ## Usage
26
+
27
+ Have your jobs descend from TrackableJob instead of ActiveJob::Base
28
+
29
+ class MyJob < TrackableJob
30
+ end
31
+
32
+ Check the status of a job using the ActiveJob job_id
33
+
34
+ my_job = MyJob.perform_later
35
+ ActiveJobStatus::JobStatus.get_status(job_id: my_job.job_id)
36
+ # => :queued, :working, :complete
37
+
38
+ Add jobs to batches. You an use any key you want (for example, you might use a
39
+ primary key or UUID from your database).
40
+
41
+ my_key = "230923asdlkj230923"
42
+ my_batch = ActiveJobStatus::JobBatch.new(batch_key: my_key)
43
+
44
+ If you'd like you can pass an initial array of ActiveJob job_ids:
45
+
46
+ my_key = "230923asdlkj230923"
47
+ my_jobs = [my_first_job.job_id, my_second_job.job_id]
48
+ my_batch = ActiveJobStatus::JobBatch.new(batch_key: my_key, job_ids: my_jobs)
49
+
50
+ You can easily add jobs to the batch
51
+
52
+ new_jobs = [some_new_job.job_id, another_new_job.job_id]
53
+ my_batch.add_jobs(job_ids: new_jobs)
54
+
55
+ And you can ask the batch if all the jobs are completed or not
56
+
57
+ my_batch.completed?
58
+ # => true, false
59
+
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it ( https://github.com/[my-github-username]/active_job_status/fork )
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler/gem_tasks'
3
+
4
+ # Default directory to look in is `/specs`
5
+ # Run with `rake spec`
6
+ RSpec::Core::RakeTask.new(:spec) do |task|
7
+ task.rspec_opts = ['--color']
8
+ end
9
+
10
+ task :default => :spec
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_job_status/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_job_status"
8
+ spec.version = ActiveJobStatus::VERSION
9
+ spec.authors = ["Brad Johnson"]
10
+ spec.email = ["cdale77@gmail.com"]
11
+ spec.summary = "Job status and batches for ActiveJob"
12
+ spec.description = "Job status and batches for ActiveJob. Create trackable jobs, check their status, and batch them together."
13
+ spec.homepage = "https://github.com/cdale77/active_job_status"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4"
25
+ spec.add_development_dependency "mock_redis", "~>0.14"
26
+
27
+ spec.add_runtime_dependency "activejob", "~>4.2"
28
+ spec.add_runtime_dependency "redis", "~> 3.2"
29
+ end
@@ -0,0 +1,20 @@
1
+ require "active_job_status/trackable_job"
2
+ require "active_job_status/job_tracker"
3
+ require "active_job_status/job_status"
4
+ require "active_job_status/job_batch"
5
+ require "active_job_status/version"
6
+ require "redis"
7
+
8
+ module ActiveJobStatus
9
+
10
+ @@redis = Redis.new
11
+
12
+ def self.redis= (redis)
13
+ @@redis = redis
14
+ end
15
+
16
+ def self.redis
17
+ @@redis
18
+ end
19
+ end
20
+
@@ -0,0 +1,25 @@
1
+ module ActiveJobStatus
2
+ class JobBatch
3
+
4
+ def initialize(batch_key:, job_ids: [])
5
+ @batch_key = batch_key
6
+ @job_ids = job_ids
7
+
8
+ add_jobs(job_ids: @job_ids)
9
+ end
10
+
11
+ def add_jobs(job_ids: [])
12
+ @job_ids = @job_ids | job_ids
13
+ ActiveJobStatus.redis.sadd(@batch_key, @job_ids)
14
+ end
15
+
16
+ def completed?
17
+ job_statuses = []
18
+ @job_ids.each do |job_id|
19
+ job_statuses << ActiveJobStatus::JobStatus.get_status(job_id: job_id)
20
+ end
21
+ !job_statuses.any?
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,11 @@
1
+ module ActiveJobStatus
2
+ module JobStatus
3
+ # Provides a way to check on the status of a given job
4
+
5
+ def self.get_status(job_id:)
6
+ status = ActiveJobStatus.redis.get(job_id)
7
+ status ? status.to_sym : nil
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,18 @@
1
+ module ActiveJobStatus
2
+ module JobTracker
3
+ # Provides methods to CRUD job status records in Redis
4
+ require "redis"
5
+
6
+ def self.enqueue(job_id:)
7
+ ActiveJobStatus.redis.set(job_id, "queued")
8
+ end
9
+
10
+ def self.update(job_id:, status:)
11
+ ActiveJobStatus.redis.set(job_id, status.to_s)
12
+ end
13
+
14
+ def self.remove(job_id:)
15
+ ActiveJobStatus.redis.del(job_id)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ require "active_job"
2
+ class TrackableJob < ActiveJob::Base
3
+
4
+ before_enqueue { ActiveJobStatus::JobTracker.enqueue(job_id: @job_id) }
5
+
6
+ before_perform { ActiveJobStatus::JobTracker.update(job_id: @job_id, status: :working) }
7
+
8
+ after_perform { ActiveJobStatus::JobTracker.remove(job_id: @job_id) }
9
+ end
10
+
11
+
@@ -0,0 +1,3 @@
1
+ module ActiveJobStatus
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,68 @@
1
+ require "spec_helper"
2
+
3
+ describe ActiveJobStatus::JobBatch do
4
+
5
+ let!(:batch_key) { "mykey" }
6
+
7
+ let!(:redis) { ActiveJobStatus.redis }
8
+
9
+ let!(:job1) { TrackableJob.perform_later }
10
+ let!(:job2) { TrackableJob.perform_later }
11
+ let!(:job3) { TrackableJob.perform_later }
12
+ let!(:job4) { TrackableJob.perform_later }
13
+
14
+ let!(:batch) { ActiveJobStatus::JobBatch.new(batch_key: batch_key,
15
+ job_ids: [job1.job_id, job2.job_id]) }
16
+
17
+ let!(:job_id_array) { [job1.job_id, job2.job_id, job3.job_id, job4.job_id] }
18
+
19
+ describe "#initialize" do
20
+ it "should create an object" do
21
+ expect(batch).to be_an_instance_of ActiveJobStatus::JobBatch
22
+ end
23
+ it "should create a redis set" do
24
+ [job1.job_id, job2.job_id].each do |job_id|
25
+ expect(redis.smembers(batch_key)).to include job_id
26
+ end
27
+ end
28
+ end
29
+
30
+ describe "#add_jobs" do
31
+ it "should add jobs to the set" do
32
+ batch.add_jobs(job_ids: [job3.job_id, job4.job_id])
33
+ job_id_array.each do |job_id|
34
+ expect(redis.smembers(batch_key)).to include job_id
35
+ end
36
+ end
37
+ end
38
+
39
+ describe "#completed?" do
40
+ it "should be false when jobs are queued" do
41
+ update_redis(id_array: job_id_array, job_status: :queued)
42
+ expect(batch.completed?).to be_falsey
43
+ end
44
+ it "should be false when jobs are working" do
45
+ update_redis(id_array: job_id_array, job_status: :working)
46
+ expect(batch.completed?).to be_falsey
47
+ end
48
+ it "should be true when jobs are completed" do
49
+ clear_redis(id_array: job_id_array)
50
+ expect(batch.completed?).to be_truthy
51
+ end
52
+ end
53
+
54
+ ##### HELPERS
55
+
56
+ def update_redis(id_array: [], job_status: :queued)
57
+ id_array.each do |id|
58
+ redis.set(id, job_status.to_s)
59
+ end
60
+ end
61
+
62
+ def clear_redis(id_array: [])
63
+ id_array.each do |id|
64
+ redis.del(id)
65
+ end
66
+ end
67
+ end
68
+
@@ -0,0 +1,26 @@
1
+ require "spec_helper"
2
+
3
+ describe ActiveJobStatus::JobStatus do
4
+
5
+ describe "::get_status" do
6
+
7
+ describe "for a queued job" do
8
+ let(:job) { TrackableJob.new.enqueue }
9
+
10
+ it "should return :queued" do
11
+ expect(ActiveJobStatus::JobStatus.get_status(job_id: job.job_id)).to eq :queued
12
+ end
13
+ end
14
+
15
+ describe "for a complete job" do
16
+
17
+ let!(:job) { TrackableJob.perform_later }
18
+ sleep(10)
19
+ #clear_performed_jobs
20
+ it "should return :complete", pending: true do
21
+ expect(ActiveJobStatus::JobStatus.get_status(job_id: job.job_id)).to be_nil
22
+ end
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,29 @@
1
+ require "spec_helper"
2
+
3
+ describe ActiveJobStatus::JobTracker do
4
+
5
+ let!(:redis) { ActiveJobStatus.redis }
6
+ let(:job) { TrackableJob.new.enqueue }
7
+
8
+ describe "::enqueue" do
9
+ it "should enqueue a job" do
10
+ ActiveJobStatus::JobTracker.enqueue(job_id: job.job_id)
11
+ expect(redis.get(job.job_id)).to eq "queued"
12
+ end
13
+ end
14
+
15
+ describe "::update" do
16
+ it "should update a job status" do
17
+ ActiveJobStatus::JobTracker.update(job_id: job.job_id, status: :working)
18
+ expect(redis.get(job.job_id)).to eq "working"
19
+ end
20
+ end
21
+
22
+ describe "::remove" do
23
+ it "should remove the redis key" do
24
+ ActiveJobStatus::JobTracker.remove(job_id: job.job_id)
25
+ expect(redis.get(job.job_id)).to eq nil
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,8 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
3
+ require "active_job_status"
4
+ include ActiveJob::TestHelper
5
+ ActiveJob::Base.queue_adapter = :test
6
+ require "mock_redis"
7
+ ActiveJobStatus.redis = MockRedis.new
8
+
@@ -0,0 +1,21 @@
1
+ require "spec_helper"
2
+
3
+ describe TrackableJob do
4
+
5
+ describe "#initialize" do
6
+
7
+ let(:trackable_job) { TrackableJob.new }
8
+
9
+ it "should create an object" do
10
+ expect(trackable_job).to be_an_instance_of TrackableJob
11
+ end
12
+ end
13
+
14
+ describe 'queueing' do
15
+ let(:trackable_job) { TrackableJob.new.enqueue }
16
+
17
+ it "should have a job id" do
18
+ expect(trackable_job.job_id).to_not be_blank
19
+ end
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_job_status
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Brad Johnson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: codeclimate-test-reporter
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mock_redis
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.14'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.14'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activejob
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4.2'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: redis
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.2'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.2'
111
+ description: Job status and batches for ActiveJob. Create trackable jobs, check their
112
+ status, and batch them together.
113
+ email:
114
+ - cdale77@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - active_job_status.gemspec
126
+ - lib/active_job_status.rb
127
+ - lib/active_job_status/job_batch.rb
128
+ - lib/active_job_status/job_status.rb
129
+ - lib/active_job_status/job_tracker.rb
130
+ - lib/active_job_status/trackable_job.rb
131
+ - lib/active_job_status/version.rb
132
+ - spec/job_batch_spec.rb
133
+ - spec/job_status_spec.rb
134
+ - spec/job_tracker_spec.rb
135
+ - spec/spec_helper.rb
136
+ - spec/trackable_job_spec.rb
137
+ homepage: https://github.com/cdale77/active_job_status
138
+ licenses:
139
+ - MIT
140
+ metadata: {}
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 2.4.5
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: Job status and batches for ActiveJob
161
+ test_files:
162
+ - spec/job_batch_spec.rb
163
+ - spec/job_status_spec.rb
164
+ - spec/job_tracker_spec.rb
165
+ - spec/spec_helper.rb
166
+ - spec/trackable_job_spec.rb