Method: ContentServer.run_backup_server

Defined in:
lib/content_server/backup_server.rb

.run_backup_serverObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
# File 'lib/content_server/backup_server.rb', line 30

def run_backup_server
  Log.info('Start backup server')
  Thread.abort_on_exception = true
  all_threads = []

  # create general tmp dir
  FileUtils.mkdir_p(Params['tmp_path']) unless File.directory?(Params['tmp_path'])
  # init tmp content data file
  $tmp_content_data_file = File.join(Params['tmp_path'], 'backup.data')

  if Params['enable_monitoring']
    Log.info("Initializing monitoring of process params on port:%s", Params['process_monitoring_web_port'])
    $process_vars.set('server_name', 'backup_server')
  end

  # # # # # # # # # # # #
  # Initialize/start monitoring and destination folder
  Params['backup_destination_folder'][0]['path']=File.expand_path(Params['backup_destination_folder'][0]['path'])
  Log.info("backup_destination_folder is:%s", Params['backup_destination_folder'][0]['path'])
  #adding destination folder to monitoring paths
  Params['monitoring_paths'] << Params['backup_destination_folder'][0]
  Log.info('Start monitoring following directories:')
  Params['monitoring_paths'].each { |path|
    Log.info("  Path:'%s'", path['path'])
  }

  # initial global local content data object
  $local_content_data_lock = Mutex.new
  $local_content_data = ContentData::ContentData.new
  $last_content_data_id = $local_content_data.unique_id

  # Read here for initial content data that exist from previous system run
  content_data_path = Params['local_content_data_path']
  if File.exists?(content_data_path) and !File.directory?(content_data_path)
    Log.info("reading initial content data that exist from previous system run from file:%s", content_data_path)
    $local_content_data.from_file(content_data_path)
    $last_content_data_id = $local_content_data.unique_id
  else
    if File.directory?(content_data_path)
      raise("Param:'local_content_data_path':'%s' cannot be a directory name", Params['local_content_data_path'])
    end
    # create directory if needed
    dir = File.dirname(Params['local_content_data_path'])
    FileUtils.mkdir_p(dir) unless File.exists?(dir)
  end

  Log.info("Init monitoring")
  fm = FileMonitoring::FileMonitoring.new()

  # Start monitoring and writing changes to queue
  all_threads << Thread.new do
    fm.monitor_files
  end

  # # # # # # # # # # # # # # # # # # # # # # # #
  # thread: Start dump local content data to file
  Log.debug1('Init thread: flush local content data to file')
  all_threads << Thread.new do
    FileUtils.mkdir_p(Params['tmp_path']) unless File.directory?(Params['tmp_path'])
    loop{
      sleep(Params['data_flush_delay'])
      ContentServer.flush_content_data
    }
  end

  #initialize global - remote content data
  $remote_content_data_lock = Mutex.new
  $remote_content_data = ContentData::ContentData.new
  remote_content = ContentServer::RemoteContentClient.new(Params['content_server_hostname'],
                                                          Params['content_server_data_port'],
                                                          Params['backup_destination_folder'][0]['path'])
  all_threads.concat(remote_content.run())

  file_copy_client = FileCopyClient.new(Params['content_server_hostname'],
                                        Params['content_server_files_port'])
  all_threads.concat(file_copy_client.threads)

  # Each
  Log.info('Start remote and local contents comparator')
  all_threads << Thread.new do
    loop do
      sleep(Params['backup_check_delay'])
      $local_content_data_lock.synchronize{
        $remote_content_data_lock.synchronize{
          diff = ContentData.remove($local_content_data, $remote_content_data)
          unless diff.nil? || diff.empty?
            Log.debug2("Backup content:\n%s", $local_content_data)
            Log.debug2("Remote content:\n%s", $remote_content_data)
            Log.debug2("Missing contents:\n%s", diff)
            Log.info('Start sync check. Backup and remote contents need a sync, requesting copy files:')
            file_copy_client.request_copy(diff)
          else
            Log.info("Start sync check. Local and remote contents are equal. No sync required.")
          end
          diff = ContentData::ContentData.new  # to clear the memory
        }
      }
    end
  end

  # # # # # # # # # # # # # # # # # # # # # # # #
  # Start process vars thread
  if Params['enable_monitoring']
    monitoring_info = MonitoringInfo::MonitoringInfo.new()
    all_threads << Thread.new do
      ContentServer.monitor_general_process_vars
    end
  end

  all_threads.each { |t| t.abort_on_exception = true }
  all_threads.each { |t| t.join }
  # Should never reach this line.
end