Class: SpiderGazelle::LaunchControl

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



37
38
39
# File 'lib/spider-gazelle.rb', line 37

def args
  @args
end

#passwordObject (readonly)

Returns the value of attribute password.



37
38
39
# File 'lib/spider-gazelle.rb', line 37

def password
  @password
end

Instance Method Details

#exec(args) ⇒ Object



40
41
42
43
44
# File 'lib/spider-gazelle.rb', line 40

def exec(args)
    options = SpiderGazelle::Options.sanitize(args)
    @args = args
    launch(options)
end

#launch(options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/spider-gazelle.rb', line 46

def launch(options)
    # Enable verbose messages if requested
    Logger.instance.verbose! if options[0][:verbose]

    # Start the Libuv Event Loop
    reactor = ::SpiderGazelle::Reactor.instance
    reactor.run do

        # Check if SG is already running
        signaller = ::SpiderGazelle::Signaller.instance

        if options[0][:isolate]
            # This ensures this process will load the spider code
            options[0][:spider] = true
            boot(true, signaller, options)
        else
            signaller.check.then do |running|
                boot(running, signaller, options)
            end
        end
    end
end

#launch_spider(args) ⇒ Object


SPIDER LAUNCH CONTROL




80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/spider-gazelle.rb', line 80

def launch_spider(args)
    require 'securerandom'
    require 'thread'

    @password ||= SecureRandom.hex
    #cmd = "#{EXEC_NAME} -s #{@password} #{Shellwords.join(args)}"
    cmd = [EXEC_NAME, '-s', @password] + args

    Thread.new do
        result = system(*cmd)

        # TODO:: We need to detect a failed load
        # This is a little more tricky as spiders
        # may come and go without this process exiting
    end
end

#shutdownObject



69
70
71
72
73
74
# File 'lib/spider-gazelle.rb', line 69

def shutdown
    reactor = Reactor.instance
    reactor.thread.schedule do
        reactor.shutdown
    end
end

#signal_master(reactor, signaller, logger, options) ⇒ Object


TTY SIGNALLING CONTROL




121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/spider-gazelle.rb', line 121

def signal_master(reactor, signaller, logger, options)
    # This is a signal request
    promise = signaller.request(options)

    promise.then do |result|
        logger.info "signal recieved #{result}"
    end
    promise.catch do |error|
        logger.info "there was an error #{error}"
    end
    promise.finally do
        reactor.shutdown
    end
end

#start_gazelle(signaller, logger, options) ⇒ Object


GAZELLE LAUNCH CONTROL




110
111
112
113
114
115
# File 'lib/spider-gazelle.rb', line 110

def start_gazelle(signaller, logger, options)
    logger.set_client signaller.pipe

    require 'spider-gazelle/gazelle'
    ::SpiderGazelle::Gazelle.new(logger.thread, :process).run!(options)
end

#start_spider(signaller, logger, options) ⇒ Object

This is called when a spider process starts



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

def start_spider(signaller, logger, options)
    logger.set_client signaller.pipe unless options[0][:isolate]

    require 'spider-gazelle/spider'
    Spider.instance.run!(options)
end