active_job_status 0.0.2 → 0.0.3

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: 5bacba651822dd7ae2550f7700b4553a05c586f9
4
- data.tar.gz: 7e27e7ad71b23346ca96bc5adcabf57dce04c619
3
+ metadata.gz: 0f5b9c6307f29a9c2f63d672cb9e3df1ebc8803e
4
+ data.tar.gz: 400d59fc1f9d4b8d850f43e3e1faf7a356784242
5
5
  SHA512:
6
- metadata.gz: 8111412adf2087716c7e6844b55e40f465d884ecdbad0f03ab54765a1bbdddc1ec5babb076ea74d490b6e8866cdbd6e041a0c364d938e63d422c60396d35298c
7
- data.tar.gz: 7b9ec2dcd014c181d9e2035b21b7ac9cf798f7dde6458fee093c0879547875afec56aa57c10a8f99a7e040cc421cbf6df10ddf986db01495c3b471fb1213b5cf
6
+ metadata.gz: ebde3b55fb024aa7663618ae10602e174ff548edb8917aa20af92ebd933147fb9faca42612556b727aa95f050e194c0db57f6732a5d7a7124db4fa21690cd721
7
+ data.tar.gz: 65b0f469ebb6c015cb5f7683e28b9c55fab8cdcc2fc1ffa1f0da70e84b34e5f6315c8e04118d995267623aac38845141a949e31260b9a7e7059c23926f0fefea
data/README.md CHANGED
@@ -4,7 +4,14 @@
4
4
 
5
5
  # ActiveJobStatus
6
6
 
7
- Uses Redis to provide simple job status information for ActiveJob.
7
+ Uses Redis to provide simple job status information for ActiveJob. This is a
8
+ work in progress! Version 0.1.0 will probably be the first usable version. Until
9
+ then please expect frequent breaking changes, chaos, etc (Jan. 2015).
10
+
11
+ This gem uses ActiveJob callbacks to set simple Redis values to track job status
12
+ and batches of jobs. To prevent it from taking over all of your Redis, both jobs
13
+ and batch tracking information expires in 72 hours. Currently you can set
14
+ batches to expire at a different interval, but not jobs.
8
15
 
9
16
  ## Installation
10
17
 
@@ -29,34 +36,49 @@ Have your jobs descend from TrackableJob instead of ActiveJob::Base
29
36
  class MyJob < TrackableJob
30
37
  end
31
38
 
32
- Check the status of a job using the ActiveJob job_id
39
+ Check the status of a job using the ActiveJob job_id. Status of a job will only
40
+ be available for 72 hours after the job is queued. For right now you can't
41
+ change that
33
42
 
34
43
  my_job = MyJob.perform_later
35
44
  ActiveJobStatus::JobStatus.get_status(job_id: my_job.job_id)
36
45
  # => :queued, :working, :complete
37
46
 
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).
47
+ Create job batches You an use any key you want (for example, you might use a
48
+ primary key or UUID from your database). If another batch with the same key
49
+ exists, its jobs will be overwritten with the supplied list.
40
50
 
41
51
  my_key = "230923asdlkj230923"
42
- my_batch = ActiveJobStatus::JobBatch.new(batch_key: my_key)
52
+ my_jobs = [my_first_job.job_id, my_second_job.job_id]
53
+ my_batch = ActiveJobStatus::JobBatch.new(batch_key: my_key, job_ids: my_jobs)
43
54
 
44
- If you'd like you can pass an initial array of ActiveJob job_ids:
55
+ Batches expire after 72 hours (259200 seconds).
56
+ You can change that by passing the initalizer an integer value (in seconds).
45
57
 
46
58
  my_key = "230923asdlkj230923"
47
59
  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)
60
+ my_batch = ActiveJobStatus::JobBatch.new(batch_key: my_key,
61
+ job_ids: my_jobs,
62
+ expire_in: 500000)
49
63
 
50
- You can easily add jobs to the batch
64
+ You can easily add jobs to the batch.
51
65
 
52
66
  new_jobs = [some_new_job.job_id, another_new_job.job_id]
53
67
  my_batch.add_jobs(job_ids: new_jobs)
54
68
 
55
- And you can ask the batch if all the jobs are completed or not
69
+ And you can ask the batch if all the jobs are completed or not.
56
70
 
57
71
  my_batch.completed?
58
72
  # => true, false
59
73
 
74
+ You can ask the batch for other bits of information.
75
+
76
+ batch.batch_key
77
+ # => "230923asdlkj230923"
78
+ batch.job_ids
79
+ # => ["b67af7a0-3ed2-4661-a2d5-ff6b6a254886", "6c0216b9-ea0c-4ee9-a3b2-501faa919a66"]
80
+ batch.expire_in
81
+ # => 259200
60
82
 
61
83
  ## Contributing
62
84
 
@@ -1,16 +1,22 @@
1
1
  module ActiveJobStatus
2
2
  class JobBatch
3
3
 
4
- def initialize(batch_key:, job_ids: [])
4
+ attr_reader :batch_key
5
+ attr_reader :job_ids
6
+ attr_reader :expire_in
7
+
8
+ def initialize(batch_key:, job_ids:, expire_in: 259200)
5
9
  @batch_key = batch_key
6
10
  @job_ids = job_ids
7
-
8
- add_jobs(job_ids: @job_ids)
11
+ @expire_in = expire_in
12
+ ActiveJobStatus.redis.del(@batch_key) # delete any old batches
13
+ ActiveJobStatus.redis.sadd(@batch_key, @job_ids)
14
+ ActiveJobStatus.redis.expire(@batch_key, @expire_in)
9
15
  end
10
16
 
11
- def add_jobs(job_ids: [])
12
- @job_ids = @job_ids | job_ids
13
- ActiveJobStatus.redis.sadd(@batch_key, @job_ids)
17
+ def add_jobs(job_ids:)
18
+ @job_ids = @job_ids + job_ids
19
+ ActiveJobStatus.redis.sadd(@batch_key, job_ids)
14
20
  end
15
21
 
16
22
  def completed?
@@ -20,6 +26,10 @@ module ActiveJobStatus
20
26
  end
21
27
  !job_statuses.any?
22
28
  end
29
+
30
+ def self.find(batch_key:)
31
+ ActiveJobStatus.redis.smembers(batch_key)
32
+ end
23
33
  end
24
34
  end
25
35
 
@@ -5,6 +5,7 @@ module ActiveJobStatus
5
5
 
6
6
  def self.enqueue(job_id:)
7
7
  ActiveJobStatus.redis.set(job_id, "queued")
8
+ ActiveJobStatus.redis.expire(job_id, 259200)
8
9
  end
9
10
 
10
11
  def self.update(job_id:, status:)
@@ -1,3 +1,3 @@
1
1
  module ActiveJobStatus
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -2,7 +2,7 @@ require "spec_helper"
2
2
 
3
3
  describe ActiveJobStatus::JobBatch do
4
4
 
5
- let!(:batch_key) { "mykey" }
5
+ let!(:batch_key) { Time.now }
6
6
 
7
7
  let!(:redis) { ActiveJobStatus.redis }
8
8
 
@@ -11,17 +11,19 @@ describe ActiveJobStatus::JobBatch do
11
11
  let!(:job3) { TrackableJob.perform_later }
12
12
  let!(:job4) { TrackableJob.perform_later }
13
13
 
14
- let!(:batch) { ActiveJobStatus::JobBatch.new(batch_key: batch_key,
15
- job_ids: [job1.job_id, job2.job_id]) }
14
+ let!(:first_jobs) { [job1.job_id, job2.job_id] }
15
+ let!(:addl_jobs) { [job3.job_id, job4.job_id] }
16
+ let!(:total_jobs) { first_jobs + addl_jobs }
16
17
 
17
- let!(:job_id_array) { [job1.job_id, job2.job_id, job3.job_id, job4.job_id] }
18
+ let!(:batch) { ActiveJobStatus::JobBatch.new(batch_key: batch_key,
19
+ job_ids: first_jobs) }
18
20
 
19
21
  describe "#initialize" do
20
22
  it "should create an object" do
21
23
  expect(batch).to be_an_instance_of ActiveJobStatus::JobBatch
22
24
  end
23
25
  it "should create a redis set" do
24
- [job1.job_id, job2.job_id].each do |job_id|
26
+ first_jobs.each do |job_id|
25
27
  expect(redis.smembers(batch_key)).to include job_id
26
28
  end
27
29
  end
@@ -29,28 +31,60 @@ describe ActiveJobStatus::JobBatch do
29
31
 
30
32
  describe "#add_jobs" do
31
33
  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
34
+ batch.add_jobs(job_ids: addl_jobs)
35
+ total_jobs.each do |job_id|
36
+ expect(ActiveJobStatus::JobBatch.find(batch_key: batch_key)).to \
37
+ include job_id
35
38
  end
36
39
  end
37
40
  end
38
41
 
39
42
  describe "#completed?" do
40
43
  it "should be false when jobs are queued" do
41
- update_redis(id_array: job_id_array, job_status: :queued)
44
+ update_redis(id_array: total_jobs, job_status: :queued)
42
45
  expect(batch.completed?).to be_falsey
43
46
  end
44
47
  it "should be false when jobs are working" do
45
- update_redis(id_array: job_id_array, job_status: :working)
48
+ update_redis(id_array: total_jobs, job_status: :working)
46
49
  expect(batch.completed?).to be_falsey
47
50
  end
48
51
  it "should be true when jobs are completed" do
49
- clear_redis(id_array: job_id_array)
52
+ clear_redis(id_array: total_jobs)
50
53
  expect(batch.completed?).to be_truthy
51
54
  end
52
55
  end
53
56
 
57
+ describe "::find" do
58
+ it "should return an array of jobs when a batch exists" do
59
+ expect(ActiveJobStatus::JobBatch.find(batch_key: batch_key)).to \
60
+ be_an_instance_of Array
61
+ end
62
+ it "should return the correct jobs" do
63
+ expect(ActiveJobStatus::JobBatch.find(batch_key: batch_key)).to \
64
+ eq first_jobs
65
+ end
66
+ it "should return nil when no batch exists" do
67
+ expect(ActiveJobStatus::JobBatch.find(batch_key: "45")).to eq []
68
+ end
69
+ end
70
+
71
+ describe "expiring job" do
72
+ it "should allow the expiration time to be set in seconds" do
73
+ expect(ActiveJobStatus::JobBatch.new(batch_key: "newkey",
74
+ job_ids: first_jobs,
75
+ expire_in: 200000)).to \
76
+ be_an_instance_of ActiveJobStatus::JobBatch
77
+ end
78
+ it "should expire" do
79
+ ActiveJobStatus::JobBatch.new(batch_key: "expiry",
80
+ job_ids: first_jobs,
81
+ expire_in: 1)
82
+ sleep 2
83
+ expect(ActiveJobStatus::JobBatch.find(batch_key: "expiry")).to be_empty
84
+
85
+ end
86
+ end
87
+
54
88
  ##### HELPERS
55
89
 
56
90
  def update_redis(id_array: [], job_status: :queued)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_job_status
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brad Johnson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-04 00:00:00.000000000 Z
11
+ date: 2015-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler