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.



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

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



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

def datastore
  @datastore
end

#event_socketObject (readonly)

The PUB socket on which resource events are published



137
138
139
# File 'lib/thingfish/handler.rb', line 137

def event_socket
  @event_socket
end

#metastoreObject (readonly)

The metastore



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

def metastore
  @metastore
end

Class Method Details

.configure(config = nil) ⇒ Object

Configurability API – install the configuration



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

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.plugin( :filters ) # pre-load the filters plugin for deferred config

  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.



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

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.



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

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.



141
142
143
144
# File 'lib/thingfish/handler.rb', line 141

def run
  self.setup_event_socket
  super
end

#setup_event_socketObject

Set up the event socket.



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

def setup_event_socket
  if self.class.event_socket_uri && ! @event_socket
    @event_socket = CZTop::Socket::PUB.new
    @event_socket.options.linger = 0
    @event_socket.bind( self.class.event_socket_uri )
  end
end

#shutdownObject

Shutdown handler hook.



158
159
160
161
# File 'lib/thingfish/handler.rb', line 158

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