Skip to content

Commit

Permalink
added start_batch!
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemarsian committed Jan 9, 2016
1 parent cf4c610 commit 0bfb8b2
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
22 changes: 21 additions & 1 deletion .pryrc
@@ -1 +1,21 @@
require './spec/active_record_helper'
require './spec/active_record_helper'

class SimpleModel < ActiveRecord::Base
include Serially

self.table_name = 'simple_items'

serially do
task :model_step1 do |instance|
true
end
task :model_step2 do |instance|
["OK", 'step 2 finished ok']
end
task :model_step3
end

def model_step3
[false, 'step 3 failed']
end
end
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -70,8 +70,9 @@ class Post < ActiveRecord::Base
end
end
```
### Start for Instance

After creating a Post, you can run `post.serially.start!` to schedule your Post tasks to run serially. They will run one after the other in the scope of the same `Serially::Job`
After creating an instance of a Post, you can run `post.serially.start!` to schedule your Post tasks to run serially. They will run one after the other in the scope of the same `Serially::Job`
in the default `serially` queue.
An example run:
```ruby
Expand All @@ -89,6 +90,11 @@ Post 1 not published - bibliography is missing
Post 2 reviewed by staff
Post 2 not published - bibliography is missing
```
### Start for Batch
If you want to schedule serially tasks for multiple instances, you can do it in a single call:
```ruby
Post.start_batch!([post1.id, post2.id, post3.id])
```

### Task Return Values

Expand Down
7 changes: 7 additions & 0 deletions lib/serially/serially.rb
Expand Up @@ -35,6 +35,13 @@ def serially(*args, &block)
@serially
end

def start_batch!(instance_ids)
queue = Serially::TaskManager[self].queue
instance_ids.each do |instance_id|
Serially::Job.enqueue(self, instance_id, queue)
end
end

def is_active_record?
self < ActiveRecord::Base
end
Expand Down
2 changes: 1 addition & 1 deletion lib/serially/version.rb
@@ -1,3 +1,3 @@
module Serially
VERSION = "0.2.0"
VERSION = "0.3.0"
end
30 changes: 30 additions & 0 deletions spec/unit/class_methods_spec.rb
Expand Up @@ -55,4 +55,34 @@
end
end
end

context '::start_batch!' do
context 'for simple class' do
let(:ids) { %w(key1 key2 key3) }
it 'should be available on any class that includes Serially' do
SimpleClass.should respond_to(:start_batch!)
SimpleModel.should respond_to(:start_batch!)
end
it 'should schedule jobs as the number of ids passed' do
SimpleInstanceId.start_batch!(ids)

resque_jobs = Resque.peek(Serially::Job.queue, 0, 10)
resque_jobs.count.should == ids.count
resque_jobs.first['class'].should == Serially::Job.to_s
resque_jobs.map{|h| h['args'][1]}.should include(*ids)
end
end
context 'for model class' do
let(:ids) { [SimpleModel.create(title: 'A').id, SimpleModel.create(title: 'B').id, SimpleModel.create(title: 'C').id] }
before(:all) { Resque.inline = true }
after(:all) { Resque.inline = false }
it 'should create task runs as expected' do
SimpleModel.start_batch!(ids)
Serially::TaskRun.count.should == 9 # 3 instances, 3 task runs for each
Serially::TaskRun.finished_error.count.should == 3
Serially::TaskRun.finished_ok.count.should == 6
Serially::TaskRun.where(item_id: ids.first).count.should == 3
end
end
end
end

0 comments on commit 0bfb8b2

Please sign in to comment.