resque-timeframe 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+ ## 0.1.0 (2010-08-11)
2
+
3
+ * First release.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Dmitry Larkin (dmitry.larkin@gmail.com)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,68 @@
1
+ resque-timeframe
2
+ ===============
3
+
4
+ Resque Timeframe is a plugin for the [Resque][0] queueing system (http://github.com/defunkt/resque).
5
+ It allows to limit job execution in a timeframe. Sometimes we need to run huge tasks and this is not good at business time.
6
+ For example, archive operation could be run at any time excluding prime time. It possible to schedule tasks at night or weekends, but this is not flexible. Some times it is not possible to calculate how many archivation tasks you can schedule at the range of time. Using this plugin it possible to define timeframes (even possible to describe a week) and schedule tasks into queue. Resque would not execute jobs if it out of timeframe.
7
+
8
+ Resque Timeframe requires Resque 1.7.0.
9
+
10
+ Install
11
+ -------
12
+
13
+ sudo gem install resque-timeframe
14
+
15
+ To use
16
+ ------
17
+
18
+ Simpliest Timeframed job works like a regular job there each day of week allowed by default
19
+
20
+ class AllowedByDefaultTimeframeJob < Resque::Plugins::TimeframedJob
21
+ timeframe :default => true # deafult value
22
+
23
+ @queue = :timeframed_queue
24
+
25
+ def self.perform(args); end
26
+ end
27
+
28
+ To define timeframe possible to using week day name
29
+
30
+ class ArchiveJob < Resque::Plugins::TimeframedJob
31
+ timeframe :monday => false # do not allow execution at Monday
32
+ timeframe :tuesday => 14..22 # from 14 p.m. till 22 p.m.
33
+ timeframe :wednesday => 0..24 # full day
34
+ timeframe :thursday => '9:30'..'11:30' # 24-hours format able to be parsed like Time.parse("23:59")
35
+ timeframe :friday => '17:30'..'23:59'
36
+ timeframe :saturday => true # same as 0..24
37
+ timeframe :sunday => true
38
+
39
+ @queue = :timeframed_queue
40
+
41
+ def self.perform(args)
42
+ end
43
+ end
44
+
45
+ or like this
46
+
47
+ class ArchiveJob < Resque::Plugins::TimeframedJob
48
+ timeframe :default => false
49
+ timeframe [:saturday, :sunday] => true # allow execution only at weekends
50
+
51
+ @queue = :timeframed_queue
52
+
53
+ def self.perform(args)
54
+ end
55
+ end
56
+
57
+
58
+
59
+ Copyright
60
+ ---------
61
+ Copyright (c) 2010 Dmitry Larkin (at [Railsware][3])
62
+
63
+
64
+
65
+ [0]: http://github.com/defunkt/resque
66
+ [1]: http://help.github.com/forking/
67
+ [2]: http://github.com/dml/resque-timeframe/issues
68
+ [3]: http://railsware.com
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require 'rake'
2
+ require 'spec/rake/spectask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :spec
7
+
8
+ desc 'Generate documentation for the resque-timeframe plugin.'
9
+ Rake::RDocTask.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'resque_timeframe'
12
+ rdoc.options << '--line-numbers' << '--inline-source'
13
+ rdoc.rdoc_files.include('README*')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ desc "Run all specs in spec directory"
18
+ Spec::Rake::SpecTask.new(:spec) do |t|
19
+ t.spec_files = FileList['spec/**/*_spec.rb']
20
+ end
@@ -0,0 +1,2 @@
1
+ require 'resque'
2
+ require 'resque-timeframe/timeframe'
@@ -0,0 +1,64 @@
1
+ require 'time'
2
+
3
+ module Resque
4
+ module Plugins
5
+ module Timeframe
6
+
7
+ WEEK = [ :sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday ].freeze
8
+
9
+ def week
10
+ WEEK
11
+ end
12
+
13
+ def settings
14
+ @options ||= WEEK.inject({:default => true}) {|c,v| c.merge({v => true})}
15
+ end
16
+
17
+ def timeframe(options={})
18
+ settings.merge!(options)
19
+ end
20
+
21
+ def allowed_at?(weekday)
22
+ case settings[weekday]
23
+ when Range
24
+ range(settings[weekday]).include?(Time.now)
25
+ when FalseClass, TrueClass
26
+ settings[weekday] && settings[:default]
27
+ else
28
+ settings[:default]
29
+ end
30
+ end
31
+
32
+ def time_at(hr)
33
+ t = Time.now
34
+ if (0..23).include?(hr)
35
+ Time.mktime(t.year, t.month, t.day, hr, 0, 0)
36
+ else
37
+ Time.mktime(t.year, t.month, t.day + 1, 0, 0, 0)
38
+ end
39
+ end
40
+
41
+ def range(date_range)
42
+ case date_range.begin
43
+ when Integer
44
+ time_at(date_range.begin)..time_at(date_range.end)
45
+ when String
46
+ Time.parse(date_range.begin)..Time.parse(date_range.end)
47
+ when Time
48
+ date_range
49
+ else
50
+ time_at(0)..time_at(24)
51
+ end
52
+ end
53
+
54
+ def before_perform_timeframe(*args)
55
+ raise Resque::Job::DontPerform unless allowed_at?(week[Time.new.wday])
56
+ end
57
+
58
+ end
59
+
60
+ class TimeframedJob
61
+ extend Timeframe
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,9 @@
1
+ module ResqueTimeframe
2
+ module VERSION
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 0
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
data/spec/dump.rdb ADDED
@@ -0,0 +1 @@
1
+ REDIS0001�
@@ -0,0 +1,132 @@
1
+ # Redis configuration file example
2
+
3
+ # By default Redis does not run as a daemon. Use 'yes' if you need it.
4
+ # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
5
+ daemonize yes
6
+
7
+ # When run as a daemon, Redis write a pid file in /var/run/redis.pid by default.
8
+ # You can specify a custom pid file location here.
9
+ pidfile ./spec/redis-test.pid
10
+
11
+ # Accept connections on the specified port, default is 6379
12
+ port 9736
13
+
14
+ # If you want you can bind a single interface, if the bind option is not
15
+ # specified all the interfaces will listen for connections.
16
+ #
17
+ # bind 127.0.0.1
18
+
19
+ # Close the connection after a client is idle for N seconds (0 to disable)
20
+ timeout 300
21
+
22
+ # Save the DB on disk:
23
+ #
24
+ # save <seconds> <changes>
25
+ #
26
+ # Will save the DB if both the given number of seconds and the given
27
+ # number of write operations against the DB occurred.
28
+ #
29
+ # In the example below the behaviour will be to save:
30
+ # after 900 sec (15 min) if at least 1 key changed
31
+ # after 300 sec (5 min) if at least 10 keys changed
32
+ # after 60 sec if at least 10000 keys changed
33
+ save 900 1
34
+ save 300 10
35
+ save 60 10000
36
+
37
+ # The filename where to dump the DB
38
+ dbfilename dump.rdb
39
+
40
+ # For default save/load DB in/from the working directory
41
+ # Note that you must specify a directory not a file name.
42
+ dir ./spec/
43
+
44
+ # Set server verbosity to 'debug'
45
+ # it can be one of:
46
+ # debug (a lot of information, useful for development/testing)
47
+ # notice (moderately verbose, what you want in production probably)
48
+ # warning (only very important / critical messages are logged)
49
+ loglevel debug
50
+
51
+ # Specify the log file name. Also 'stdout' can be used to force
52
+ # the demon to log on the standard output. Note that if you use standard
53
+ # output for logging but daemonize, logs will be sent to /dev/null
54
+ logfile stdout
55
+
56
+ # Set the number of databases. The default database is DB 0, you can select
57
+ # a different one on a per-connection basis using SELECT <dbid> where
58
+ # dbid is a number between 0 and 'databases'-1
59
+ databases 16
60
+
61
+ ################################# REPLICATION #################################
62
+
63
+ # Master-Slave replication. Use slaveof to make a Redis instance a copy of
64
+ # another Redis server. Note that the configuration is local to the slave
65
+ # so for example it is possible to configure the slave to save the DB with a
66
+ # different interval, or to listen to another port, and so on.
67
+
68
+ # slaveof <masterip> <masterport>
69
+
70
+ ################################## SECURITY ###################################
71
+
72
+ # Require clients to issue AUTH <PASSWORD> before processing any other
73
+ # commands. This might be useful in environments in which you do not trust
74
+ # others with access to the host running redis-server.
75
+ #
76
+ # This should stay commented out for backward compatibility and because most
77
+ # people do not need auth (e.g. they run their own servers).
78
+
79
+ # requirepass foobared
80
+
81
+ ################################### LIMITS ####################################
82
+
83
+ # Set the max number of connected clients at the same time. By default there
84
+ # is no limit, and it's up to the number of file descriptors the Redis process
85
+ # is able to open. The special value '0' means no limts.
86
+ # Once the limit is reached Redis will close all the new connections sending
87
+ # an error 'max number of clients reached'.
88
+
89
+ # maxclients 128
90
+
91
+ # Don't use more memory than the specified amount of bytes.
92
+ # When the memory limit is reached Redis will try to remove keys with an
93
+ # EXPIRE set. It will try to start freeing keys that are going to expire
94
+ # in little time and preserve keys with a longer time to live.
95
+ # Redis will also try to remove objects from free lists if possible.
96
+ #
97
+ # If all this fails, Redis will start to reply with errors to commands
98
+ # that will use more memory, like SET, LPUSH, and so on, and will continue
99
+ # to reply to most read-only commands like GET.
100
+ #
101
+ # WARNING: maxmemory can be a good idea mainly if you want to use Redis as a
102
+ # 'state' server or cache, not as a real DB. When Redis is used as a real
103
+ # database the memory usage will grow over the weeks, it will be obvious if
104
+ # it is going to use too much memory in the long run, and you'll have the time
105
+ # to upgrade. With maxmemory after the limit is reached you'll start to get
106
+ # errors for write operations, and this may even lead to DB inconsistency.
107
+
108
+ # maxmemory <bytes>
109
+
110
+ ############################### ADVANCED CONFIG ###############################
111
+
112
+ # Glue small output buffers together in order to send small replies in a
113
+ # single TCP packet. Uses a bit more CPU but most of the times it is a win
114
+ # in terms of number of queries per second. Use 'yes' if unsure.
115
+ glueoutputbuf yes
116
+
117
+ # Use object sharing. Can save a lot of memory if you have many common
118
+ # string in your dataset, but performs lookups against the shared objects
119
+ # pool so it uses more CPU and can be a bit slower. Usually it's a good
120
+ # idea.
121
+ #
122
+ # When object sharing is enabled (shareobjects yes) you can use
123
+ # shareobjectspoolsize to control the size of the pool used in order to try
124
+ # object sharing. A bigger pool size will lead to better sharing capabilities.
125
+ # In general you want this value to be at least the double of the number of
126
+ # very common strings you have in your dataset.
127
+ #
128
+ # WARNING: object sharing is experimental, don't enable this feature
129
+ # in production before of Redis 1.0-stable. Still please try this feature in
130
+ # your development environment so that we can test it better.
131
+ #shareobjects no
132
+ #shareobjectspoolsize 1024
@@ -0,0 +1,77 @@
1
+ require File.join(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Resque::Plugins::Timeframe do
4
+
5
+ context "Convention" do
6
+ it "should follow the convention" do
7
+ lambda {
8
+ Resque::Plugin.lint(Resque::Plugins::Timeframe)
9
+ }.should_not raise_error
10
+ end
11
+
12
+ it "should recognize time from range of strings" do
13
+ @range = "01:00".."08:30"
14
+ @date_range = Time.parse("01:00")..Time.parse("08:30")
15
+ RegularWeekRestrictionJob.range(@range).should be_kind_of(Range)
16
+ RegularWeekRestrictionJob.range(@range).should == @date_range
17
+ end
18
+
19
+ it "should recognize time from range of integers" do
20
+ @range = 4..16
21
+ @date_range = Time.parse("04:00")..Time.parse("16:00")
22
+ RegularWeekRestrictionJob.range(@range).should be_kind_of(Range)
23
+ RegularWeekRestrictionJob.range(@range).should == @date_range
24
+ end
25
+
26
+ it "should recognize time from range of time instances" do
27
+ @date_range = Time.parse("04:00")..Time.parse("16:00")
28
+ RegularWeekRestrictionJob.range(@date_range).should be_kind_of(Range)
29
+ RegularWeekRestrictionJob.range(@date_range).should == @date_range
30
+ end
31
+ end
32
+
33
+ context "Settings" do
34
+ it "should allowed by default" do
35
+ AllowedByDefaultTimeframeJob.allowed_at?(:monday).should be_true
36
+ end
37
+
38
+ it "should be restrict if default set to false" do
39
+ RestrictedByDefaultTimeframeJob.allowed_at?(:monday).should be_false
40
+ end
41
+
42
+ it "should allow a job if exact time range specified" do
43
+ @time = Time.mktime(2010, 8, 11, 11, 21, 00)
44
+ Time.stub!(:now).and_return(@time)
45
+ RegularWeekRestrictionJob.allowed_at?(:thursday).should be_true
46
+ end
47
+
48
+ it "should does not allow a job if exact time range specified but out of current time" do
49
+ @time = Time.mktime(2010, 8, 11, 12, 33, 00)
50
+ Time.stub!(:now).and_return(@time)
51
+ RegularWeekRestrictionJob.allowed_at?(:friday).should be_false
52
+ end
53
+ end
54
+
55
+ context "Resque" do
56
+ include PerformJob
57
+
58
+ before(:each) do
59
+ Resque.redis.flushall
60
+ @time = Time.mktime(2010, 8, 11, 11, 59, 00)
61
+ Time.stub!(:now).and_return(@time)
62
+ end
63
+
64
+ it "should perform job if timeframe is allowed" do
65
+ AllowedByDefaultTimeframeJob.should_receive(:perform).and_return("OK")
66
+ result = perform_job(AllowedByDefaultTimeframeJob, 1)
67
+ result.should be_true
68
+ end
69
+
70
+ it "should not perform job if timeframe is restricted" do
71
+ RestrictedByDefaultTimeframeJob.should_not_receive(:perform)
72
+ result = perform_job(RestrictedByDefaultTimeframeJob, 1)
73
+ result.should be_false
74
+ end
75
+ end
76
+
77
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,8 @@
1
+ --colour
2
+ --format
3
+ specdoc
4
+ --reverse
5
+ --timeout
6
+ 20
7
+ --loadby
8
+ mtime
@@ -0,0 +1,94 @@
1
+ require 'rubygems'
2
+ require 'spec/autorun'
3
+ require 'mocha'
4
+
5
+ dir = File.dirname(__FILE__)
6
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
7
+ require 'resque-timeframe'
8
+
9
+ #
10
+ # make sure we can run redis
11
+ #
12
+
13
+ if !system("which redis-server")
14
+ puts '', "** can't find `redis-server` in your path"
15
+ puts "** try running `sudo rake install`"
16
+ abort ''
17
+ end
18
+
19
+
20
+ #
21
+ # start our own redis when the tests start,
22
+ # kill it when they end
23
+ #
24
+
25
+ at_exit do
26
+ next if $!
27
+
28
+ exit_code = Spec::Runner.run
29
+
30
+ pid = `ps -e -o pid,command | grep [r]edis-test`.split(" ")[0]
31
+ puts "Killing test redis server [#{pid}]..."
32
+ `rm -f #{dir}/dump.rdb`
33
+ Process.kill("KILL", pid.to_i)
34
+ exit exit_code
35
+ end
36
+
37
+ puts "Starting redis for testing at localhost:9736..."
38
+ `redis-server #{dir}/redis-test.conf`
39
+ Resque.redis = 'localhost:9736'
40
+
41
+
42
+ #
43
+ # helper
44
+ #
45
+ module PerformJob
46
+ def perform_job(klass, *args)
47
+ resque_job = Resque::Job.new(:testqueue, 'class' => klass, 'args' => args)
48
+ resque_job.perform
49
+ end
50
+ end
51
+
52
+ #
53
+ # job classes
54
+ #
55
+ class AllowedByDefaultTimeframeJob < Resque::Plugins::TimeframedJob
56
+ @queue = :timeframed_queue
57
+ def self.perform(args); end
58
+ end
59
+
60
+ class RestrictedByDefaultTimeframeJob < Resque::Plugins::TimeframedJob
61
+ timeframe :default => false
62
+
63
+ @queue = :timeframed_queue
64
+ def self.perform(args); end
65
+ end
66
+
67
+ class WorkingDaysTimeframeJob < Resque::Plugins::TimeframedJob
68
+ timeframe week - [:saturday, :sunday] => 0..9 # 24 hour format
69
+
70
+ @queue = :timeframed_queue
71
+ def self.perform(args); end
72
+ end
73
+
74
+ class DisabledDayTimeframeJob < Resque::Plugins::TimeframedJob
75
+ timeframe :sunday => true
76
+
77
+ @queue = :timeframed_queue
78
+ def self.perform(args); end
79
+ end
80
+
81
+ class RegularWeekRestrictionJob < Resque::Plugins::TimeframedJob
82
+ timeframe :monday => 0..11 # 0 a.m. .. 11 a.m.
83
+ timeframe :tuesday => 14..23 # 14 p.m. .. 23 p.m.
84
+ timeframe :wednesday => 0..11
85
+ timeframe :thursday => '9:30'..'11:30'
86
+ timeframe :friday => '17:30'..'23:59'
87
+ timeframe :saturday => true
88
+ timeframe :sunday => false
89
+
90
+ @queue = :timeframed_queue
91
+
92
+ def self.perform(args)
93
+ end
94
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-timeframe
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Dmitry Larkin
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-12 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: resque
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 11
30
+ segments:
31
+ - 1
32
+ - 7
33
+ - 0
34
+ version: 1.7.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: resque-timeframe is an extension to resque queue system that allow the execution at configured time.
38
+ email: dmitry.larkin@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - README.markdown
45
+ - LICENSE
46
+ - CHANGELOG
47
+ files:
48
+ - Rakefile
49
+ - lib/resque-timeframe/timeframe.rb
50
+ - lib/resque-timeframe/version.rb
51
+ - lib/resque-timeframe.rb
52
+ - spec/dump.rdb
53
+ - spec/redis-test.conf
54
+ - spec/resque-timeframe/timeframed_job_spec.rb
55
+ - spec/spec.opts
56
+ - spec/spec_helper.rb
57
+ - README.markdown
58
+ - LICENSE
59
+ - CHANGELOG
60
+ has_rdoc: true
61
+ homepage: http://www.railsware.com
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --main
67
+ - README.rdoc
68
+ - --charset=UTF-8
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.3.7
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Bigtable adapter
96
+ test_files: []
97
+