Class: SpiderGazelle::Spider

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/spider-gazelle/spider.rb,
lib/spider-gazelle/spider/http1.rb,
lib/spider-gazelle/spider/binding.rb,
lib/spider-gazelle/spider/app_store.rb

Defined Under Namespace

Modules: AppStore Classes: Binding, Http1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSpider

Returns a new instance of Spider.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/spider-gazelle/spider.rb', line 28

def initialize
    @logger = Logger.instance
    @signaller = Signaller.instance
    @thread = @signaller.thread

    # Gazelle pipe connection management
    @gazelles = {
        # thread: [],
        # inline: gazelle_instance
    }
    @counts = {
        # process: number
        # thread: number
    }
    @loading = {}     # mode => load defer
    @bindings = {}    # port => binding
    @iterators = {}   # mode => gazelle round robin iterator
    @iterator_source = {}   # mode => gazelle thread array (iterator source)

    @running = true
    @loaded = false
    @bound = false
    @delay_port_binding = false

    @load_complete = @thread.defer
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



25
26
27
# File 'lib/spider-gazelle/spider.rb', line 25

def logger
  @logger
end

#threadsObject (readonly)

Returns the value of attribute threads.



25
26
27
# File 'lib/spider-gazelle/spider.rb', line 25

def threads
  @threads
end

Instance Method Details

#bind_application_portsObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/spider-gazelle/spider.rb', line 115

def bind_application_ports
    if @delay_port_binding && !@loaded
        loaded.finally { bind_application_ports }
        return
    end
    @bound = true

    @options.each do |options|
        @logger.verbose { "Loading rackup #{options}" }
        iterator = @iterators[options[:mode]]

        binding = @bindings[options[:port]] = Binding.new(iterator, options)
        binding.bind
    end
end

#delay_port_bindingObject



131
132
133
# File 'lib/spider-gazelle/spider.rb', line 131

def delay_port_binding
    @delay_port_binding = true
end

#in_mode?(mode) ⇒ Boolean

Applications can query the availability of various modes to share resources

Returns:

  • (Boolean)


21
22
23
# File 'lib/spider-gazelle/spider.rb', line 21

def in_mode?(mode)
    !!@loading[mode.to_sym]
end

#loadObject

Load a second application (requires a new port binding)



98
99
100
# File 'lib/spider-gazelle/spider.rb', line 98

def load

end

#loadedObject

This allows applications to recieve a callback once the server has completed loading the applications. Port binding is in progress



16
17
18
# File 'lib/spider-gazelle/spider.rb', line 16

def loaded
    @load_complete.promise
end

#readyObject

Load gazelles and make the required bindings



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/spider-gazelle/spider.rb', line 66

def ready
    load_promise = load_applications
    load_promise.then do
        # Check a shutdown request didn't occur as we were loading
        if @running
            @logger.verbose "All gazelles running"

            # This happends on the master thread so we don't need to check
            # for the shutdown events here
            @loaded = true
            bind_application_ports unless @delay_port_binding
        else
            @logger.warn "A shutdown event occured while loading"
            perform_shutdown
        end
    end

    # Provide applications with a load complete callback
    @load_complete.resolve(load_promise)
end

#run!(options) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/spider-gazelle/spider.rb', line 55

def run!(options)
    @options = options
    @logger.verbose { "Spider Pid: #{Process.pid} started" }
    if options[0][:isolate]
        ready
    else
        @signaller.authenticate(options[0][:spider])
    end
end

#shutdown(finished) ⇒ Object

Shutdown after current requests have completed



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/spider-gazelle/spider.rb', line 103

def shutdown(finished)
    @shutdown_defer = finished

    @logger.verbose { "Spider Pid: #{Process.pid} shutting down (loaded #{@loaded})" }

    if @loaded
        perform_shutdown
    else
        @running = false
    end
end

#updateObject

Pass existing bindings to the master process



93
94
95
# File 'lib/spider-gazelle/spider.rb', line 93

def update

end

#waitObject

Load gazelles and wait for the bindings to be sent



88
89
90
# File 'lib/spider-gazelle/spider.rb', line 88

def wait

end