Class: FileProcessingJob::Server::Connection

Inherits:
EM::Connection
  • Object
show all
Defined in:
lib/fpj/server.rb

Constant Summary collapse

@@q =
EM::Queue.new()

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.push(message) ⇒ Object



130
131
132
# File 'lib/fpj/server.rb', line 130

def self.push(message)
  @@q.push(message)
end

Instance Method Details

#bindObject



176
177
178
# File 'lib/fpj/server.rb', line 176

def bind
  logger.info "client connected to the server"
end

#configObject



134
135
136
# File 'lib/fpj/server.rb', line 134

def config 
  @config ||= FileProcessingJob::Server.config
end

#loggerObject



138
139
140
# File 'lib/fpj/server.rb', line 138

def logger
  FileProcessingJob.logger
end

#receive_data(data) ⇒ Object

inbound client connection



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/fpj/server.rb', line 143

def receive_data data
  data = data.strip
  case (data)
  when "next"
    @@q.pop {|filename| send_data filename }
  when /^get (.*)$/
    filename = $1
    streamer = EventMachine::FileStreamer.new(self, File.join(self.config.inbox_directory, filename))
    streamer.callback{
      FileUtils.move(File.join(self.config.inbox_directory, filename), File.join(self.config.processing_directory, filename))
    }
    # streamer.error {
    #   logger.error "Unable to stream file #{filename} to client. The file can be found in the 'error' directory"
    #   FileUtils.move(File.join(self.config.inbox_directory, filename), File.join(self.config.error_directory, filename))
    # }
  when /^processed (.*)$/
    filename = $1
    if (File.exists?(File.join(self.config.processed_directory, filename)))
      logger.error "file #{filename} already exists in processed directory"
    else
      FileUtils.move(File.join(self.config.processing_directory, filename), File.join(self.config.processed_directory, filename))
      logger.debug "finished processing #{filename}"
    end
    send_data "ack"
  when /^error (.*)$/
    filename = $1
    FileUtils.move(File.join(self.config.processing_directory, filename), File.join(self.config.error_directory, filename))
    logger.error "Unable to stream file #{filename} to client. The file can be found in the 'error' directory"
  when "close"
    close_connection
  end
end

#unbindObject



180
181
182
# File 'lib/fpj/server.rb', line 180

def unbind
  logger.info "client disconnected from the server"
end