cowtech-rails 2.5.1.0 → 2.6.0.0
Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,122 @@
|
|
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
|
+
class Scheduler
|
10
|
+
attr_accessor :application
|
11
|
+
attr_accessor :logger
|
12
|
+
attr_accessor :pid
|
13
|
+
attr_accessor :definitions
|
14
|
+
attr_accessor :scheduler
|
15
|
+
|
16
|
+
def self.start(application_class, log_file, pid_file, &definitions)
|
17
|
+
self.new(application_class, log_file, pid_file, definitions).execute
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.log(msg)
|
21
|
+
puts msg
|
22
|
+
#@@class_logger ||= Logger.new(Rails.root.to_s + "/log/scheduler.log")
|
23
|
+
#@@class_logger.debug(msg)
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(application_class, log_file, pid_file, block = nil, &definitions)
|
27
|
+
@application = application_class
|
28
|
+
@logger = Logger.new(log_file)
|
29
|
+
@pid = pid_file.to_s
|
30
|
+
@definitions = block || definitions
|
31
|
+
@scheduler = Rufus::Scheduler.start_new
|
32
|
+
@rake_loaded = false
|
33
|
+
end
|
34
|
+
|
35
|
+
def log_string(message, options = {})
|
36
|
+
rv = ""
|
37
|
+
|
38
|
+
rv += "[#{Time.now.strftime("%Y-%m-%d %T")}] " if !options[:no_time]
|
39
|
+
rv += options[:prefix].ensure_array.collect {|p| "[" + p.center(6, " ") + "]" }.join(" ") + " " if options[:prefix].present?
|
40
|
+
rv += message
|
41
|
+
|
42
|
+
rv
|
43
|
+
end
|
44
|
+
|
45
|
+
def log(message, options = {})
|
46
|
+
msg = self.log_string(message, options)
|
47
|
+
type = options[:type] || :info
|
48
|
+
(options[:logger] || @logger).send(type, msg)
|
49
|
+
end
|
50
|
+
|
51
|
+
def execute_rake_task(label, name, args)
|
52
|
+
begin
|
53
|
+
args = args.symbolize_keys
|
54
|
+
args_string = args.present? ? " with arguments #{args.to_json}" : ""
|
55
|
+
|
56
|
+
task = Rake::Task[name]
|
57
|
+
values = task.arg_names.collect {|a| args[a.to_sym] }
|
58
|
+
|
59
|
+
self.log(label + args_string + " ...", {:prefix => ["RAKE", "START", name]})
|
60
|
+
task.reenable
|
61
|
+
task.invoke(*values)
|
62
|
+
self.log("Rake task ended.", {:prefix => ["RAKE", "END", name]})
|
63
|
+
rescue Exception => e
|
64
|
+
self.log("Rake task failed with exception: [#{e.class.to_s}] #{e.to_s}.", {:prefix => ["RAKE", "ERROR", name]})
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def execute_inline_task(label, name)
|
69
|
+
begin
|
70
|
+
self.log(label + " ...", {:prefix => ["INLINE", "START", name]})
|
71
|
+
yield if block_given?
|
72
|
+
self.log("Inline task ended.", {:prefix => ["INLINE", "END", name]})
|
73
|
+
rescue Exception => e
|
74
|
+
self.log("Inline task failed with exception: [#{e.class.to_s}] #{e.to_s}.", {:prefix => ["RAKE", "ERROR", name]})
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def execute
|
79
|
+
self.log("Scheduler started.", {:prefix => "MAIN"})
|
80
|
+
self.handle_plain
|
81
|
+
end
|
82
|
+
|
83
|
+
def handle_phusion_passenger
|
84
|
+
if defined?(PhusionPassenger) then
|
85
|
+
File.delete(@pid) if FileTest.exists?(@pid)
|
86
|
+
|
87
|
+
PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
88
|
+
if forked && !FileTest.exists?(@pid) then
|
89
|
+
self.log("Starting process with PID #{$$}", {:prefix => ["WORKER", "START"]})
|
90
|
+
File.open(@pid, "w") {|f| f.write($$) }
|
91
|
+
self.handle_plain
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
PhusionPassenger.on_event(:stopping_worker_process) do
|
96
|
+
if FileTest.exists?(@pid) then
|
97
|
+
if File.open(@pid, "r") {|f| pid = f.read.to_i} == $$ then
|
98
|
+
self.log("Stopped process with PID #{$$}", {:prefix => ["WORKER", "STOP"]})
|
99
|
+
File.delete(@pid)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
else
|
104
|
+
self.handle_plain
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def handle_plain
|
109
|
+
if !@rake_loaded then
|
110
|
+
@application.load_tasks
|
111
|
+
@rake_loaded = true
|
112
|
+
end
|
113
|
+
|
114
|
+
@definitions.call(self)
|
115
|
+
end
|
116
|
+
|
117
|
+
def method_missing(method, *args, &block)
|
118
|
+
self.scheduler.send(method, *args, &block)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
data/lib/cowtech/version.rb
CHANGED
data/lib/cowtech.rb
CHANGED
@@ -10,8 +10,7 @@ require 'rake'
|
|
10
10
|
require 'cowtech-extensions'
|
11
11
|
require 'cowtech/extensions'
|
12
12
|
require 'cowtech/monkey_patches'
|
13
|
-
|
14
|
-
#require dir + '/../app/models/model_base'
|
13
|
+
require 'cowtech/scheduler'
|
15
14
|
|
16
15
|
module Cowtech
|
17
16
|
class Engine < Rails::Engine
|
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.
|
4
|
+
version: 2.6.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-
|
12
|
+
date: 2011-12-28 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: cowtech-extensions
|
16
|
-
requirement: &
|
16
|
+
requirement: &70262008252160 !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: *
|
24
|
+
version_requirements: *70262008252160
|
25
25
|
description: A general purpose Rails utility plugin.
|
26
26
|
email: shogun_panda@me.com
|
27
27
|
executables: []
|
@@ -40,10 +40,10 @@ 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
|
44
43
|
- lib/cowtech.rb
|
45
44
|
- lib/cowtech/extensions.rb
|
46
45
|
- lib/cowtech/monkey_patches.rb
|
46
|
+
- lib/cowtech/scheduler.rb
|
47
47
|
- lib/cowtech/tasks/app.rake
|
48
48
|
- lib/cowtech/tasks/log.rake
|
49
49
|
- lib/cowtech/tasks/mongodb.rake
|
@@ -1,124 +0,0 @@
|
|
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 self.start(application_class, log_file, pid_file, &definitions)
|
18
|
-
self.new(application_class, log_file, pid_file, definitions).execute
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.log(msg)
|
22
|
-
puts msg
|
23
|
-
#@@class_logger ||= Logger.new(Rails.root.to_s + "/log/scheduler.log")
|
24
|
-
#@@class_logger.debug(msg)
|
25
|
-
end
|
26
|
-
|
27
|
-
def initialize(application_class, log_file, pid_file, block = nil, &definitions)
|
28
|
-
@application = application_class
|
29
|
-
@logger = Logger.new(log_file)
|
30
|
-
@pid = pid_file.to_s
|
31
|
-
@definitions = block || definitions
|
32
|
-
@scheduler = Rufus::Scheduler.start_new
|
33
|
-
@rake_loaded = false
|
34
|
-
end
|
35
|
-
|
36
|
-
def log_string(message, options = {})
|
37
|
-
rv = ""
|
38
|
-
|
39
|
-
rv += "[#{Time.now.strftime("%Y-%m-%d %T")}] " if !options[:no_time]
|
40
|
-
rv += options[:prefix].ensure_array.collect {|p| "[" + p.center(6, " ") + "]" }.join(" ") + " " if options[:prefix].present?
|
41
|
-
rv += message
|
42
|
-
|
43
|
-
rv
|
44
|
-
end
|
45
|
-
|
46
|
-
def log(message, options = {})
|
47
|
-
msg = self.log_string(message, options)
|
48
|
-
type = options[:type] || :info
|
49
|
-
(options[:logger] || @logger).send(type, msg)
|
50
|
-
end
|
51
|
-
|
52
|
-
def execute_rake_task(label, name, args)
|
53
|
-
begin
|
54
|
-
args = args.symbolize_keys
|
55
|
-
args_string = args.present? ? " with arguments #{args.to_json}" : ""
|
56
|
-
|
57
|
-
task = Rake::Task[name]
|
58
|
-
values = task.arg_names.collect {|a| args[a.to_sym] }
|
59
|
-
|
60
|
-
self.log(label + args_string + " ...", {:prefix => ["RAKE", "START", name]})
|
61
|
-
task.reenable
|
62
|
-
task.invoke(*values)
|
63
|
-
self.log("Rake task ended.", {:prefix => ["RAKE", "END", name]})
|
64
|
-
rescue Exception => e
|
65
|
-
self.log("Rake task failed with exception: [#{e.class.to_s}] #{e.to_s}.", {:prefix => ["RAKE", "ERROR", name]})
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
def execute_inline_task(label, name)
|
70
|
-
begin
|
71
|
-
self.log(label + " ...", {:prefix => ["INLINE", "START", name]})
|
72
|
-
yield if block_given?
|
73
|
-
self.log("Inline task ended.", {:prefix => ["INLINE", "END", name]})
|
74
|
-
rescue Exception => e
|
75
|
-
self.log("Inline task failed with exception: [#{e.class.to_s}] #{e.to_s}.", {:prefix => ["RAKE", "ERROR", name]})
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
def execute
|
80
|
-
self.log("Scheduler started.", {:prefix => "MAIN"})
|
81
|
-
self.handle_plain
|
82
|
-
end
|
83
|
-
|
84
|
-
def handle_phusion_passenger
|
85
|
-
if defined?(PhusionPassenger) then
|
86
|
-
File.delete(@pid) if FileTest.exists?(@pid)
|
87
|
-
|
88
|
-
PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
89
|
-
if forked && !FileTest.exists?(@pid) then
|
90
|
-
self.log("Starting process with PID #{$$}", {:prefix => ["WORKER", "START"]})
|
91
|
-
File.open(@pid, "w") {|f| f.write($$) }
|
92
|
-
self.handle_plain
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
PhusionPassenger.on_event(:stopping_worker_process) do
|
97
|
-
if FileTest.exists?(@pid) then
|
98
|
-
if File.open(@pid, "r") {|f| pid = f.read.to_i} == $$ then
|
99
|
-
self.log("Stopped process with PID #{$$}", {:prefix => ["WORKER", "STOP"]})
|
100
|
-
File.delete(@pid)
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
else
|
105
|
-
self.handle_plain
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
def handle_plain
|
110
|
-
if !@rake_loaded then
|
111
|
-
@application.load_tasks
|
112
|
-
@rake_loaded = true
|
113
|
-
end
|
114
|
-
|
115
|
-
@definitions.call(self)
|
116
|
-
end
|
117
|
-
|
118
|
-
def method_missing(method, *args, &block)
|
119
|
-
self.scheduler.send(method, *args, &block)
|
120
|
-
end
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|