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.



31
32
33
# File 'lib/spider-gazelle.rb', line 31

def args
  @args
end

#passwordObject (readonly)

Returns the value of attribute password.



31
32
33
# File 'lib/spider-gazelle.rb', line 31

def password
  @password
end

Instance Method Details

#exec(args) ⇒ Object



34
35
36
37
38
# File 'lib/spider-gazelle.rb', line 34

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

#launch(options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/spider-gazelle.rb', line 40

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




74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/spider-gazelle.rb', line 74

def launch_spider(args)
    require 'securerandom'

    @password ||= SecureRandom.hex

    #cmd = "#{EXEC_NAME} -s #{@password} #{Shellwords.join(args)}"

    thread = Reactor.instance.thread
    spider = thread.spawn(EXEC_NAME, args: (['-s', @password] + args), mode: :inherit)
    spider.finally do
        signaller = ::SpiderGazelle::Signaller.instance
        signaller.panic!('Unexpected spider exit') unless signaller.shutting_down
    end
end

#shutdownObject



63
64
65
66
67
68
# File 'lib/spider-gazelle.rb', line 63

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

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


TTY SIGNALLING CONTROL




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

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_spider(signaller, logger, options) ⇒ Object

This is called when a spider process starts



90
91
92
93
# File 'lib/spider-gazelle.rb', line 90

def start_spider(signaller, logger, options)
    require 'spider-gazelle/spider'
    Spider.instance.run!(options)
end