Skip to content

Commit b62b55d

Browse files
committed
Fixed a mesh fs sync issue
1 parent 834fac2 commit b62b55d

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

agent/apps/ztm/chat/api.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,23 @@ export default function ({ app, mesh }) {
152152
return Promise.resolve()
153153
}
154154

155+
var publishQueue = []
156+
var isPublishing = false
157+
155158
function publishMessage(dirname, message) {
156-
return mesh.dir(dirname).then(filenames => {
159+
publishQueue.push([dirname, message])
160+
if (!isPublishing) {
161+
isPublishing = true
162+
publishNext()
163+
}
164+
}
165+
166+
function publishNext() {
167+
if (publishQueue.length === 0) return (isPublishing = false)
168+
var item = publishQueue.shift();
169+
var dirname = item[0]
170+
var message = item[1]
171+
mesh.dir(dirname).then(filenames => {
157172
var timestamps = filenames.filter(
158173
name => name.endsWith('.json')
159174
).map(
@@ -189,7 +204,7 @@ export default function ({ app, mesh }) {
189204
return mesh.write(filename, JSON.encode(list))
190205
}
191206
)
192-
})
207+
}).then(() => publishNext())
193208
}
194209

195210
function allEndpoints() {

agent/fs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ export default function(storeDir) {
8787
} catch {}
8888
}
8989

90-
function write(filename, data) {
90+
function write(filename, data, time) {
9191
var path = os.path.normalize(filename)
92-
var t = Date.now()
92+
var t = time || Date.now()
9393
var h = hash(path, data)
9494
var meta = { pathname: path, timestamp: t }
9595
os.write(os.path.join(storeDir, h), data)

agent/mesh.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,14 +1134,16 @@ export default function (rootDir, config) {
11341134
}
11351135

11361136
function syncFile(pathname) {
1137+
pathname = os.path.normalize(pathname)
1138+
var st = fs.stat(pathname)
11371139
return findFile(pathname).then(meta => {
1138-
if (!meta || meta.size < 0) return null
1139-
pathname = os.path.normalize(pathname)
1140+
if (!meta) return st ? fs.raw(st.hash) : null
1141+
if (meta.size < 0) return st?.time > meta.time ? fs.raw(st.hash) : null
11401142

11411143
var hash = meta.hash
1142-
var st = fs.stat(pathname)
1144+
var time = meta.time
11431145
if (st?.hash === hash) return fs.raw(hash)
1144-
if (st?.time > meta.time) return fs.raw(hash)
1146+
if (st?.time > time) return fs.raw(st.hash)
11451147

11461148
var sources = [...meta.sources]
11471149
return pickOne()
@@ -1159,7 +1161,7 @@ export default function (rootDir, config) {
11591161
logError(`Download of file ${hash} from ep ${ep} is corrupted`)
11601162
return pickOne()
11611163
}
1162-
fs.write(pathname, data)
1164+
fs.write(pathname, data, time)
11631165
advertiseFilesystem()
11641166
return data
11651167
}).catch(ret => {
@@ -1319,6 +1321,7 @@ export default function (rootDir, config) {
13191321
if (globalPath) {
13201322
fs.write(globalPath, data)
13211323
advertiseFilesystem()
1324+
return syncFile(globalPath)
13221325
}
13231326
}
13241327
return Promise.resolve()

0 commit comments

Comments
 (0)