Class: Thingfish::Handler

Inherits:
Strelka::App
  • Object
show all
Extended by:
Configurability, Loggability, Strelka::MethodUtilities
Defined in:
lib/thingfish/handler.rb

Overview

Network-accessable datastore service

Constant Summary collapse

ID =

Strelka App ID

'thingfish'
CONFIG_DEFAULTS =

Configurability API – set config defaults

{
  datastore: 'memory',
  metastore: 'memory',
  processors: [],
  event_socket_uri: 'tcp://127.0.0.1:3475',
}
OPERATIONAL_METADATA_KEYS =

Metadata keys which aren’t directly modifiable via the REST API :TODO: Consider making either all of these or a subset of them

be immutable.
%w[
  format
  extent
  created
  uploadaddress
]
%w[
  relationship
  format
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHandler

Set up the metastore, datastore, and event socket when the handler is created.



115
116
117
118
119
120
121
# File 'lib/thingfish/handler.rb', line 115

def initialize( * ) # :notnew:
  super

  @datastore = Thingfish::Datastore.create( self.class.datastore )
  @metastore = Thingfish::Metastore.create( self.class.metastore )
  @event_socket = nil
end

Instance Attribute Details

#datastoreObject (readonly)

The datastore



129
130
131
# File 'lib/thingfish/handler.rb', line 129

def datastore
  @datastore
end

#event_socketObject (readonly)

The PUB socket on which resource events are published



135
136
137
# File 'lib/thingfish/handler.rb', line 135

def event_socket
  @event_socket
end

#metastoreObject (readonly)

The metastore



132
133
134
# File 'lib/thingfish/handler.rb', line 132

def metastore
  @metastore
end

Class Method Details

.configure(config = nil) ⇒ Object

Configurability API – install the configuration



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/thingfish/handler.rb', line 78

def self::configure( config=nil )
  config = self.defaults.merge( config || {} )

  self.datastore        = config[:datastore]
  self.metastore        = config[:metastore]
  self.event_socket_uri = config[:event_socket_uri]

  self.processors = self.load_processors( config[:processors] )
  self.processors.each do |processor|
    self.filter( :request, &processor.method(:process_request) )
    self.filter( :response, &processor.method(:process_response) )
  end
end

.load_processors(processor_list) ⇒ Object

Load the Thingfish::Processors in the given processor_list and return an instance of each one.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/thingfish/handler.rb', line 95

def self::load_processors( processor_list )
  self.log.info "Loading processors"
  processors = []

  processor_list.each do |processor_type|
    begin
      processors << Thingfish::Processor.create( processor_type )
      self.log.debug "  loaded %s: %p" % [ processor_type, processors.last ]
    rescue LoadError => err
      self.log.error "%p: %s while loading the %s processor" %
        [ err.class, err.message, processor_type ]
    end
  end

  return processors
end

Instance Method Details

#restartObject

Restart handler hook.



163
164
165
166
167
168
169
170
171
# File 'lib/thingfish/handler.rb', line 163

def restart
  if self.event_socket
    oldsock = @event_socket
    @event_socket = @event_socket.dup
    oldsock.close
  end

  super
end

#runObject

Run the handler – overridden to set up the event socket on startup.



139
140
141
142
# File 'lib/thingfish/handler.rb', line 139

def run
  self.setup_event_socket
  super
end

#setup_event_socketObject

Set up the event socket.



146
147
148
149
150
151
152
# File 'lib/thingfish/handler.rb', line 146

def setup_event_socket
  if self.class.event_socket_uri && ! @event_socket
    @event_socket = Mongrel2.zmq_context.socket( :PUB )
    @event_socket.linger = 0
    @event_socket.bind( self.class.event_socket_uri )
  end
end

#shutdownObject

Shutdown handler hook.



156
157
158
159
# File 'lib/thingfish/handler.rb', line 156

def shutdown
  self.event_socket.close if self.event_socket
  super
end

#thingfishObject

Configurability API



62
# File 'lib/thingfish/handler.rb', line 62

config_key :thingfish