Method: TestingServer.run_backup_testing_server

Defined in:
lib/testing_server/testing_server.rb

.run_backup_testing_serverObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/testing_server/testing_server.rb', line 104

def run_backup_testing_server
  Log.info('Testing server started')
  init_log4r
  $log4r.info 'Testing server started'
  all_threads = [];
  messages = Queue.new
  # used for synchronization between servers
  validation_timestamp = Time.now.to_i
  # holds boolean whether all contents according to this timestamp that should be backed are backed and OK
  is_cur_synch_ok = nil
  # holds boolean whether all already backuped files are OK
  is_backup_ok = nil
  # holds boolean whether master is valid. receive from master
  is_master_ok = nil
  # number of contents must be backuped this synch
  # with current scenarios this number expected to grow
  # used to check that file_generator is actually running
  numb_backuped_contents = 0

  all_threads << Thread.new do
    ContentServer.run_backup_server
  end

  receive_msg_proc = lambda do |message|
    $log4r.info("message received: #{message}")
    messages.push(message)
  end
  tcp = Networking::TCPClient.new(Params['content_server_hostname'],
                                  Params['testing_server_port'], receive_msg_proc)
  all_threads << tcp.tcp_thread

  # used to request index from master per validation interval of time
  all_threads << Thread.new do
    while(true) do
      sleep Params['validation_interval']
      validation_timestamp = Time.now.to_i
      tcp.send_obj([:GET_INDEX, validation_timestamp])
      $log4r.info "GET_INDEX sent with timestamp #{validation_timestamp}"
    end
  end

  # gets messages from master via queue
  # handles them according to type
  while(true) do
    msg_type, response_validation_timestamp, msg_body = messages.pop
    $log4r.info "#{msg_type} : #{validation_timestamp} : #{msg_body.class}"
    unless (validation_timestamp == response_validation_timestamp)
      # TODO problem of servers synch. What should be done here?
      $log4r.warning "Timestamps differs: requested #{validation_timestamp} : received #{response_validation_timestamp}"
    end

    case msg_type
    when :PUT_INDEX
      # index of already backuped contents
      backuped_index = ContentData::ContentData.new
      backuped_index.from_file Params['local_content_data_path']
      # index contains only master current contents
      # that must be backuped according to backup_time_requirement parameter
      index_must_be_backuped = ContentData::ContentData.new(msg_body)
      # we backup contents, so content mtime used to determine contents should be validated
      index_must_be_backuped.each_content do |checksum, size, mtime|
        if (validation_timestamp - mtime < Params['backup_time_requirement'])
          index_must_be_backuped.remove_content checksum
        end
      end
      is_cur_synch_ok =
        Validations::IndexValidations.validate_remote_index index_must_be_backuped, backuped_index
      is_backup_ok = backuped_index.validate
      numb_backuped_contents = index_must_be_backuped.contents_size
    when :PUT_VALIDATION
      is_master_ok = msg_body
    else
      $log4r.error "Incorrect message received: #{msg_type}"
    end

    unless (is_master_ok.nil? || is_cur_synch_ok.nil? || is_backup_ok.nil?)
      send_email is_master_ok, is_cur_synch_ok, is_backup_ok, numb_backuped_contents
      is_master_ok = nil
      is_cur_synch_ok = nil
      is_backup_ok = nil
      numb_backuped_contents = 0
    end
  end
end