Class: Boatman

Inherits:
Object
  • Object
show all
Defined in:
lib/boatman.rb,
lib/boatman/copyable.rb,
lib/boatman/monitored_file.rb,
lib/boatman/monitored_directory.rb

Defined Under Namespace

Modules: Copyable Classes: MonitoredDirectory, MonitoredFile

Class Method Summary collapse

Class Method Details

.load(args) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/boatman.rb', line 23

def self.load(args)
  if(args.size < 1 || args.size > 2)
    Boatman.print_usage
    exit(0)
  end

  puts "Logging to boatman.log"
  require 'logger'
  Boatman.logger = Logger.new("boatman.log")
  logger.level = Logger::INFO

  @config_file = args[0]

  @working_directory = args[1] || "."
  Boatman.logger.info "Working directory: #{@working_directory}"

  load_config_file(args[0])
end

.load_config_file(file) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/boatman.rb', line 42

def self.load_config_file(file)
  Boatman.logger.info  "Loading Config File: #{@config_file}"
  config = YAML.load_file(@config_file)

  @task_files = config["tasks"]

  config["directories"].each do |key, value|
    Object.class_eval do
      define_method(key) do
        return value
      end
    end
  end
end


57
58
59
# File 'lib/boatman.rb', line 57

def self.print_usage
  puts "Usage: boatman <config file> [<working directory>]"
end

.runObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/boatman.rb', line 61

def self.run
  @task_files.each do |task_file|
    require "#{@working_directory}/#{task_file}"
    puts "Added task #{task_file}"
  end

  interrupted = false
  trap("INT") { interrupted = true }
  
  while true do
    if interrupted
      puts "Exiting from boatman"
      return
    end

    tasks.each do |task|
      if task[:last_run].nil? || Time.now - task[:last_run] > task[:time_interval]
        begin
          # do everything in the context of the working directory
          puts "Going to check #{task[:directory].path} at #{Time.now}" if $DEBUG
          Dir.chdir(@working_directory) do
            task[:directory].instance_eval &task[:block]
          end
          puts "Leaving #{task[:directory].path} at #{Time.now}" if $DEBUG
        rescue Exception => e
          Boatman.logger.error "Task monitoring #{task[:directory].path} had an error: #{e.message}" rescue nil
          Boatman.logger.error "Backtrace: #{e.backtrace.join("\n")}" if $DEBUG

          # clear the FTP connection cache for good luck
          FTPUtils::FTPConnection.clear_connection_cache
        end

        task[:last_run] = Time.now
      end
    end
    
    sleep 1
  end
end