metrognome 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in metrognome.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Chris Carlon
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,31 @@
1
+ # Metrognome
2
+
3
+ A simple repeating task scheduler for Rails applications
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'metrognome'
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install metrognome
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request at cjc25/metrognome
30
+
31
+ TODO: Commit message/code style guidelines
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,9 @@
1
+ module Metrognome
2
+ class Railtie < Rails::Railtie
3
+ railtie_name :metrognome
4
+
5
+ rake_tasks do
6
+ load "tasks/metrognome.rake"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ module Metrognome
2
+ class Registrar
3
+ class << self
4
+ def registered
5
+ @registered ||= []
6
+ end
7
+
8
+ def register scheduler
9
+ raise ArgumentError.new "scheduler must be a Metrognome::Scheduler!" unless scheduler.is_a? Metrognome::Scheduler
10
+
11
+ @registered ||= []
12
+ @registered << scheduler
13
+ scheduler.lock = next_filename
14
+ scheduler.last = Time.now - 1.year
15
+ scheduler.call_setup
16
+ end
17
+
18
+ private
19
+ def next_filename
20
+ @counter ||= 0
21
+ @counter += 1
22
+ "metrognome-#{@counter}"
23
+ end
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,55 @@
1
+ module Metrognome
2
+ class Runner
3
+ class << self
4
+ # TODO pull out reaper_interval into config
5
+ attr_accessor :running, :reaper_last
6
+ REAPER_INTERVAL = 1
7
+
8
+ def start
9
+ setup
10
+
11
+ loop do
12
+ if @_sigterm
13
+ running.each { |t| t.kill }
14
+ reap
15
+ break
16
+ end
17
+ reap if Time.now - reaper_last > REAPER_INTERVAL
18
+
19
+ Metrognome::Registrar.registered.each do |sched|
20
+ if Time.now - sched.last > sched.interval
21
+ running << Thread.new do
22
+ sched.call_task
23
+
24
+ # DB connections will no longer be closed automatically, so
25
+ # close it manually here
26
+ ActiveRecord::Base.connection.close
27
+ end
28
+ end
29
+ end
30
+ # TODO GCD sleep instead of 1
31
+ sleep 1
32
+ end
33
+
34
+ Metrognome::Registrar.registered.each { |s| File.delete s.lock }
35
+ end
36
+
37
+ private
38
+ def setup
39
+ require 'set'
40
+ self.running = Set.new
41
+ self.reaper_last = Time.now
42
+
43
+ Signal.trap "TERM" do
44
+ exit 1 if @_sigterm
45
+ @_sigterm = 1
46
+ end
47
+ end
48
+
49
+ def reap
50
+ running.delete_if { |t| t.join(0) }
51
+ self.reaper_last = Time.now
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,39 @@
1
+ module Metrognome
2
+ class Scheduler
3
+ attr_accessor :interval, :lock, :last
4
+
5
+ def initialize interval, *variables
6
+ self.interval = interval
7
+
8
+ variables.each do |v|
9
+ raise ArgumentError.new "All variables must be Strings or Symbols!" unless v.is_a? String or v.is_a? Symbol
10
+ eval "class << self; attr_accessor :#{v}; end"
11
+ end
12
+ end
13
+
14
+ def lock
15
+ File.join(Rails.root, 'tmp', @lock)
16
+ end
17
+
18
+ def setup &block
19
+ @setup = block
20
+ end
21
+
22
+ def task &block
23
+ @task = block
24
+ end
25
+
26
+ def call_setup
27
+ instance_eval &@setup if @setup
28
+ end
29
+
30
+ def call_task
31
+ File.open(lock, File::RDWR | File::CREAT, 0600) do |file|
32
+ break unless file.flock(File::LOCK_EX | File::LOCK_NB)
33
+ self.last = Time.now
34
+ instance_eval &@task if @task
35
+ end
36
+ end
37
+ end
38
+ end
39
+
@@ -0,0 +1,3 @@
1
+ module Metrognome
2
+ VERSION = "0.0.1"
3
+ end
data/lib/metrognome.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'rails'
2
+ require 'metrognome/railtie'
3
+
4
+ require 'metrognome/scheduler'
5
+ require 'metrognome/registrar'
6
+ require 'metrognome/runner'
7
+
@@ -0,0 +1,60 @@
1
+ namespace :metrognome do
2
+ # TODO move PIDFILE into config
3
+ module Metrognome
4
+ PIDFILE = File.join 'tmp', 'metrognome.pid'
5
+ end
6
+
7
+ task :register => :environment do
8
+ require 'metrognome'
9
+ Dir[File.join(Rails.root, 'config', 'metrognome', '*.rb')].each do |file|
10
+ require file
11
+ end
12
+ end
13
+
14
+ desc 'Begin the metrognome scheduler'
15
+ task :start => :register do
16
+ unless File.exists? Metrognome::PIDFILE
17
+ Signal.trap 'HUP', 'IGNORE'
18
+
19
+ pid = fork do
20
+ Rails.logger = Logger.new 'log/metrognome.log'
21
+ Metrognome::Runner.start
22
+ end
23
+
24
+ Process.detach pid
25
+ File.open(Metrognome::PIDFILE, File::CREAT | File::WRONLY | File::TRUNC, 0600) do |f|
26
+ f.write pid
27
+ end
28
+ else
29
+ $stderr.puts "#{Metrognome::PIDFILE} already exists. Is metrognome already running?"
30
+ end
31
+ end
32
+
33
+ desc 'End the metrognome scheduler'
34
+ task :stop do
35
+ if File.exists? Metrognome::PIDFILE
36
+ pid = 0
37
+ begin
38
+ pid = File.read(Metrognome::PIDFILE).to_i
39
+ rescue
40
+ $stderr.puts "Could not read the contents of #{Metrognome::PIDFILE}"
41
+ return
42
+ end
43
+ raise "Bad PID in #{Metrognome::PIDFILE}" if pid == 0
44
+
45
+ begin
46
+ Process.kill :TERM, pid
47
+ rescue Errno::EPERM
48
+ $stderr.puts "Not permitted to kill process #{pid}"
49
+ return
50
+ rescue Errno::ESRCH
51
+ $stderr.puts "Process #{pid} is not running"
52
+ end
53
+
54
+ File.delete Metrognome::PIDFILE
55
+ else
56
+ $stderr.puts "#{Metrognome::PIDFILE} does not exist"
57
+ end
58
+ end
59
+ end
60
+
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'metrognome/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "metrognome"
8
+ gem.version = Metrognome::VERSION
9
+ gem.authors = ["Chris Carlon"]
10
+ gem.email = ["chris.carlon25@gmail.com"]
11
+ gem.description = %q{A simple repeating task scheduler for Rails applications}
12
+ gem.summary = %q{A simple repeating task scheduler for Rails applications}
13
+ gem.homepage = "https://github.com/cjc25/metrognome"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency 'rails', '~> 3.2'
21
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: metrognome
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Carlon
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
30
+ description: A simple repeating task scheduler for Rails applications
31
+ email:
32
+ - chris.carlon25@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/metrognome.rb
43
+ - lib/metrognome/railtie.rb
44
+ - lib/metrognome/registrar.rb
45
+ - lib/metrognome/runner.rb
46
+ - lib/metrognome/scheduler.rb
47
+ - lib/metrognome/version.rb
48
+ - lib/tasks/metrognome.rake
49
+ - metrognome.gemspec
50
+ homepage: https://github.com/cjc25/metrognome
51
+ licenses: []
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 1.8.24
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: A simple repeating task scheduler for Rails applications
74
+ test_files: []