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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/boatman.rb', line 16

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/boatman.rb', line 35

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


50
51
52
# File 'lib/boatman.rb', line 50

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

.runObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/boatman.rb', line 54

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]
        # do everything in the context of the working directory

        Dir.chdir(@working_directory) do
          task[:directory].instance_eval &task[:block]
        end

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