coiasira 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :rubygems
2
+
3
+ # Specify your gem's dependencies in activerecord-sqlanywhere-adapter.gemspec
4
+ gemspec
data/README ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,79 @@
1
+ # Rakefile for Coiasira. -*-ruby-*-
2
+ require 'bundler'
3
+
4
+ def install_tasks(opts = nil)
5
+ dir = caller.find{|c| /Rakefile:/}[/^(.*?)\/Rakefile:/, 1]
6
+ h = Bundler::GemHelper.new(dir, opts && opts[:name])
7
+ h.install
8
+ h
9
+ end
10
+ helper = install_tasks
11
+ spec = helper.gemspec
12
+
13
+ require 'rake/clean'
14
+ CLEAN.include 'pkg'
15
+
16
+ task :git_local_check do
17
+ sh "git diff --no-ext-diff --ignore-submodules --quiet --exit-code" do |ok, _|
18
+ raise "working directory is unclean" if !ok
19
+ sh "git diff-index --cached --quiet --ignore-submodules HEAD --" do |ok, _|
20
+ raise "git index is unclean" if !ok
21
+ end
22
+ end
23
+ end
24
+
25
+
26
+ require 'rake/rdoctask'
27
+ Rake::RDocTask.new do |rdoc|
28
+ rdoc.rdoc_dir = 'rdoc'
29
+ rdoc.title = "#{spec.name} #{spec.version}"
30
+ rdoc.rdoc_files.include('README*')
31
+ rdoc.rdoc_files.include('lib/**/*.rb')
32
+ end
33
+
34
+
35
+ #require 'rake/rdoctask'
36
+ #require 'rake/testtask'
37
+ #require File.join(File.dirname(__FILE__), 'lib', 'coiasira', 'version')
38
+
39
+ #desc "Default Task"
40
+ #task :default => [:package]
41
+
42
+ #desc "Do predistribution stuff"
43
+ #task :predist => [:chmod, :rdoc]
44
+
45
+
46
+ #desc "Make an archive as .tar.gz"
47
+ #task :dist => [:test, :predist] do
48
+ # sh "git archive --format=tar --prefix=#{release}/ HEAD^{tree} >#{release}.tar"
49
+ # sh "pax -waf #{release}.tar -s ':^:#{release}/:' RDOX doc"
50
+ # sh "gzip -f -9 #{release}.tar"
51
+ #end
52
+
53
+ #desc "Make binaries executable"
54
+ #task :chmod do
55
+ # Dir["bin/*"].each { |binary| File.chmod(0775, binary) }
56
+ # Dir["test/cgi/test*"].each { |binary| File.chmod(0775, binary) }
57
+ #end
58
+
59
+ #desc "Run all the tests"
60
+ #task :test => [:chmod] do
61
+ # sh "specrb -Ilib:test -w #{ENV['TEST'] || '-a'} #{ENV['TESTOPTS']}"
62
+ #end
63
+
64
+ #def gem_version
65
+ # Coiasira::VERSION::STRING
66
+ #end
67
+
68
+ #def release
69
+ # "coiasira-#{gem_version}"
70
+ #end
71
+
72
+ #def manifest
73
+ # `svn ls -R`.split("\n")
74
+ #end
75
+
76
+ #task :install => [:gem] do
77
+ # sh "gem install pkg/#{release}.gem"
78
+ #end
79
+
data/bin/coiasira ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- ruby -*-
3
+
4
+ require 'optparse'
data/coiasira.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/coiasira", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "coiasira"
6
+ s.version = Coiasira::VERSION
7
+ s.author = 'Brian Olsen'
8
+ s.email = 'brian@maven-group.org'
9
+ s.homepage = "http://github.com/griff/coaisira"
10
+ s.summary = "some helpers for running scheduled jobs"
11
+ s.description = "Something something"
12
+
13
+ s.required_rubygems_version = ">= 1.3.6"
14
+
15
+ s.add_development_dependency "bundler", ">= 1.0.0"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
19
+ s.require_path = 'lib'
20
+
21
+ s.test_files = Dir['spec/*_spec.rb']
22
+
23
+ end
@@ -0,0 +1,51 @@
1
+ module Coiasira
2
+ class Base
3
+ # Accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then passed
4
+ # on to any new database connections made and which can be retrieved on both a class and instance level by calling +logger+.
5
+ unless defined? @@logger
6
+ @@logger = nil
7
+ end
8
+
9
+ def self.logger
10
+ @@logger
11
+ end
12
+
13
+ def self.logger=(obj)
14
+ @@logger = obj
15
+ end
16
+
17
+ def logger
18
+ @@logger
19
+ end
20
+
21
+ class << self
22
+ def process(context)
23
+ new.process(context)
24
+ end
25
+
26
+ def default_action
27
+ 'default'
28
+ end
29
+ end
30
+
31
+ def initialize()
32
+ @action = self.class.default_action
33
+ end
34
+
35
+ # def logger
36
+ # merged.logger || self.class.logger
37
+ # end
38
+
39
+ def process(context)
40
+ @action = context.action if context && context.respond_to?(:action) && context.action
41
+ logger.info("Starting job #{self.class.name.underscore} with action #{@action}")
42
+ begin
43
+ self.__send__(@action, context)
44
+ logger.info("Finished job #{self.class.name.underscore} with action #{@action}")
45
+ rescue
46
+ logger.info("Failed job #{self.class.name.underscore} with action #{@action}")
47
+ raise
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,33 @@
1
+ if defined? Rails::Initializer
2
+ require "#{RAILS_ROOT}/config/environment"
3
+ end
4
+
5
+ def setup
6
+ puts "script/job <job_name> [action] [arguments]"
7
+ exit 1
8
+ end
9
+
10
+ setup if ARGV.include?('--help') || ARGV.include?('-h') || ARGV.size == 0
11
+
12
+ command = ARGV.shift
13
+ runner = Coiasira::Runner.new
14
+
15
+ unless runner.has_command?(command)
16
+ puts "No job named '#{command}'"
17
+ setup
18
+ end
19
+
20
+ log = Logger.new(STDOUT)
21
+ log.level = Logger::DEBUG
22
+ Coiasira::Base.logger = log
23
+
24
+ context = Coiasira::Context.new
25
+ if( ARGV.size > 0 )
26
+ action = ARGV.shift
27
+ context.action = action
28
+ ARGV.each do |arg|
29
+ name, value = arg.split('=')
30
+ context.details.data[name] = value
31
+ end
32
+ end
33
+ runner.process(command, context)
@@ -0,0 +1,42 @@
1
+ module Coiasira
2
+ class Context
3
+ attr_reader :merged, :details, :trigger, :previous_fire_time, :next_fire_time, :scheduled_fire_time
4
+
5
+ def initialize
6
+ @details = JobDetail.new
7
+ @trigger = Trigger.new
8
+ @merged = JobData.new
9
+ @data = Hash.new
10
+ end
11
+
12
+ def [](key)
13
+ @data[key]
14
+ end
15
+
16
+ def []=(key, value)
17
+ @data[key] = value
18
+ end
19
+
20
+ def action
21
+ details.data.action
22
+ end
23
+
24
+ def action=(value)
25
+ details.data.action=value.to_s
26
+ end
27
+
28
+ def prepare
29
+ @merged.merge! details.data
30
+ @merged.merge! trigger.data
31
+ end
32
+
33
+ def method_missing(sym, value=nil)
34
+ sym = sym.to_s
35
+ if sym =~ /^(.*)=$/
36
+ return self[$~[1]] = value
37
+ else
38
+ return self[sym]
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,12 @@
1
+ module Coiasira
2
+ class JobData < Hash
3
+ def method_missing(sym, value=nil)
4
+ sym = sym.to_s
5
+ if sym =~ /^(.*)=$/
6
+ return self[$~[1]] = value
7
+ else
8
+ return self[sym]
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,46 @@
1
+ module Coiasira
2
+ class JobDetail
3
+ attr_accessor :name, :group, :description
4
+ attr_reader :data
5
+
6
+ def initialize
7
+ @data = JobData.new
8
+ end
9
+
10
+ def full_name
11
+ "#{group}.#{name}"
12
+ end
13
+
14
+ def volatility=(value)
15
+ @volatility = value
16
+ end
17
+
18
+ def volatility?
19
+ @volatility
20
+ end
21
+
22
+ def durability=(value)
23
+ @durability = value
24
+ end
25
+
26
+ def durability?
27
+ @durability
28
+ end
29
+
30
+ def requests_recovery=(value)
31
+ @requests_recovery = value
32
+ end
33
+
34
+ def requests_recovery?
35
+ @requests_recovery
36
+ end
37
+
38
+ def stateful=(value)
39
+ @stateful = value
40
+ end
41
+
42
+ def stateful?
43
+ @stateful
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,13 @@
1
+ module Coiasira
2
+ class JobExecutionError < SchedulerError
3
+ attr_accessor :refire, :unschedule_firing_trigger, :unchedule_all_triggers
4
+
5
+ def initialize(options={})
6
+ unknown_keys = options.keys - [:refire_immediately, :unschedule_firing_trigger, :unchedule_all_triggers].flatten
7
+ raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
8
+ @refire = options[:refire_immediately] || false
9
+ @unschedule_trigger = options[:unschedule_firing_trigger] || false
10
+ @unchedule_all_triggers = options[:unchedule_all_triggers] || false
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module Coiasira
2
+ class JobWrapper
3
+ attr_reader :job_class
4
+
5
+ def initialize(name)
6
+ @name = name
7
+ @job_class = nil
8
+ end
9
+
10
+ def process(context)
11
+ @job_class = Jobs.find_class(@name) unless job_class
12
+ job_class.process(context)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,83 @@
1
+ module Coiasira
2
+ module Jobs
3
+ # The root paths which may contain job files
4
+ unless defined? @@job_paths
5
+ @@job_paths = []
6
+ end
7
+
8
+ def self.job_paths
9
+ @@job_paths
10
+ end
11
+
12
+ def job_paths
13
+ @@job_paths
14
+ end
15
+ def self.job_paths=(obj)
16
+ @@job_paths = obj
17
+ end
18
+ def job_paths=(obj)
19
+ @@job_paths = obj
20
+ end
21
+
22
+ class << self
23
+ # Returns an array of paths, cleaned of double-slashes and relative path references.
24
+ # * "\\\" and "//" become "\\" or "/".
25
+ # * "/foo/bar/../config" becomes "/foo/config".
26
+ # The returned array is sorted by length, descending.
27
+ def normalize_paths(paths)
28
+ # do the hokey-pokey of path normalization...
29
+ paths = paths.collect do |path|
30
+ path = path.
31
+ gsub("//", "/"). # replace double / chars with a single
32
+ gsub("\\\\", "\\"). # replace double \ chars with a single
33
+ gsub(%r{(.)[\\/]$}, '\1') # drop final / or \ if path ends with it
34
+
35
+ # eliminate .. paths where possible
36
+ re = %r{[^/\\]+[/\\]\.\.[/\\]}
37
+ path.gsub!(re, "") while path.match(re)
38
+ path
39
+ end
40
+
41
+ # start with longest path, first
42
+ paths = paths.uniq.sort_by { |path| - path.length }
43
+ end
44
+
45
+ # Returns the array of job names currently available.
46
+ def possible_jobs
47
+ unless @possible_jobs
48
+ @possible_jobs = []
49
+ paths = job_paths.select { |path| File.directory?(path) && path != "." }
50
+
51
+ seen_paths = Hash.new {|h, k| h[k] = true; false}
52
+ normalize_paths(paths).each do |load_path|
53
+ Dir["#{load_path}/**/*_job.rb"].collect do |path|
54
+ next if seen_paths[path.gsub(%r{^\.[/\\]}, "")]
55
+
56
+ job_name = path[(load_path.length + 1)..-1]
57
+
58
+ job_name.gsub!(/_job\.rb\Z/, '')
59
+ @possible_jobs << job_name
60
+ end
61
+ end
62
+
63
+ # remove duplicates
64
+ @possible_jobs.uniq!
65
+ end
66
+ @possible_jobs
67
+ end
68
+
69
+ def has_job?(name)
70
+ possible_jobs.include?(name)
71
+ end
72
+
73
+ def load_job(name)
74
+ raise NameError, name unless has_job?(name)
75
+ JobWrapper.new(name)
76
+ end
77
+
78
+ def find_class(name)
79
+ "#{name.camelize}Job".constantize
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,90 @@
1
+ module Rails
2
+ class Initializer
3
+ def self.alias_method_chain(target, feature)
4
+ # Strip out punctuation on predicates or bang methods since
5
+ # e.g. target?_without_feature is not a valid method name.
6
+ aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
7
+ yield(aliased_target, punctuation) if block_given?
8
+
9
+ with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
10
+
11
+ alias_method without_method, target
12
+ alias_method target, with_method
13
+
14
+ case
15
+ when public_method_defined?(without_method)
16
+ public target
17
+ when protected_method_defined?(without_method)
18
+ protected target
19
+ when private_method_defined?(without_method)
20
+ private target
21
+ end
22
+ end
23
+
24
+ def initialize_coiasira
25
+ return unless configuration.frameworks.include?(:coiasira)
26
+ ::Coiasira::Jobs.job_paths = configuration.job_paths
27
+ # ActionController::Routing::Routes.configuration_file = configuration.routes_configuration_file
28
+ # ActionController::Routing::Routes.reload
29
+ end
30
+
31
+ def initialize_routing_with_coiasira
32
+ initialize_routing_without_coiasira
33
+ initialize_coiasira
34
+ end
35
+ alias_method_chain :initialize_routing, :coiasira
36
+ end
37
+
38
+ class Configuration
39
+ def self.alias_method_chain(target, feature)
40
+ # Strip out punctuation on predicates or bang methods since
41
+ # e.g. target?_without_feature is not a valid method name.
42
+ aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
43
+ yield(aliased_target, punctuation) if block_given?
44
+
45
+ with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
46
+
47
+ alias_method without_method, target
48
+ alias_method target, with_method
49
+
50
+ case
51
+ when public_method_defined?(without_method)
52
+ public target
53
+ when protected_method_defined?(without_method)
54
+ protected target
55
+ when private_method_defined?(without_method)
56
+ private target
57
+ end
58
+ end
59
+
60
+ attr_accessor :coiasira
61
+
62
+ # The list of paths that should be searched for jobs. (Defaults
63
+ # to <tt>app/jobs</tt>.)
64
+ attr_accessor :job_paths
65
+
66
+ def initialize_with_coiasira
67
+ initialize_without_coiasira
68
+ self.job_paths = default_job_paths
69
+ end
70
+ alias_method_chain :initialize, :coiasira
71
+
72
+ private
73
+ def default_frameworks_with_coiasira
74
+ default_frameworks_without_coiasira + [ :coiasira ]
75
+ end
76
+ alias_method_chain :default_frameworks, :coiasira
77
+
78
+ def default_load_paths_with_coiasira
79
+ paths = default_load_paths_without_coiasira
80
+
81
+ # Add the app's job directory
82
+ paths.concat(Dir["#{root_path}/app/jobs/"])
83
+ end
84
+ alias_method_chain :default_load_paths, :coiasira
85
+
86
+ def default_job_paths
87
+ [File.join(root_path, 'app', 'jobs')]
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,12 @@
1
+ module Coiasira
2
+ class Runner
3
+ def has_command?(cmd)
4
+ Jobs.has_job?(cmd)
5
+ end
6
+
7
+ def process(cmd, context=nil)
8
+ context.prepare if context.respond_to? :prepare
9
+ Jobs.load_job(cmd).process(context)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ module Coiasira
2
+ class SchedulerError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,9 @@
1
+ module Coiasira
2
+ class Trigger
3
+ attr_reader :data
4
+
5
+ def initialize
6
+ @data = JobData.new
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Coiasira #:nodoc:
2
+ VERSION = '0.1.0'
3
+ end
data/lib/coiasira.rb ADDED
@@ -0,0 +1,41 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ =begin comment
5
+ unless defined?(ActiveSupport)
6
+ begin
7
+ $:.unshift "#{File.dirname(__FILE__)}/../../activesupport/lib"
8
+ require 'active_support'
9
+ rescue LoadError
10
+ require 'rubygems'
11
+ begin
12
+ gem 'activesupport'
13
+ rescue LoadError
14
+ require 'coiasira/core_ext'
15
+ end
16
+ end
17
+ end
18
+ =end
19
+
20
+ require 'coiasira/version'
21
+ require 'coiasira/scheduler_error'
22
+ require 'coiasira/job_execution_error'
23
+ require 'coiasira/job_data'
24
+ require 'coiasira/job_detail'
25
+ require 'coiasira/trigger'
26
+ require 'coiasira/context'
27
+ require 'coiasira/base'
28
+ require 'coiasira/job_wrapper'
29
+ require 'coiasira/jobs'
30
+ require 'coiasira/runner'
31
+
32
+ module Coiasira
33
+ def self.silence_warnings
34
+ oldv, $VERBOSE = $VERBOSE, nil
35
+ begin
36
+ yield
37
+ ensure
38
+ $VERBOSE = oldv
39
+ end
40
+ end
41
+ end
File without changes
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: coiasira
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Brian Olsen
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-13 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: bundler
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 23
29
+ segments:
30
+ - 1
31
+ - 0
32
+ - 0
33
+ version: 1.0.0
34
+ type: :development
35
+ version_requirements: *id001
36
+ description: Something something
37
+ email: brian@maven-group.org
38
+ executables:
39
+ - coiasira
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - README
48
+ - Rakefile
49
+ - bin/coiasira
50
+ - coiasira.gemspec
51
+ - lib/coiasira.rb
52
+ - lib/coiasira/base.rb
53
+ - lib/coiasira/commands/job.rb
54
+ - lib/coiasira/context.rb
55
+ - lib/coiasira/job_data.rb
56
+ - lib/coiasira/job_detail.rb
57
+ - lib/coiasira/job_execution_error.rb
58
+ - lib/coiasira/job_wrapper.rb
59
+ - lib/coiasira/jobs.rb
60
+ - lib/coiasira/rails/initializer.rb
61
+ - lib/coiasira/runner.rb
62
+ - lib/coiasira/scheduler_error.rb
63
+ - lib/coiasira/trigger.rb
64
+ - lib/coiasira/version.rb
65
+ - test/spec_coiasira.rb
66
+ homepage: http://github.com/griff/coaisira
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options: []
71
+
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 23
89
+ segments:
90
+ - 1
91
+ - 3
92
+ - 6
93
+ version: 1.3.6
94
+ requirements: []
95
+
96
+ rubyforge_project:
97
+ rubygems_version: 1.8.5
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: some helpers for running scheduled jobs
101
+ test_files: []
102
+