cowtech-rails 2.4.0.0 → 2.5.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,12 +14,17 @@ module Cowtech
14
14
  attr_accessor :definitions
15
15
  attr_accessor :scheduler
16
16
 
17
- def initialize(application_class, log_file, pid_file, definitions)
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 initialize(application_class, log_file, pid_file, block = nil, &definitions)
18
22
  @application = application_class
19
23
  @logger = Logger.new(log_file)
20
24
  @pid = pid_file.to_s
21
- @definitions = definitions
25
+ @definitions = block || definitions
22
26
  @scheduler = Rufus::Scheduler.start_new
27
+ @rake_loaded = false
23
28
  end
24
29
 
25
30
  def log_string(message, options = {})
@@ -63,49 +68,47 @@ module Cowtech
63
68
  end
64
69
  end
65
70
 
66
- def define_tasks
67
- raise "Must be overriden by subclasses."
68
- end
69
-
70
71
  def execute
71
72
  self.log("Scheduler started.", {:prefix => "MAIN"})
72
73
  self.handle_plain
73
-
74
- # if defined?(PhusionPassenger) then
75
- # self.handle_phusion_passenger
76
- # else
77
- # self.handle_plain
78
- # end
79
74
  end
80
75
 
81
76
  def handle_phusion_passenger
82
- File.delete(@pid) if FileTest.exists?(@pid)
77
+ if defined?(PhusionPassenger) then
78
+ File.delete(@pid) if FileTest.exists?(@pid)
83
79
 
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
80
+ PhusionPassenger.on_event(:starting_worker_process) do |forked|
81
+ if forked && !FileTest.exists?(@pid) then
82
+ self.log("Starting process with PID #{$$}", {:prefix => ["WORKER", "START"]})
83
+ File.open(@pid, "w") {|f| f.write($$) }
84
+ self.handle_plain
85
+ end
86
+ end
91
87
 
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
88
+ PhusionPassenger.on_event(:stopping_worker_process) do
89
+ if FileTest.exists?(@pid) then
90
+ if File.open(@pid, "r") {|f| pid = f.read.to_i} == $$ then
91
+ self.log("Stopped process with PID #{$$}", {:prefix => ["WORKER", "STOP"]})
92
+ File.delete(@pid)
93
+ end
94
+ end
95
+ end
96
+ else
97
+ self.handle_plain
98
+ end
100
99
  end
101
100
 
102
101
  def handle_plain
103
- @application.load_tasks
102
+ if !@rake_loaded then
103
+ @application.load_tasks
104
+ @rake_loaded = true
105
+ end
106
+
104
107
  @definitions.call(self)
105
- end
108
+ end
106
109
 
107
- def self.start(application_class, log_file, pid_file, &definitions)
108
- self.new(application_class, log_file, pid_file, definitions).execute
110
+ def method_missing(method, *args, &block)
111
+ self.scheduler.send(method, *args, &block)
109
112
  end
110
113
  end
111
114
  end
@@ -6,6 +6,7 @@
6
6
 
7
7
  dir = File.dirname(__FILE__)
8
8
 
9
+ require 'rake'
9
10
  require 'cowtech-extensions'
10
11
  require 'cowtech/extensions'
11
12
  require 'cowtech/monkey_patches'
@@ -18,7 +18,7 @@ module Cowtech
18
18
  IO.popen(cmd) do |f| print f.gets end
19
19
  end
20
20
 
21
- def self.rotate(email_class = nil)
21
+ def self.rotate(args)
22
22
  puts "Rotating log files..."
23
23
 
24
24
  # Get timestamp
@@ -42,7 +42,10 @@ module Cowtech
42
42
  FileUtils.mkdir_p(dir) if !File.directory?(dir)
43
43
 
44
44
  # Send file via mail
45
- email_class.constantize.log_report(log_file).deliver if Rails.env == "production" && email_class.present?
45
+ if Rails.env == "production" && args[:email_class].present? then
46
+ puts "\tForwarding log file to requested email address..."
47
+ args[:email_class].constantize.log_report(log_file).deliver
48
+ end
46
49
 
47
50
  # Copy file
48
51
  FileUtils.cp(log_file, new_file)
@@ -75,7 +78,7 @@ end
75
78
  namespace :log do
76
79
  desc "Rotates log files"
77
80
  task :rotate, [:email_class] => [:environment] do |task, args|
78
- Cowtech::RubyOnRails::LogUtils.rotate(args[:email_class])
81
+ Cowtech::RubyOnRails::LogUtils.rotate(args)
79
82
  end
80
83
 
81
84
  desc "Clean every log file"
@@ -6,12 +6,12 @@
6
6
  module Cowtech
7
7
  module RubyOnRails
8
8
  class MongoUtils
9
- @@log_compressor_command = "tar cjvf"
9
+ @@log_compressor_command = "tar cjf"
10
10
  @@log_compressed_extension = "tbz2"
11
11
 
12
12
  def self.run_command(cmd); system(cmd) end
13
13
 
14
- def self.backup
14
+ def self.backup(task_args)
15
15
  puts "--- Backupping MongoDB ..."
16
16
 
17
17
  # Get configuration
@@ -21,6 +21,7 @@ module Cowtech
21
21
 
22
22
  # Set output dir
23
23
  dest_file = Rails.root + "backups/mongodb/mongo-#{Time.now.strftime("%Y%m%d-%H%M%S")}"
24
+ final_file = dest_file.to_s + "." + @@log_compressed_extension
24
25
 
25
26
  # Create directory
26
27
  dir = File.dirname(dest_file)
@@ -39,9 +40,15 @@ module Cowtech
39
40
 
40
41
  # Compress
41
42
  puts "\t\tCompressing backup ..."
42
- Cowtech::RubyOnRails::MongoUtils.run_command(@@log_compressor_command + " " + dest_file.to_s + "." + @@log_compressed_extension + " " + dest_file.to_s)
43
+ Cowtech::RubyOnRails::MongoUtils.run_command(@@log_compressor_command + " " + final_file + " " + dest_file.to_s)
43
44
  FileUtils.rm_rf(dest_file.to_s)
44
-
45
+
46
+ # Send file via mail
47
+ if Rails.env == "production" && task_args[:email_class].present? then
48
+ puts "\tForwarding backup file to requested email address..."
49
+ task_args[:email_class].constantize.backup(final_file).deliver
50
+ end
51
+
45
52
  puts "Backup saved in #{dest_file}.#{@@log_compressed_extension}"
46
53
  end
47
54
 
@@ -61,8 +68,8 @@ end
61
68
 
62
69
  namespace :mongodb do
63
70
  desc "Backups MongoDB collections"
64
- task :backup do |task|
65
- Cowtech::RubyOnRails::MongoUtils.backup
71
+ task :backup, [:email_class] => [:environment] do |task, args|
72
+ Cowtech::RubyOnRails::MongoUtils.backup(args)
66
73
  end
67
74
 
68
75
  desc "Clean every backup file"
@@ -12,9 +12,10 @@ module Cowtech
12
12
 
13
13
  def self.run_command(cmd); system(cmd) end
14
14
 
15
- def self.mysql_execute(config)
16
- dest_file = Rails.root + "backups/mysql/db-#{Time.now.strftime("%Y%m%d-%H%M%S")}.sql"
17
-
15
+ def self.mysql_execute(config, task_args)
16
+ dest_file = Rails.root + "backups/mysql/mysql-#{Time.now.strftime("%Y%m%d-%H%M%S")}.sql"
17
+ final_file = dest_file.to_s + "." + @@log_compressed_extension
18
+
18
19
  dump_cmd = "mysqldump"
19
20
  dump_args = {"" => "-R -r \"#{dest_file}\"", "host" => "-h @@ARG@@", "username" => "-u @@ARG@@", "password" => "--password=\"@@ARG@@\"", "database" => "@@ARG@@"}
20
21
 
@@ -40,7 +41,13 @@ module Cowtech
40
41
  puts "\tCompressing backup ..."
41
42
  Cowtech::RubyOnRails::MysqlUtils.run_command(@@log_compressor_command + " " + dest_file.to_s)
42
43
 
43
- puts "Backup saved in #{dest_file}.#{@@log_compressed_extension}"
44
+ # Send file via mail
45
+ if Rails.env == "production" && task_args[:email_class].present? then
46
+ puts "\tForwarding backup file to requested email address..."
47
+ task_args[:email_class].constantize.backup(final_file).deliver
48
+ end
49
+
50
+ puts "Backup saved in #{final_file}"
44
51
  end
45
52
 
46
53
  # ALIAS
@@ -71,14 +78,14 @@ module Cowtech
71
78
  end
72
79
  end
73
80
 
74
- def self.backup
81
+ def self.backup(args)
75
82
  puts "--- Backupping database ..."
76
83
  # OTTENIAMO LA CONFIGURAZIONE
77
84
  db_config = YAML.load_file(Rails.root + "config/database.yml")
78
85
  env = Rails.env
79
86
 
80
87
  # ESEGUIAMO
81
- Cowtech::RubyOnRails::MysqlUtils.send("#{db_config[env]["adapter"]}_execute", db_config[env])
88
+ Cowtech::RubyOnRails::MysqlUtils.send("#{db_config[env]["adapter"]}_execute", db_config[env], args)
82
89
  end
83
90
 
84
91
  def self.backup_clean
@@ -102,8 +109,8 @@ namespace :mysql do
102
109
  end
103
110
 
104
111
  desc "Backups database"
105
- task :backup do |task|
106
- Cowtech::RubyOnRails::SqlUtils.backup
112
+ task :backup, [:email_class] => [:environment] do |task, args|
113
+ Cowtech::RubyOnRails::SqlUtils.backup(args)
107
114
  end
108
115
 
109
116
  desc "Clean every backup file"
@@ -8,7 +8,7 @@ module Cowtech
8
8
  module Rails
9
9
  module Version
10
10
  MAJOR = 2
11
- MINOR = 4
11
+ MINOR = 5
12
12
  PATCH = 0
13
13
  BUILD = 0
14
14
 
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.0.0
4
+ version: 2.5.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-16 00:00:00.000000000Z
12
+ date: 2011-12-17 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cowtech-extensions
16
- requirement: &70294263869080 !ruby/object:Gem::Requirement
16
+ requirement: &70342270472780 !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: *70294263869080
24
+ version_requirements: *70342270472780
25
25
  description: A general purpose Rails utility plugin.
26
26
  email: shogun_panda@me.com
27
27
  executables: []