Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #8605 from brave/issue-8602
Browse files Browse the repository at this point in the history
use muon.file.writeImportant to write ledger files (#8602)
  • Loading branch information
bsclifton committed May 3, 2017
2 parents 612350d + 360f3a5 commit e0bd325
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 84 deletions.
2 changes: 1 addition & 1 deletion app/browser/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ const updateAboutDetails = (tab, tabValue) => {
trackedBlockersCount,
adblockCount,
httpsUpgradedCount,
newTabDetail: newTabDetail.toJS()
newTabDetail: newTabDetail && newTabDetail.toJS()
})
} else if (location === 'about:autofill') {
const defaultSession = session.defaultSession
Expand Down
95 changes: 12 additions & 83 deletions app/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
contributeP: (stickyP OR !excluded) AND eligibleP AND !blockedP
*/

const crypto = require('crypto')
const fs = require('fs')
const os = require('os')
const path = require('path')
Expand Down Expand Up @@ -274,16 +273,13 @@ var init = () => {
try {
appDispatcher.register(doAction)
initialize(getSetting(settings.PAYMENTS_ENABLED))

doneTimer = setInterval(doneWriter, 1 * msecs.minute)
} catch (ex) { console.log('ledger.js initialization failed: ' + ex.toString() + '\n' + ex.stack) }
}

var quit = () => {
quitP = true
visit('NOOP', underscore.now(), null)
clearInterval(doneTimer)
doneWriter()

if ((!getSetting(settings.PAYMENTS_ENABLED)) && (getSetting(settings.SHUTDOWN_CLEAR_HISTORY))) reset(true)
}
Expand Down Expand Up @@ -723,7 +719,7 @@ var initialize = (paymentsEnabled) => {
}
getStateInfo(stateResult)

atomicWriter(pathName(statePath), stateResult, { flushP: true }, () => {})
muonWriter(pathName(statePath), stateResult)
})
}
} catch (ex) {
Expand Down Expand Up @@ -980,7 +976,7 @@ const fetchFavIcon = (entry, url, redirects) => {
var updatePublisherInfo = (changedPublisher) => {
var data

atomicWriter(pathName(synopsisPath), synopsis, () => {})
muonWriter(pathName(synopsisPath), synopsis)
if (!publisherInfo._internal.enabled) return

publisherInfo.synopsis = synopsisNormalizer(changedPublisher)
Expand Down Expand Up @@ -1584,29 +1580,15 @@ var updateLedgerInfo = () => {
* ledger client callbacks
*/

var logs = []

var callback = (err, result, delayTime) => {
var i, results, then
var results
var entries = client && client.report()
var now = underscore.now()

if (clientOptions.verboseP) {
console.log('\nledger client callback: clientP=' + (!!client) + ' errP=' + (!!err) + ' resultP=' + (!!result) +
' delayTime=' + delayTime)
}

if (entries) {
then = now - msecs.week
logs = logs.concat(entries)

for (i = 0; i < logs.length; i++) if (logs[i].when > then) break
if ((i !== 0) && (i !== logs.length)) logs = logs.slice(i)
if (result) entries.push({ who: 'callback', what: result, when: underscore.now() })

atomicWriter(pathName(logPath), entries, { flag: 'a' }, () => {})
}

if (err) {
console.log('ledger client error(1): ' + JSON.stringify(err, null, 2) + (err.stack ? ('\n' + err.stack) : ''))
if (!client) return
Expand Down Expand Up @@ -1672,7 +1654,7 @@ var callback = (err, result, delayTime) => {
})
}

atomicWriter(pathName(statePath), result, { flushP: true }, () => {})
muonWriter(pathName(statePath), result)
run(delayTime)
}

Expand Down Expand Up @@ -1813,7 +1795,7 @@ var run = (delayTime) => {
result = client.vote(winner)
if (result) state = result
})
if (state) atomicWriter(pathName(statePath), state, { flushP: true }, () => {})
if (state) muonWriter(pathName(statePath), state)
} catch (ex) {
console.log('ledger client error(2): ' + ex.toString() + (ex.stack ? ('\n' + ex.stack) : ''))
}
Expand Down Expand Up @@ -2045,7 +2027,7 @@ var setPaymentInfo = (amount) => {
client.setBraveryProperties(bravery, (err, result) => {
if (err) return console.log('ledger setBraveryProperties: ' + err.toString())

if (result) atomicWriter(pathName(statePath), result, { flushP: true }, () => {})
if (result) muonWriter(pathName(statePath), result)
})
if (ledgerInfo.created) getPaymentInfo()
}
Expand Down Expand Up @@ -2093,69 +2075,16 @@ var networkConnected = underscore.debounce(() => {
* low-level utilities
*/

var syncingP = {}

var atomicWriter = (path, obj, options, cb) => {
var data, suffix

if (typeof options === 'function') {
cb = options
options = null
}
options = underscore.defaults(options || {}, { encoding: 'utf8', mode: parseInt('644', 8) })

if ((!options.flushP) && (!syncingP[path])) syncingP[path] = true
if (syncingP[path]) {
syncingP[path] = { obj: obj, options: options, cb: cb }
if (ledgerInfo._internal.debugP) console.log('\ndeferring ' + path)
return
}
syncingP[path] = true

data = JSON.stringify(obj, null, 2)
suffix = '-' + crypto.createHash('md5').update(data).digest('hex')
if (ledgerInfo._internal.debugP) console.log('\nwriting ' + path + suffix)
fs.writeFile(path + suffix, data, options, (err) => {
var deferred = syncingP[path]

delete syncingP[path]
if (typeof deferred === 'object') {
if (ledgerInfo._internal.debugP) console.log('\nrestarting ' + path)
return atomicWriter(path, deferred.obj, deferred.options, deferred.cb)
}

if (err) {
console.log('write error: ' + err.toString())
return cb(err)
}
var muonWriter = (path, payload) => {
muon.file.writeImportant(path, JSON.stringify(payload, null, 2), (success) => {
if (!success) return console.log('write error: ' + path)

if ((quitP) && (!getSetting(settings.PAYMENTS_ENABLED)) && (getSetting(settings.SHUTDOWN_CLEAR_HISTORY))) {
if (ledgerInfo._internal.debugP) console.log('\ndeleting ' + path + suffix)
fs.unlink(path + suffix, (err) => {
if (err) console.log('unlink error: ' + err.toString())
cb(err)
})
return
if (ledgerInfo._internal.debugP) console.log('\ndeleting ' + path)
return fs.unlink(path, (err) => { if (err) console.log('unlink error: ' + err.toString()) })
}

if (ledgerInfo._internal.debugP) console.log('\nrenaming ' + path + suffix)
fs.rename(path + suffix, path, (err) => {
if (err) console.log('rename error: ' + err.toString())
cb(err)
})
})
}

var doneWriter = () => {
underscore.keys(syncingP).forEach((path) => {
var deferred = syncingP[path]

if (typeof deferred !== 'object') return

delete syncingP[path]
if (ledgerInfo._internal.debugP) console.log('\nflushing ' + path)
deferred.options.flushP = true
atomicWriter(path, deferred.obj, deferred.options, deferred.cb)
if (ledgerInfo._internal.debugP) console.log('\nwrote ' + path)
})
}

Expand Down

0 comments on commit e0bd325

Please sign in to comment.