cowtech-rails 2.3.3.0 → 2.4.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,113 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the cowtech-rails gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ module Cowtech
8
+ module RubyOnRails
9
+ module Models
10
+ class Scheduler
11
+ attr_accessor :application
12
+ attr_accessor :logger
13
+ attr_accessor :pid
14
+ attr_accessor :definitions
15
+ attr_accessor :scheduler
16
+
17
+ def initialize(application_class, log_file, pid_file, definitions)
18
+ @application = application_class
19
+ @logger = Logger.new(log_file)
20
+ @pid = pid_file.to_s
21
+ @definitions = definitions
22
+ @scheduler = Rufus::Scheduler.start_new
23
+ end
24
+
25
+ def log_string(message, options = {})
26
+ rv = ""
27
+
28
+ rv += "[#{Time.now.strftime("%Y-%m-%d %T")}]" if !options[:no_time]
29
+ prefix = options[:prefix].ensure_array.collect {|p| "[" + p.center(6, " ") + "]" }.join(" ")
30
+ rv += " #{prefix} " if prefix.present?
31
+ rv += message
32
+
33
+ rv
34
+ end
35
+
36
+ def log(message, options = {})
37
+ msg = self.log_string(message, options)
38
+ type = options[:type] || :info
39
+ (options[:logger] || @logger).send(type, msg)
40
+ end
41
+
42
+ def execute_rake_task(label, name, args = nil)
43
+ begin
44
+ args_string = args.present? ? " with arguments #{args.to_json}" : ""
45
+
46
+ self.log(label + args_string + " ...", {:prefix => ["RAKE", "START", name]})
47
+ task = Rake::Task[name]
48
+ task.reenable
49
+ task.invoke(args)
50
+ self.log("Rake task ended.", {:prefix => ["RAKE", "END", name]})
51
+ rescue Exception => e
52
+ self.log("Rake task failed with exception: [#{e.class.to_s}] #{e.to_s}.", {:prefix => ["RAKE", "ERROR", name]})
53
+ end
54
+ end
55
+
56
+ def execute_inline_task(label, name)
57
+ begin
58
+ self.log(label + " ...", {:prefix => ["INLINE", "START", name]})
59
+ yield if block_given?
60
+ self.log("Inline task ended.", {:prefix => ["INLINE", "END", name]})
61
+ rescue Exception => e
62
+ self.log("Inline task failed with exception: [#{e.class.to_s}] #{e.to_s}.", {:prefix => ["RAKE", "ERROR", name]})
63
+ end
64
+ end
65
+
66
+ def define_tasks
67
+ raise "Must be overriden by subclasses."
68
+ end
69
+
70
+ def execute
71
+ self.log("Scheduler started.", {:prefix => "MAIN"})
72
+ self.handle_plain
73
+
74
+ # if defined?(PhusionPassenger) then
75
+ # self.handle_phusion_passenger
76
+ # else
77
+ # self.handle_plain
78
+ # end
79
+ end
80
+
81
+ def handle_phusion_passenger
82
+ File.delete(@pid) if FileTest.exists?(@pid)
83
+
84
+ PhusionPassenger.on_event(:starting_worker_process) do |forked|
85
+ if forked && !FileTest.exists?(@pid) then
86
+ self.log("Starting process with PID #{$$}", {:prefix => ["WORKER", "START"]})
87
+ File.open(@pid, "w") {|f| f.write($$) }
88
+ self.handle_plain
89
+ end
90
+ end
91
+
92
+ PhusionPassenger.on_event(:stopping_worker_process) do
93
+ if FileTest.exists?(@pid) then
94
+ if File.open(@pid, "r") {|f| pid = f.read.to_i} == $$ then
95
+ self.log("Stopped process with PID #{$$}", {:prefix => ["WORKER", "STOP"]})
96
+ File.delete(@pid)
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+ def handle_plain
103
+ @application.load_tasks
104
+ @definitions.call(self)
105
+ end
106
+
107
+ def self.start(application_class, log_file, pid_file, &definitions)
108
+ self.new(application_class, log_file, pid_file, definitions).execute
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
@@ -8,8 +8,8 @@ module Cowtech
8
8
  module Rails
9
9
  module Version
10
10
  MAJOR = 2
11
- MINOR = 3
12
- PATCH = 3
11
+ MINOR = 4
12
+ PATCH = 0
13
13
  BUILD = 0
14
14
 
15
15
  STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cowtech-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.3.0
4
+ version: 2.4.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-15 00:00:00.000000000Z
12
+ date: 2011-12-16 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cowtech-extensions
16
- requirement: &70294472345600 !ruby/object:Gem::Requirement
16
+ requirement: &70294263869080 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70294472345600
24
+ version_requirements: *70294263869080
25
25
  description: A general purpose Rails utility plugin.
26
26
  email: shogun_panda@me.com
27
27
  executables: []
@@ -40,6 +40,7 @@ files:
40
40
  - app/models/cowtech/ruby_on_rails/models/mongoid/cowtech.rb
41
41
  - app/models/cowtech/ruby_on_rails/models/mongoid/logging.rb
42
42
  - app/models/cowtech/ruby_on_rails/models/mongoid/sequence.rb
43
+ - app/models/cowtech/ruby_on_rails/models/scheduler.rb
43
44
  - lib/cowtech.rb
44
45
  - lib/cowtech/extensions.rb
45
46
  - lib/cowtech/monkey_patches.rb