Class: CouchReplica::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_replica/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, source_endpoint:, target_endpoint:, replicator_url:) ⇒ Database

Returns a new instance of Database.



11
12
13
14
15
16
# File 'lib/couch_replica/database.rb', line 11

def initialize(name, source_endpoint:, target_endpoint:, replicator_url:)
  @name = name
  @source_endpoint = source_endpoint
  @target_endpoint = target_endpoint
  @replicator_url = replicator_url
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/couch_replica/database.rb', line 9

def name
  @name
end

#replicator_urlObject (readonly)

Returns the value of attribute replicator_url.



9
10
11
# File 'lib/couch_replica/database.rb', line 9

def replicator_url
  @replicator_url
end

#source_endpointObject (readonly)

Returns the value of attribute source_endpoint.



9
10
11
# File 'lib/couch_replica/database.rb', line 9

def source_endpoint
  @source_endpoint
end

#target_endpointObject (readonly)

Returns the value of attribute target_endpoint.



9
10
11
# File 'lib/couch_replica/database.rb', line 9

def target_endpoint
  @target_endpoint
end

Instance Method Details

#doc_urlObject



57
58
59
# File 'lib/couch_replica/database.rb', line 57

def doc_url
  "#{replicator_url}/#{replicator_id}"
end

#loggerObject



73
74
75
# File 'lib/couch_replica/database.rb', line 73

def logger
  @logger ||= Logger.new(STDOUT)
end

#replicator_idObject



61
62
63
# File 'lib/couch_replica/database.rb', line 61

def replicator_id
  "pull-#{name}"
end

#source_urlObject



65
66
67
# File 'lib/couch_replica/database.rb', line 65

def source_url
  "#{source_endpoint}/#{name}"
end

#startObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/couch_replica/database.rb', line 18

def start
  doc = status
  if doc
    logger.info "Replication already started from #{source_url} to #{target_url}"
    return
  end
  
  res = RestClient.post replicator_url, {
    _id: replicator_id,
    source: source_url,
    target: target_url,
    continuous: true,
    create_target: true
  }.to_json, content_type: :json
  
  logger.info "Started push replication from #{source_url} to #{target_url}"
  
  res
end

#statusObject



51
52
53
54
55
# File 'lib/couch_replica/database.rb', line 51

def status
  JSON.parse RestClient.get doc_url
rescue RestClient::ResourceNotFound
  nil
end

#stopObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/couch_replica/database.rb', line 38

def stop
  doc = status
  
  if !doc
    logger.info "No replication found from #{source_url} to #{target_url}"
    return
  end
  
  res = RestClient.delete doc_url, params: {rev: doc['_rev']}
  logger.info "Stopped replication from #{source_url} to #{target_url}"
  res
end

#target_urlObject



69
70
71
# File 'lib/couch_replica/database.rb', line 69

def target_url
  "#{target_endpoint}/#{name}"
end