cowtech-rails 2.5.0.0 → 2.5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,6 +18,12 @@ module Cowtech
18
18
  self.new(application_class, log_file, pid_file, definitions).execute
19
19
  end
20
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
+
21
27
  def initialize(application_class, log_file, pid_file, block = nil, &definitions)
22
28
  @application = application_class
23
29
  @logger = Logger.new(log_file)
@@ -30,9 +36,8 @@ module Cowtech
30
36
  def log_string(message, options = {})
31
37
  rv = ""
32
38
 
33
- rv += "[#{Time.now.strftime("%Y-%m-%d %T")}]" if !options[:no_time]
34
- prefix = options[:prefix].ensure_array.collect {|p| "[" + p.center(6, " ") + "]" }.join(" ")
35
- rv += " #{prefix} " if prefix.present?
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?
36
41
  rv += message
37
42
 
38
43
  rv
@@ -44,14 +49,17 @@ module Cowtech
44
49
  (options[:logger] || @logger).send(type, msg)
45
50
  end
46
51
 
47
- def execute_rake_task(label, name, args = nil)
52
+ def execute_rake_task(label, name, args)
48
53
  begin
54
+ args = args.symbolize_keys
49
55
  args_string = args.present? ? " with arguments #{args.to_json}" : ""
50
56
 
51
- self.log(label + args_string + " ...", {:prefix => ["RAKE", "START", name]})
52
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]})
53
61
  task.reenable
54
- task.invoke(args)
62
+ task.invoke(*values)
55
63
  self.log("Rake task ended.", {:prefix => ["RAKE", "END", name]})
56
64
  rescue Exception => e
57
65
  self.log("Rake task failed with exception: [#{e.class.to_s}] #{e.to_s}.", {:prefix => ["RAKE", "ERROR", name]})
@@ -17,16 +17,16 @@ module Cowtech
17
17
  def self.run_command(cmd)
18
18
  IO.popen(cmd) do |f| print f.gets end
19
19
  end
20
-
21
- def self.rotate(args)
22
- puts "Rotating log files..."
20
+
21
+ def self.rotate(rake_args)
22
+ Cowtech::RubyOnRails::Models::Scheduler.log "Rotating log files..."
23
23
 
24
24
  # Get timestamp
25
25
  tstamp = Time.now.strftime("%Y%m%d")
26
26
 
27
27
  # For each log file
28
28
  Dir.glob(Rails.root + "log/*.log") do |log_file|
29
- puts "\tRotating #{log_file} ..."
29
+ Cowtech::RubyOnRails::Models::Scheduler.log "\tRotating #{log_file} ..."
30
30
  new_name = "#{File.basename(log_file)}-#{tstamp}" # CREIAMO IL NOME
31
31
 
32
32
  # Resolv duplicates
@@ -42,9 +42,9 @@ module Cowtech
42
42
  FileUtils.mkdir_p(dir) if !File.directory?(dir)
43
43
 
44
44
  # Send file via mail
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
45
+ if (Rails.env == "production" || rake_args["force"].to_boolean) && rake_args["email_class"].present? then
46
+ Cowtech::RubyOnRails::Models::Scheduler.log "\tForwarding log file to requested email address..."
47
+ rake_args["email_class"].constantize.log_report(log_file).deliver
48
48
  end
49
49
 
50
50
  # Copy file
@@ -55,18 +55,18 @@ module Cowtech
55
55
  end
56
56
 
57
57
  # Truncate files
58
- puts "Truncating current log files ..."
58
+ Cowtech::RubyOnRails::Models::Scheduler.log "Truncating current log files ..."
59
59
  Dir.glob(Rails.root + "log/*.log") do |log_file|
60
60
  File.open(log_file, "w").close
61
61
  end
62
62
  end
63
63
 
64
64
  def self.clean
65
- puts "Cleaning log files..."
65
+ Cowtech::RubyOnRails::Models::Scheduler.log "Cleaning log files..."
66
66
 
67
67
  ["backups/logs/*.log", "backups/logs/*.#{@@log_compressed_extension}"].each do |path|
68
68
  Dir.glob(Rails.root + path) do |log_file|
69
- puts "\tDeleting #{log_file.gsub(Rails.root.to_s + "/", "")} ..."
69
+ Cowtech::RubyOnRails::Models::Scheduler.log "\tDeleting #{log_file.gsub(Rails.root.to_s + "/", "")} ..."
70
70
  File.delete(log_file)
71
71
  end
72
72
  end
@@ -77,7 +77,7 @@ end
77
77
 
78
78
  namespace :log do
79
79
  desc "Rotates log files"
80
- task :rotate, [:email_class] => [:environment] do |task, args|
80
+ task :rotate, [:email_class, :force] => [:environment] do |task, args|
81
81
  Cowtech::RubyOnRails::LogUtils.rotate(args)
82
82
  end
83
83
 
@@ -11,8 +11,8 @@ module Cowtech
11
11
 
12
12
  def self.run_command(cmd); system(cmd) end
13
13
 
14
- def self.backup(task_args)
15
- puts "--- Backupping MongoDB ..."
14
+ def self.backup(rake_args)
15
+ Cowtech::RubyOnRails::Models::Scheduler.log "--- Backupping MongoDB ..."
16
16
 
17
17
  # Get configuration
18
18
  mongo_config = YAML.load_file(Rails.root + "config/mongoid.yml")
@@ -28,36 +28,36 @@ module Cowtech
28
28
  FileUtils.mkdir_p(dir) if !File.directory?(dir)
29
29
 
30
30
  databases.each do |db|
31
- puts "\t\tBackupping DB #{db} ..."
31
+ Cowtech::RubyOnRails::Models::Scheduler.log "\t\tBackupping DB #{db} ..."
32
32
 
33
33
  dump_cmd = "mongodump"
34
34
  dump_args = {"" => "-o \"#{dest_file}\"", "database" => "-d #{db}"}
35
35
 
36
36
  # Execute command
37
- puts "\t\tDumping data ..."
37
+ Cowtech::RubyOnRails::Models::Scheduler.log "\t\tDumping data ..."
38
38
  Cowtech::RubyOnRails::MongoUtils.run_command(dump_cmd + " " + dump_args.values.join(" "))
39
39
  end
40
40
 
41
41
  # Compress
42
- puts "\t\tCompressing backup ..."
42
+ Cowtech::RubyOnRails::Models::Scheduler.log "\t\tCompressing backup ..."
43
43
  Cowtech::RubyOnRails::MongoUtils.run_command(@@log_compressor_command + " " + final_file + " " + dest_file.to_s)
44
44
  FileUtils.rm_rf(dest_file.to_s)
45
45
 
46
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
47
+ if (Rails.env == "production" || rake_args["force"].to_boolean) && rake_args["email_class"].present? then
48
+ Cowtech::RubyOnRails::Models::Scheduler.log "\tForwarding backup file to requested email address..."
49
+ rake_args["email_class"].constantize.backup(final_file).deliver
50
50
  end
51
51
 
52
- puts "Backup saved in #{dest_file}.#{@@log_compressed_extension}"
52
+ Cowtech::RubyOnRails::Models::Scheduler.log "Backup saved in #{dest_file}.#{@@log_compressed_extension}"
53
53
  end
54
54
 
55
55
  def self.backup_clean
56
- puts "--- Cleaning database backup files ..."
56
+ Cowtech::RubyOnRails::Models::Scheduler.log "--- Cleaning database backup files ..."
57
57
 
58
58
  ["backups/mysql/*.sql", "backups/mysql/backup/*.#{@@log_compressed_extension}"].each do |path|
59
59
  Dir.glob(Rails.root + path) do |log_file|
60
- puts "\tDeleting #{log_file.gsub(Rails.root.to_s + "/", "")} ..."
60
+ Cowtech::RubyOnRails::Models::Scheduler.log "\tDeleting #{log_file.gsub(Rails.root.to_s + "/", "")} ..."
61
61
  File.delete(log_file)
62
62
  end
63
63
  end
@@ -68,7 +68,7 @@ end
68
68
 
69
69
  namespace :mongodb do
70
70
  desc "Backups MongoDB collections"
71
- task :backup, [:email_class] => [:environment] do |task, args|
71
+ task :backup, [:email_class, :force] => [:environment] do |task, args|
72
72
  Cowtech::RubyOnRails::MongoUtils.backup(args)
73
73
  end
74
74
 
@@ -12,7 +12,7 @@ module Cowtech
12
12
 
13
13
  def self.run_command(cmd); system(cmd) end
14
14
 
15
- def self.mysql_execute(config, task_args)
15
+ def self.mysql_execute(config, rake_args)
16
16
  dest_file = Rails.root + "backups/mysql/mysql-#{Time.now.strftime("%Y%m%d-%H%M%S")}.sql"
17
17
  final_file = dest_file.to_s + "." + @@log_compressed_extension
18
18
 
@@ -34,20 +34,20 @@ module Cowtech
34
34
  FileUtils.mkdir_p(dir) if !File.directory?(dir)
35
35
 
36
36
  # Execute command
37
- puts "\tDumping data ..."
37
+ Cowtech::RubyOnRails::Models::Scheduler.log "\tDumping data ..."
38
38
  Cowtech::RubyOnRails::MysqlUtils.run_command(dump_cmd + " " + dump_args.values.join(" "))
39
39
 
40
40
  # Compress
41
- puts "\tCompressing backup ..."
41
+ Cowtech::RubyOnRails::Models::Scheduler.log "\tCompressing backup ..."
42
42
  Cowtech::RubyOnRails::MysqlUtils.run_command(@@log_compressor_command + " " + dest_file.to_s)
43
43
 
44
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
45
+ if (Rails.env == "production" || rake_args["force"].to_boolean) && rake_args["email_class"].present? then
46
+ Cowtech::RubyOnRails::Models::Scheduler.log "\tForwarding backup file to requested email address..."
47
+ rake_args["email_class"].constantize.backup(final_file).deliver
48
48
  end
49
49
 
50
- puts "Backup saved in #{final_file}"
50
+ Cowtech::RubyOnRails::Models::Scheduler.log "Backup saved in #{final_file}"
51
51
  end
52
52
 
53
53
  # ALIAS
@@ -61,12 +61,12 @@ module Cowtech
61
61
  @@log_compressed_extension = "bz2"
62
62
 
63
63
  def self.to_fixtures
64
- puts "--- Dumping database into fixtures ..."
64
+ Cowtech::RubyOnRails::Models::Scheduler.log "--- Dumping database into fixtures ..."
65
65
  sql = "SELECT * FROM %s"
66
66
  skip_tables = ["schema_info"]
67
67
  ActiveRecord::Base.establish_connection
68
68
  (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
69
- puts "--- --- Dumping table #{table_name} ..."
69
+ Cowtech::RubyOnRails::Models::Scheduler.log "--- --- Dumping table #{table_name} ..."
70
70
  i = "01"
71
71
  File.open("#{RAILS_ROOT}/test/fixture/#{table_name}.yml", 'w') do |file|
72
72
  data = ActiveRecord::Base.connection.select_all(sql % table_name)
@@ -78,22 +78,22 @@ module Cowtech
78
78
  end
79
79
  end
80
80
 
81
- def self.backup(args)
82
- puts "--- Backupping database ..."
81
+ def self.backup(rake_args)
82
+ Cowtech::RubyOnRails::Models::Scheduler.log "--- Backupping database ..."
83
83
  # OTTENIAMO LA CONFIGURAZIONE
84
84
  db_config = YAML.load_file(Rails.root + "config/database.yml")
85
85
  env = Rails.env
86
86
 
87
87
  # ESEGUIAMO
88
- Cowtech::RubyOnRails::MysqlUtils.send("#{db_config[env]["adapter"]}_execute", db_config[env], args)
88
+ Cowtech::RubyOnRails::MysqlUtils.send("#{db_config[env]["adapter"]}_execute", db_config[env], rake_args)
89
89
  end
90
90
 
91
91
  def self.backup_clean
92
- puts "--- Cleaning database backup files ..."
92
+ Cowtech::RubyOnRails::Models::Scheduler.log "--- Cleaning database backup files ..."
93
93
 
94
94
  ["backups/mysql/*.sql", "backups/mysql/backup/*.#{@@log_compressed_extension}"].each do |path|
95
95
  Dir.glob(Rails.root + path) do |log_file|
96
- puts "\tDeleting #{log_file.gsub(Rails.root.to_s + "/", "")} ..."
96
+ Cowtech::RubyOnRails::Models::Scheduler.log "\tDeleting #{log_file.gsub(Rails.root.to_s + "/", "")} ..."
97
97
  File.delete(log_file)
98
98
  end
99
99
  end
@@ -109,7 +109,7 @@ namespace :mysql do
109
109
  end
110
110
 
111
111
  desc "Backups database"
112
- task :backup, [:email_class] => [:environment] do |task, args|
112
+ task :backup, [:email_class, :force] => [:environment] do |task, args|
113
113
  Cowtech::RubyOnRails::SqlUtils.backup(args)
114
114
  end
115
115
 
@@ -9,7 +9,7 @@ module Cowtech
9
9
  module Version
10
10
  MAJOR = 2
11
11
  MINOR = 5
12
- PATCH = 0
12
+ PATCH = 1
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.5.0.0
4
+ version: 2.5.1.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-17 00:00:00.000000000Z
12
+ date: 2011-12-18 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cowtech-extensions
16
- requirement: &70342270472780 !ruby/object:Gem::Requirement
16
+ requirement: &70180249711500 !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: *70342270472780
24
+ version_requirements: *70180249711500
25
25
  description: A general purpose Rails utility plugin.
26
26
  email: shogun_panda@me.com
27
27
  executables: []