-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.js
79 lines (71 loc) · 2.4 KB
/
sync.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
var Pouch = require('pouchdb')
var _ = require('underscore')
var Backbone = require('backbone')
var sys = require('sys')
var exec = require('child_process').exec;
var moment = require('moment')
var $ = require('jquery')
var syncServer = 'http://pi:raspberry@sync.local:5984/'
var bellServer = 'http://pi:raspberry@raspberrypi.local:5984/'
var db = new Pouch(syncServer + 'replicator')
var replications = new Backbone.Collection();
replications.replicated = 0
replications.on('done', function() {
console.log('done')
// Get the facilityId, save an Action document, and then shutdown the system
Pouch(bellServer + 'whoami').get('facility', function(err, doc) {
var facilityId = doc.facilityId
// @todo we should think about the schema for memberId.
// Might want source/sourceType and target/targetType properties
var action = {
memberId: "syncBell",
kind: 'Action',
action: 'synced',
objectId: facilityId,
timestamp: moment.utc().unix(),
context: 'syncBell',
facilityId: facilityId
}
Pouch(bellServer + 'actions').post(action, function(err, response) {
console.log('Sync has been logged. Going for shutdown')
exec('poweroff')
})
})
})
replications.on('ready', function() {
console.log('ready')
_.each(replications.models, function(model) {
Pouch.replicate(model.get('source'), model.get('target'), {
complete: function(err, response) {
if(err) {
console.log("Replication " + (replications.replicated + 1) + ":")
console.log(err)
}
console.log("Replication " + (replications.replicated + 1) + ":")
console.log(response)
replications.replicated++
if(replications.replicated == replications.models.length) {
replications.trigger('done')
}
}
})
})
})
// Get replications so it will trigger ready event
console.log('Looking for Bell Server')
$.getJSON(bellServer, function() {
console.log('Found Bell Server! :-)')
console.log('fetching all docs in replicator')
db.allDocs({include_docs: true}, function(err, response) {
console.log('received all docs in replicator')
_.each(response.rows, function(row) {
if(row.doc.kind == 'Replicate') {
replications.models.push(new Backbone.Model(row.doc))
}
})
replications.trigger('ready')
});
})
.fail(function() {
console.log('Bell Server not found :-(')
})