-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (72 loc) · 2.55 KB
/
index.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
require('dotenv').load();
var events = require('events');
var eventEmitter = new events.EventEmitter();
var sync = require('./lib').sync;
var co = require('co'),
exec = require('mz/child_process').exec,
koa = require('koa'),
app = koa(),
musaDB = require('./models').sequelize;
/**
* Kleio-TMS Synchronization schema
* CONSTRAINTS:
* 1. Objects in the TMS Database system are analogous to Artifacts in the Kleio system
* 2. Artifacts cannot be added by a Kleio Administrator using the Kleio Admininstrator Panel
* 3. Objects and Artifacts will be compared given their ObjectNumber, we will assume that this ObjectNumber is unique per TMS Object
*
* HOW IT WORKS
* 1. If an Object ir removed from the TMS Database, the pertaining Artifact will be removed from the Kleio Database
* 2. If an Object is updated in the TMS Database, the pertaining Artifact will be updated in the Kleio Database
* 3. New Objects in the TMS Database will be added to the Kleio Database
*
* @type {number}
*/
/**
* CONSTRAINTS FOR IMAGE MOVING
* 1. Images cannot contain any whitespaces in their names
* 2. Images for the same object number will be replaced on the synchronization
*/
var counter = 0;
eventEmitter.on('sync', function(counter){
co(function * (){
//Turn off the API server
//yield exec('pm2 stop server');
var oldDir = process.env.ORIGINAL_PATH;
var newDir = process.env.ARTIFACT_IMAGE_PATH;
var Synchronization = musaDB.models.Synchronization;
//Sync the image folder
yield exec('rsync -a ' +oldDir+ ' ' +newDir+ '');
//Get last sync date
var lastUpdate = yield Synchronization.findAll({
order : '"lastSynchronization" DESC',
limit : 1
});
//Sync the database
yield sync(lastUpdate.lastSynchronization);
//Log the synchronization
yield musaDB.transaction(function(t) {
return Synchronization.create({
lastSynchronization: Date.now()
}, { transaction : t});
});
//Turn API back up
}).catch(function(err){
console.error(err);
});
});
setInterval(function(){
counter = counter + 1;
if(counter == 30){
//It's time for that sync
// eventEmitter.emit('sync', counter);
counter = 0;
}
}, 1000 * 60 * 60 * 24); //This must run every day (1000 * 60 * 60 * 24)
app.use(function*(){
if(this.url === '/server/reset'){
counter = 0;
eventEmitter.emit('sync');
}
});
app.listen(3000);
console.log("Server listening on port 3000");