Class: Heliosphere::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/heliosphere/server.rb

Constant Summary collapse

POLL_INTERVAL =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port) ⇒ Server

Returns a new instance of Server.



11
12
13
# File 'lib/heliosphere/server.rb', line 11

def initialize(port)
  @port = port
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



9
10
11
# File 'lib/heliosphere/server.rb', line 9

def port
  @port
end

Instance Method Details

#clear_indexObject



44
45
46
47
48
# File 'lib/heliosphere/server.rb', line 44

def clear_index
  Heliosphere.indexer.models.each do |model|
    model.remove_all_from_index
  end
end

#down?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/heliosphere/server.rb', line 54

def down?
  not up?
end

#reindexObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/heliosphere/server.rb', line 31

def reindex
  wait(&:up?)
  clear_index

  Heliosphere.indexer.models.each do |model|
    model.all.each do |record|
      Sunspot.index(record)
    end
  end

  Sunspot.commit
end

#startObject



15
16
17
18
19
# File 'lib/heliosphere/server.rb', line 15

def start
  Sunspot::Rails::Server.new.start if down?
  wait(&:up?)
  sleep POLL_INTERVAL
end

#start_and_reindexObject



26
27
28
29
# File 'lib/heliosphere/server.rb', line 26

def start_and_reindex
  start
  reindex
end

#stopObject



21
22
23
24
# File 'lib/heliosphere/server.rb', line 21

def stop
  Sunspot::Rails::Server.new.stop if up?
  wait(&:down?)
end

#up?Boolean

Returns:

  • (Boolean)


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

def up?
  `netstat -an` =~ /\b#{port}\b/m
end

#wait(&block) ⇒ Object



58
59
60
# File 'lib/heliosphere/server.rb', line 58

def wait(&block)
  sleep POLL_INTERVAL until yield(self)
end