day_planner 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7a2a1486d2af1a3439a34e767e576e3a7f35a186
4
+ data.tar.gz: df3c42df197b2e4abe2af7a03b0f657cde4d4b93
5
+ SHA512:
6
+ metadata.gz: be3e16005d5f5a48ba2fdbc29804ea1ee575415e28963d59734e115865fc2a4e55d38deae0dbb16cc958cf67edbc6c8a386e2db4f4b2752189db40fd4dc2364d
7
+ data.tar.gz: 5a4b80de0b271862877071eb4d0cfd8da3858c17611988e62d1a2f699d836c32469c373ffea094206414da4aa66b88ef1fd575ef8bcfca5a7580b86dfbe2e814
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ # YARD artifacts
20
+ .yardoc
21
+ _yardoc
22
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in day_planner.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 dsiefert
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Damon Siefert
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # DayPlanner
2
+
3
+ Day Planner is a simple tool to manage in-process execution of scheduled tasks. There are a lot of tools for running scheduled tasks outside the process ([Clockwork](http://rubygems.org/gems/clockwork) is probably what you're looking for if you want an elegant implementation of scheduled tasks outside the main process.)
4
+
5
+ I wrote this because I needed a simple, lightweight tool to schedule small, light tasks on an application running on Heroku, without the extra cost of a dedicated clock process to handle it, but executing more frequently than Heroku's free scheduler permits.
6
+
7
+ Schedule management is in a thread of its own; each scheduled task is run in a thread as well, meaning that lots of long-running tasks could result in lots of threads. Ruby 2.0's handling of threading is improved over the 1.9 branch, so I require that as a minimum, but I obviously can't guarantee that you won't encounter situations in which poorly behaved tasks block your application's main thread. If you want assurances against that, use any of the other tools (did I mention [Clockwork](http://rubygems.org/gems/clockwork)?)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'day_planner'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install day_planner
22
+
23
+ If you're using Rails, it'll expect to find a file listing scheduled tasks in 'config/day_planner_tasks.rb'. If you're not using Rails, do whatever makes you happy. Stick some scheduled tasks somewhere and make sure it's somewhere that runs when your application starts.
24
+
25
+ ## Usage
26
+
27
+ Here's an example of a scheduled task:
28
+
29
+ DayPlanner.schedule(every: 2.minutes) do
30
+ MyClass.my_class_method
31
+ end
32
+
33
+ Obviously you only get those cute little time methods in Rails; otherwise, pass in an integer number of seconds. More sophisticated ways of scheduling tasks are intended in the future, but for now you'll just have to cope. If you don't specify an interval with 'every', it'll happen once and never again.
34
+
35
+ The tasks in the schedule will each be performed on startup and then thereafter according to their stated intervals.
36
+
37
+ By default, DayPlanner checks for tasks to be performed once per minute. You have the power to change this:
38
+
39
+ DayPlanner.interval = 5.seconds
40
+
41
+ Or, like
42
+
43
+ DayPlanner.interval = 5
44
+
45
+ Note that if you try to schedule a task with an interval shorter than DayPlanner's interval, it'll complain and fail. If you shorten DayPlanner's interval to less than that of one of its tasks, it'll probably also complain and fail. Use your best judgment.
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'day_planner/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "day_planner"
8
+ spec.version = DayPlanner::VERSION
9
+ spec.authors = ["Damon Siefert"]
10
+ spec.email = ["siefert@gmail.com"]
11
+ spec.description = %q{Simple gem to handle in-process scheduling of tasks}
12
+ spec.summary = %q{Built to have a way to schedule tasks without running an extra process and incurring extra costs on Heroku.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.required_ruby_version = ">= 2.0.0"
25
+ end
@@ -0,0 +1,92 @@
1
+ module DayPlanner
2
+ @@tasks = []
3
+
4
+ class << self
5
+ def tasks
6
+ @@tasks
7
+ end
8
+
9
+ def schedule(options, &block)
10
+ raise ArgumentError unless options.is_a?(Hash)
11
+
12
+ Task.new(options, &block)
13
+ end
14
+
15
+ def activate
16
+ @@master.kill if defined?(@@master)
17
+
18
+ @@master = Thread.new do
19
+ while true
20
+ check_schedule
21
+ sleep(interval)
22
+ end
23
+ end
24
+ end
25
+
26
+ def interval
27
+ defined?(@@interval) ? @@interval : 60
28
+ end
29
+
30
+ def interval=(value)
31
+ tasks.each do |t|
32
+ Rails.logger.warn("DayPlanner: Check interval exceeds the interval of one of its tasks. The task will perform at most every #{value.inspect}.") if t.interval > value
33
+ end
34
+
35
+ @@interval = value
36
+ end
37
+
38
+ private
39
+ def check_schedule
40
+ # Rails.logger.debug("DayPlanner is checking for tasks to perform")
41
+
42
+ tasks.each do |t|
43
+ if Time.now > t.last_executed + t.interval
44
+ begin
45
+ t.perform
46
+ rescue => e
47
+ Rails.logger.warn("DayPlanner: Scheduled task threw an error! Behave yourselves!")
48
+ Rails.logger.warn(e.inspect)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ class Task
56
+ attr_reader :last_executed, :interval
57
+
58
+ def perform
59
+ @last_executed = Time.now
60
+
61
+ @thread.kill if defined?(@thread)
62
+
63
+ @thread = Thread.new do
64
+ @task.call
65
+ end
66
+ end
67
+
68
+ def initialize(options, &block)
69
+ if options[:every]
70
+ @interval = options[:every]
71
+ raise ArgumentError, "DayPlanner: Task interval is less than scheduler interval. Task not scheduled." if @interval < DayPlanner.interval
72
+ else
73
+ raise ArgumentError, "DayPlanner: Scheduling tasks at anything other than simple intervals using 'every' is still not implemented, not even a little bit."
74
+ end
75
+
76
+ @task = block
77
+
78
+ DayPlanner.tasks.push(self)
79
+
80
+ begin
81
+ perform
82
+ rescue => e
83
+ Rails.logger.warn("DayPlanner: Task caused error on first performance. There's no second chance for a good first impression!")
84
+ Rails.logger.warn(e.inspect)
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ DayPlanner.activate
91
+
92
+ require Rails.root.join('config', 'day_planner_tasks') if defined?(Rails)
@@ -0,0 +1,8 @@
1
+ module DayPlanner
2
+ MAJOR = 0
3
+ MINOR = 0
4
+ TINY = 1
5
+ BUILD = nil
6
+
7
+ VERSION = [MAJOR, MINOR, TINY, BUILD].compact.join(".")
8
+ end
@@ -0,0 +1,2 @@
1
+ require "day_planner/version"
2
+ require "day_planner/base"
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: day_planner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Damon Siefert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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: Simple gem to handle in-process scheduling of tasks
42
+ email:
43
+ - siefert@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - day_planner.gemspec
55
+ - lib/day_planner.rb
56
+ - lib/day_planner/base.rb
57
+ - lib/day_planner/version.rb
58
+ homepage: ''
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: 2.0.0
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.0.14
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Built to have a way to schedule tasks without running an extra process and
82
+ incurring extra costs on Heroku.
83
+ test_files: []