Sha256: 6d8d1a3003caae35099175123e319002ae0dc2642b688f950beb499c791d59c7

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

#!/bin/env ruby

require 'websocket-eventmachine-client'
require 'json'
require 'jsondiff'


class Producer

	@producers_by_regexp = {}
	@patterns = []

	def self.patterns
		@patterns
	end


	def self.init
		@producers_by_regexp = Hash[*ObjectSpace.each_object(Class).select { |klass| klass < Producer }.map { |klass| klass.patterns.map { |pattern| [Regexp.new(Regexp.escape(pattern).gsub('\*','.*?')), klass] }}.flatten]
		@patterns = @producers_by_regexp.values.map { |klass| klass.patterns }.flatten
	end
	

	def self.for(object_id)
		@producers_by_regexp.each_pair.find { |pattern,generator| object_id =~ pattern }[1]
	end
	
end

($LOAD_PATH+['./lib']).each { |load_path|
	Dir[load_path+'/seapigs/*.rb'].each { |f| require f }
}

Producer.init


EM.run {

	socket = WebSocket::EventMachine::Client.connect(uri: ARGV[0])

	
	socket.onopen {
		socket.send(JSON.dump(action: 'client-options-set', options: {name: 'worker'}))
		Producer.patterns.each { |pattern|
			socket.send JSON.dump(action: 'object-producer-register', pattern: pattern)
		}
	}

	
	socket.onmessage { |message|
		message = JSON.load message
		case message['action']
		when 'object-produce'
			start = Time.new
			object, version = Producer.for(object_id = message['id']).produce(object_id)
			puts 'Generated %25s in %5.2fs'%[object_id, (Time.new-start).to_f]
			socket.send JSON.dump(action: 'object-patch', id: object_id, old_version: 0, new_version: version, value: object)
		end
	}

	
	socket.onclose { |code, reason|
		EM.stop
	}

}

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
seapig-client-0.0.4 bin/seapig-worker
seapig-client-0.0.3 bin/seapig-worker
seapig-client-0.0.2 bin/seapig-worker