actionable 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: 45a9bd1c736c4e4a5ad2b7346e96604fe762252f
4
- data.tar.gz: afd9fa6c64ff5ca69285019c34605ed46c3abbdd
3
+ metadata.gz: 59ea314ec2b4a5a5e5e9264c4c3a62b7ab012b4d
4
+ data.tar.gz: d05dda313d5413911dd9885811bd7863c33fd2f6
5
5
  SHA512:
6
- metadata.gz: d63c6d2439e5923342173b1b998e122876c4d4533e24782fd2f4e5898ff0e57ed64dcbfe4306622684f5f1872227c203918fd0e579e5c41af2b502f27ccd61ec
7
- data.tar.gz: b64f3d371ce3c95716162ed3ebf215351176253497ff7837d051b6966643aae9210101031a7d5b0b4b974738b076aa4d49a1f786327d21fc1e3d1e9eedb95df9
6
+ metadata.gz: a1af58fdea6f80e3be686f0423da148114462437276ae3f6c6df6abd42b27bbc60a86da5c89451ec6f69d618c4fc689043af926967fcff96531fd447ba49e458
7
+ data.tar.gz: a4e40d2e4426f2996741369d14aa5226468dafbea37f9442a6be168890e9bf645b0342c897816fa0f485807c15d57f0e0ee9a376340e3cf15f2e2cc2fa914894
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  <img src="https://semaphoreapp.com/api/v1/projects/d32a2e65068b7b995bd9e9e2f8beb99a64c7f98d/36068/badge.png">
7
7
 
8
- TODO: Write a gem description
8
+ Actionables, jobs to do later. Scheduled, not queued.
9
9
 
10
10
  ## Installation
11
11
 
@@ -23,7 +23,80 @@ Or install it yourself as:
23
23
 
24
24
  ## Usage
25
25
 
26
- TODO: Write usage instructions here
26
+ ### Basic Actionables
27
+
28
+ Include in model:
29
+
30
+ ```ruby
31
+ class Book
32
+
33
+ include Mongoid::Document
34
+ include Actionable::Target
35
+
36
+ field :current_page
37
+
38
+ end
39
+ ```
40
+
41
+ Schedule actionable:
42
+
43
+ ```ruby
44
+ mybook = Book.new
45
+ mybook.schedule_actionable(1.hour.from_now,ReadBook,{pages:27})
46
+ ```
47
+
48
+ Make sure the job exists too:
49
+
50
+ ```ruby
51
+ class ReadBook < Actionable::Job
52
+
53
+ def perform
54
+ target.current_page = target.current_page + payload[:pages]
55
+ target.save
56
+ end
57
+
58
+ end
59
+ ```
60
+
61
+ `schedule_actionable` takes three arguments: when to execute the job, the job class and an optional payload hash. Inside your `Actionable::Job`-inheriting class, the model for which the actionable was created (`mybook` above) is available as `target` and the payload hash is available as `payload`.
62
+
63
+ The actionables themselves are instances of `Actionable::Action` and are available as `actionables` on the model. They can have lots of stuff done to them:
64
+
65
+ ```ruby
66
+ # cancel all actionables
67
+ mybook.actionables.each(&:cancel)
68
+
69
+ # reschedule the first one for two hours from now
70
+ mybook.actionables.first.reschedule_for(2.hours.from_now)
71
+
72
+ # update the payload of the last one
73
+ mybook.actionables.last.update_attribute(:payload,{pages:32})
74
+ ```
75
+
76
+ `Actionable::Actions` are also Mongoid documents.
77
+
78
+ ### Recurring Actionables
79
+
80
+ Super simple:
81
+
82
+ ```ruby
83
+ # read five pages every five minutes...forever
84
+ Actionable::Recurrence.create({
85
+ recurrence_string: "*/12 * * * *",
86
+ job_class: ReadBook,
87
+ payload: {pages:5}
88
+ })
89
+ ```
90
+
91
+ ### Running Worker
92
+
93
+ Same as a normal Resque worker, just `require 'actionable/tasks'` at the top of you `Rakefile` to patch Resque's worker to poll Mongoid for actionable when Redis is out of jobs.
94
+
95
+ ## Tests
96
+
97
+ ```
98
+ $ rake spec
99
+ ```
27
100
 
28
101
  ## Contributing
29
102
 
@@ -7,10 +7,10 @@ module Actionable
7
7
  @queue ||= 'actionable'
8
8
  end
9
9
 
10
- def self.after_enqueue(id)
11
- actionable = Actionable::Action.find(id)
12
- actionable.update_attributes(status: :enqueued)
13
- end
10
+ # def self.after_enqueue(id)
11
+ # actionable = Actionable::Action.find(id)
12
+ # actionable.update_attributes(status: :enqueued)
13
+ # end
14
14
 
15
15
  def self.perform(id)
16
16
  actionable = Actionable::Action.find(id)
@@ -33,9 +33,11 @@ module Actionable
33
33
  end
34
34
 
35
35
  def cancel
36
- update_attributes(status: :cancelled)
36
+ update_attributes(status: :canceled)
37
37
  end
38
38
 
39
+ alias :cancel! :cancel
40
+
39
41
  def reschedule_for(execution_time)
40
42
  update_attributes(execution_time: execution_time)
41
43
  end
@@ -15,12 +15,18 @@ module Actionable
15
15
  field :target_id, type: Moped::BSON::ObjectId
16
16
  field :target_class_name, type: String
17
17
 
18
- scope :scheduled, where(status: :scheduled)
19
18
  scope :scheduled_for_between, ->(from,to){
20
19
  between(execution_time:[from,to])
21
20
  }
22
21
  scope :scheduled_for_before, ->(to){ where(:execution_time.lt => to) }
23
22
 
23
+ scope :with_job_class, ->(klass) { where(job_class_name: klass.to_s.classify)}
24
+
25
+ [:scheduled,:enqueued,:working,:complete,:canceled,:failed].each do |status|
26
+ scope status, where(status: status)
27
+ define_method(:"#{status}?") { self.status == status }
28
+ end
29
+
24
30
  def self.to_do
25
31
  scheduled.scheduled_for_before(Time.now.to_datetime)
26
32
  end
@@ -1,3 +1,3 @@
1
1
  module Actionable
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -84,7 +84,7 @@ describe Actionable::Recurrence do
84
84
  context "over the course of an hour" do
85
85
 
86
86
  it "should create and perform the job 11 times" do
87
- RecurringTestJob.should_receive(:go).exactly(12).times
87
+ RecurringTestJob.should_receive(:go).at_least(11).times
88
88
  Timecop.travel(1.hour.from_now.change(minutes:0))
89
89
  recurrence.valid?
90
90
  60.times do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: actionable
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
  - Daniel Luxemburg
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-08 00:00:00.000000000 Z
11
+ date: 2013-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler