ost-scheduler 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8db52f61fc13cfda5cab33f3e41308254faf5926
4
+ data.tar.gz: 0224270c8c9b60e2a2ea1f170e2add7edbd9bee2
5
+ SHA512:
6
+ metadata.gz: e8462a3d68aa3610e86177af40a8189e83b605864c6e566a0c6dccfb04d28f4fa0a88f8cbe2804c426b5c799dce35b482b216d614483ee574dfb2421541ab367
7
+ data.tar.gz: 6ce97559cd1748a0855785be8e80b4fab0c50c936e5fdbcaa99214e40e06b3b9b5c55bd2c8f9db15d269f49885bcffc9b5072bb366918fe72b4ab432ce5bed14
data/README.md ADDED
@@ -0,0 +1,91 @@
1
+ Ost Scheduler
2
+ ===
3
+
4
+ Ost-Scheduler is an extension to [Ost](http://github.com/soveran/ost) that adds support for queueing items in the future.
5
+
6
+ Description
7
+ -----------
8
+
9
+ Ost provides simple, lightweight background job functionality.
10
+
11
+ **Ost-Scheduler** makes it easy to enqueue object ids and process them with
12
+ workers.
13
+
14
+ Say you want to process video uploads. In your application you will
15
+ have something like this:
16
+
17
+ ``` ruby
18
+ Ost[:videos_to_process].push_at( Time.now + 60 , @video.id )
19
+ ```
20
+
21
+ Then, you will have a worker that will look like this:
22
+
23
+ ``` ruby
24
+ require "ost-scheduler"
25
+
26
+ Ost[:videos_to_process].each_delayed do |id|
27
+ # Do something with it!
28
+ end
29
+ ```
30
+
31
+ Usage
32
+ -----
33
+
34
+ Ost uses a lightweight Redis client called [Redic][redic]. To connect to
35
+ a Redis database, you will need to set an instance of `Redic`, with a URL
36
+ of the form `redis://:<passwd>@<host>:<port>/<db>`.
37
+
38
+ You can customize the connection by calling `Ost.redis=`:
39
+
40
+ ``` ruby
41
+ require "ost-scheduler"
42
+
43
+ Ost.redis = Redic.new("redis://127.0.0.1:6379")
44
+ ```
45
+
46
+ Then you only need to refer to a queue for it to pop into existence:
47
+
48
+ ``` ruby
49
+ require "ost-scheduler"
50
+
51
+ Ost.redis = Redic.new("redis://127.0.0.1:6379")
52
+
53
+ Ost[:event].push_at(@datetime, @feed.id)
54
+ ```
55
+
56
+ Ost defaults to a Redic connection to `redis://127.0.0.1:6379`. The example
57
+ above could be rewritten as:
58
+
59
+ ``` ruby
60
+ require "ost-scheduler"
61
+
62
+ Ost[:event].push_at(@datetime, @feed.id)
63
+ ```
64
+
65
+ A worker is a Ruby file with this basic code:
66
+
67
+ ``` ruby
68
+ require "ost-scheduler"
69
+
70
+ Ost[:rss_feeds].each_delayed do |id|
71
+ # ...
72
+ end
73
+ ```
74
+
75
+
76
+ Available methods
77
+ =================
78
+
79
+ `Ost[:example].push_at(datetime, item)`: add `item` to
80
+ the `:example` queue in a exactly `datetime`.
81
+
82
+ `Ost[:example].each_delayed { |item| ... }`: consume `item` from the `:example` queue. .
83
+
84
+ `Ost.stop`: halt processing for all queues.
85
+
86
+ `Ost[:example].stop`: halt processing for the `example` queue.
87
+
88
+ Installation
89
+ ------------
90
+
91
+ $ gem install ost-scheduler
@@ -0,0 +1,59 @@
1
+ require "ost"
2
+ module OstScheduler
3
+ TIME_PERIOD = ENV["TIME_PERIOD"] || "24"
4
+ TIMEOUT_SCHEDULER = ENV["OST_TIMEOUT"] || "1"
5
+ # /class Queue
6
+
7
+ def push_at(time,value)
8
+ score = time.to_i if time.class == Time
9
+ redis.call("ZADD", @key, score, value)
10
+ end
11
+
12
+
13
+ def each_delayed(&block)
14
+ loop do
15
+ now = Time.now.to_i
16
+ min_time = now - range_period
17
+ items = redis.call("ZRANGEBYSCORE",@key , min_time, now)
18
+ if !items.count.zero?
19
+ items.each do |item|
20
+ block.call(item)
21
+ end
22
+ redis.call("ZREMRANGEBYSCORE",@key , min_time, now)
23
+ end
24
+
25
+ sleep(TIMEOUT_SCHEDULER.to_i)
26
+
27
+ break if @stopping
28
+ end
29
+ end
30
+
31
+ def range_period
32
+ (TIME_PERIOD.to_i * 60 * 60)
33
+ end
34
+
35
+ def items
36
+ if redis.call("TYPE", @key) == "zset"
37
+ redis.call("ZRANGE", @key, 0, -1)
38
+ else
39
+ redis.call("LRANGE", @key, 0, -1)
40
+ end
41
+ end
42
+
43
+ def size
44
+ if redis.call("TYPE", @key) == "zset"
45
+ redis.call("ZCARD", @key)
46
+ else
47
+ redis.call("LLEN", @key)
48
+ end
49
+ end
50
+
51
+ # end
52
+ end
53
+
54
+ class Ost::Queue
55
+ remove_method :items
56
+ remove_method :size
57
+ include OstScheduler
58
+ end
59
+
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "ost-scheduler"
3
+ s.version = "0.1.1"
4
+ s.summary = "Redis based queues and workers in the future."
5
+ s.description = "Ost-scheduler lets you manage queues and workers with Redis."
6
+ s.authors = ["Leandro Rodriguez"]
7
+ s.email = ["learod17@gmail.com"]
8
+ s.homepage = "http://github.com/learod/ost-scheduler"
9
+ s.license = "MIT"
10
+
11
+ s.files = `git ls-files`.split("\n")
12
+
13
+ s.add_dependency "ost"
14
+ s.add_development_dependency "cutest"
15
+ end
16
+
data/test/access.rb ADDED
@@ -0,0 +1,22 @@
1
+ require_relative "helper"
2
+
3
+ scope do
4
+
5
+ test "access to delayed queued items" do
6
+ Ost[:events].push_at((Time.now + 60), 1)
7
+
8
+ assert_equal ["1"], Ost[:events].items
9
+ end
10
+
11
+ test "access to queue size" do
12
+ queue = Ost[:events]
13
+ assert_equal 0, queue.size
14
+
15
+ queue.push_at((Time.now), 1)
16
+ assert_equal 1, queue.size
17
+
18
+ queue.stop
19
+ queue.each_delayed { }
20
+ assert_equal 0, queue.size
21
+ end
22
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "cutest"
2
+ require "ost"
3
+ require_relative "../lib/ost-scheduler"
4
+
5
+ Ost.redis = Redic.new("redis://127.0.0.1:6379")
6
+
7
+ prepare do
8
+ Ost.redis.call("FLUSHALL")
9
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ost-scheduler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Leandro Rodriguez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ost
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: cutest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Ost-scheduler lets you manage queues and workers with Redis.
42
+ email:
43
+ - learod17@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - README.md
49
+ - lib/ost-scheduler.rb
50
+ - ost-scheduler.gemspec
51
+ - test/access.rb
52
+ - test/helper.rb
53
+ homepage: http://github.com/learod/ost-scheduler
54
+ licenses:
55
+ - MIT
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 2.2.2
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Redis based queues and workers in the future.
77
+ test_files: []