Class: DCA::Commands::Area

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/dca/commands/area.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



6
7
8
# File 'lib/dca/commands/area.rb', line 6

def self.source_root
  File.expand_path('../templates', __FILE__)
end

Instance Method Details

#create(name, title, description, url) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dca/commands/area.rb', line 15

def create name, title, description, url
  @name = name
  @class_name = name.camelize

  template 'area/analyzer.rb.erb', "lib/areas/#{name.downcase}/analyzer.rb"
  template 'area/position.rb.erb', "lib/areas/#{name.downcase}/models/position.rb"
  template 'area/page.rb.erb', "lib/areas/#{name.downcase}/models/page.rb"
  template 'area/models.rb.erb', "lib/areas/#{name.downcase}/models.rb"
  template 'area/area.rb.erb', "lib/areas/#{name.downcase}.rb"

  template 'spec/analyzer_spec.rb.erb', "spec/areas/#{name.downcase}/analyzer_spec.rb"
  template 'spec/spec_helper.rb.erb', "spec/areas/#{name.downcase}/spec_helper.rb"

  empty_directory 'config'
  area_file = 'config/areas.yml'
  areas = {}
  areas = YAML.load_file(area_file) if File.exist? area_file
  area_hash = {'title' => title, 'description' => description, 'url' => url}
  if areas.has_key? name
    if areas[name] == area_hash
      shell.say_status :identical, area_file, :blue
    else
      areas[name] = area_hash
      shell.say_status :conflict, area_file, :red
      if shell.file_collision(area_file) { areas.to_yaml }
        open(area_file, 'w:utf-8') { |file| file.write areas.to_yaml }
        shell.say_status :force, area_file, :yellow
      end
    end
  else
    status = areas.empty? ? :create : :update
    areas[name] = area_hash
    open(area_file, 'w:utf-8') { |file| file.write areas.to_yaml }
    shell.say_status status, area_file, :green
  end
end

#start(name) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dca/commands/area.rb', line 53

def start name
  shell.say "Starting analyze area #{name}"
  config = area_config name.to_sym

  job_ops = {}
  job_ops[:distributed] = true if config[:distributed]
  job = "#{DCA.project_name}::Areas::#{name}::AnalyzerJob".constantize
  job.create job_ops

  background = config[:background].nil? ? true : config[:background]
  run_worker name, config[:workers] || 1, background
end

#stop(name) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dca/commands/area.rb', line 68

def stop name
  shell.say "Stopping analyze area #{name}"

  pids = workers_pids name
  unless pids.empty?
    system("kill -s #{options[:force] ? 'TERM' : 'QUIT'} #{pids.join(' ')}")
  end

  wait_worker name

  Resque.remove_queue name
end