-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetadataSync.js
More file actions
72 lines (65 loc) · 1.92 KB
/
MetadataSync.js
File metadata and controls
72 lines (65 loc) · 1.92 KB
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
import Logging from './lib/logging'
import {Meteor} from 'meteor/meteor'
import isArray from 'lodash/isArray'
const DEFAULT_INTERVAL = 30
// used to manage a regular interval sync between Salesforce metadata and a local
// Mongo collection
// the records in the collection will be indexed using the fullName property
class MetadataSync {
constructor(syncforce, collection, metaType, fullNames) {
this.connection = syncforce.getConnection()
this.collection = collection
this.metaType = metaType
this.fullNames = fullNames
this.intervalHandle = null
this.running = null
}
start() {
if (!this.intervalHandle) {
this.intervalHandle = Meteor.setInterval(this.run.bind(this), DEFAULT_INTERVAL * 60000)
}
// run now
this.run()
}
stop() {
if (this.intervalHandle) {
Meteor.clearInterval(this.intervalHandle)
this.intervalHandle = null
}
}
run() {
if(this.running)
return
this.running = true
Logging.debug('Running ' + this.metaType + ' metadata sync for ' + this.fullNames.join(', '))
var syncRead = Meteor.wrapAsync(this.connection.metadata.read, this.connection.metadata)
try {
let result = syncRead(this.metaType, this.fullNames)
if(!isArray(result))
result = [result]
const status = this.processResults(result)
Logging.debug('Sync ' + this.metaType + ' metadata complete', status)
} catch(err) {
Logging.error('Error running ' + this.metaType + ' metadata sync', err)
}
this.running = false
}
processResults(result) {
const status = {
updated: 0,
inserted: 0
}
result.forEach(meta => {
const r = this.collection.upsert({ fullName: meta.fullName }, {
$set: { ...meta, metaDataType: this.metaType }
})
if(r.insertedId) {
status.inserted++
} else {
status.updated++
}
})
return status
}
}
export default MetadataSync