expirable 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +76 -0
- data/lib/generators/expirable/setup/setup_generator.rb +13 -0
- data/lib/generators/expirable/setup/templates/clock.rb +14 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b8956d367c94c4aa41af394e953d3ed1373c9fab
|
4
|
+
data.tar.gz: 73a7a94a0ca5c1ce64280751eb783b68e5aa2231
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d4bd2017ed7aef995fca1ab9a0a44ca6db3a2cc834eb6432f336853be2fda9c95e392de844975e643d0d09f1296eb5b912198cd09f5fa66e79cee23ec3b162ad
|
7
|
+
data.tar.gz: 487c0bb9cd88a3943943d4d5349d3966ea3e51b5ab6d41d6a32b5839ee23c472dc50d6006ffa25ddb4b240d48305cf730dd77d87aa2f9f6120967caaa63bfbce
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
Expirable
|
2
|
+
=========
|
3
|
+
|
4
|
+
Expirable is a simple Rails mixin to handle deadlines in workflows.
|
5
|
+
It builds on [Delayed::Job](https://github.com/collectiveidea/delayed_job)
|
6
|
+
and [Clockwork](https://github.com/tomykaira/clockwork) to provide a painless approach
|
7
|
+
to handling time-based events.
|
8
|
+
|
9
|
+
|
10
|
+
Installation
|
11
|
+
------------
|
12
|
+
|
13
|
+
To install the Expirable and its dependencies, add `expirable` and an
|
14
|
+
appropriate Delayed::Jobs backend to your Gemfile:
|
15
|
+
```rb
|
16
|
+
gem 'delayed_job_active_record'
|
17
|
+
gem 'expirable'
|
18
|
+
```
|
19
|
+
|
20
|
+
Next, set up Delayed::Job if you are not already using it in your project:
|
21
|
+
```bash
|
22
|
+
rails generate delayed_job:active_record
|
23
|
+
rake db:migrate
|
24
|
+
```
|
25
|
+
|
26
|
+
Finally, set up Expirable itself by running `rails g expirable:setup`.
|
27
|
+
This will create a Clockwork configuration file called _lib/clock.rb_.
|
28
|
+
By default, it is configured to check deadlines at one minute past every hour.
|
29
|
+
See the [Clockwork manual](https://github.com/tomykaira/clockwork#clockwork---a-clock-process-to-replace-cron--)
|
30
|
+
for more information if you need to change this configuration.
|
31
|
+
|
32
|
+
|
33
|
+
Running Expirable
|
34
|
+
-----------------
|
35
|
+
|
36
|
+
To run Expirable, you will need to run a Clockwork worker and a Delayed::Job worker.
|
37
|
+
You can run the Clockwork worker by executing `bundle exec clockwork lib/clock.rb`
|
38
|
+
from the root of the Rails project, and Delayed::Job worker with `./bin/delayed_job run`.
|
39
|
+
|
40
|
+
You can also use [Foreman](http://blog.daviddollar.org/2011/05/06/introducing-foreman.html)
|
41
|
+
to manage these processes. For example, if you run your Rails application on Unicorn, then
|
42
|
+
you can create a `Procfile` with the following content and run your application with `foreman start`:
|
43
|
+
```conf
|
44
|
+
cron: bundle exec clockwork lib/clock.rb
|
45
|
+
web: bundle exec unicorn_rails
|
46
|
+
worker: ./bin/delayed_job run
|
47
|
+
```
|
48
|
+
|
49
|
+
|
50
|
+
Handling Deadlines
|
51
|
+
------------------
|
52
|
+
|
53
|
+
To be expirable, a class must do the following three things:
|
54
|
+
|
55
|
+
1. include `Expirable`;
|
56
|
+
2. define a `deadline_expired` instance method that will be invoked when the model's deadline expires; and
|
57
|
+
3. define a class method named `newly_expired` that returns a collection of model instances that have expired, but haven't yet received a `deadline_expired` message.
|
58
|
+
|
59
|
+
As an example, the following model uses [AASM](https://github.com/aasm/aasm) and Expirable to transition tasks to a failed state when their deadline is passed:
|
60
|
+
```rb
|
61
|
+
class ExampleTask < ActiveRecord::Base
|
62
|
+
include AASM
|
63
|
+
include Expirable
|
64
|
+
|
65
|
+
scope :newly_expired,
|
66
|
+
-> { in_progress.where('deadline < ?', DateTime.now) }
|
67
|
+
|
68
|
+
aasm do
|
69
|
+
# ... state and event definitions skipped ...
|
70
|
+
|
71
|
+
event :deadline_expired do
|
72
|
+
transitions from: :in_progress, to: :missed
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
```
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Expirable
|
2
|
+
module Generators
|
3
|
+
class SetupGenerator < Rails::Generators::Base
|
4
|
+
desc "Creates a basic clock.rb file to perform expiration hourly"
|
5
|
+
|
6
|
+
source_root File.expand_path("../templates", __FILE__)
|
7
|
+
|
8
|
+
def create_clock_config
|
9
|
+
copy_file "clock.rb", "lib/clock.rb"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'clockwork'
|
2
|
+
require './config/boot'
|
3
|
+
require './config/environment'
|
4
|
+
|
5
|
+
# Ensure all of the models are loaded. Expirable models are automatically
|
6
|
+
# registered when their class definitions are evaluated, so we need to
|
7
|
+
# load all of them when clockwork starts up.
|
8
|
+
Rails.application.eager_load!
|
9
|
+
|
10
|
+
module Clockwork
|
11
|
+
every 1.hour, 'Expirable.send_expired_events', at: '**:01' do
|
12
|
+
Expirable.send_expired_events
|
13
|
+
end
|
14
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: expirable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brendan MacDonell
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-03-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -58,7 +58,10 @@ executables: []
|
|
58
58
|
extensions: []
|
59
59
|
extra_rdoc_files: []
|
60
60
|
files:
|
61
|
+
- README.md
|
61
62
|
- lib/expirable.rb
|
63
|
+
- lib/generators/expirable/setup/setup_generator.rb
|
64
|
+
- lib/generators/expirable/setup/templates/clock.rb
|
62
65
|
homepage: http://rubygems.org/gems/expirable
|
63
66
|
licenses:
|
64
67
|
- MIT
|