123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- 'use strict'
- const debug = require('debug')('joystream:sync')
- async function syncCallback(api, storage) {
-
-
-
- const knownContentIds = (await api.assets.getKnownContentIds()) || []
- const roleAddress = api.identities.key.address
- const providerId = api.storageProviderId
-
- const allChecks = knownContentIds.map(async (contentId) => {
-
- let { relationship, relationshipId } = await api.assets.getStorageRelationshipAndId(providerId, contentId)
-
-
-
- let fileLocal
- try {
-
- const stats = await storage.stat(contentId)
- fileLocal = stats.local
- } catch (err) {
-
- debug(err.message)
-
- fileLocal = false
- }
- if (!fileLocal) {
- try {
- await storage.synchronize(contentId)
- } catch (err) {
-
-
- return
- }
-
- return
- }
- if (!relationship) {
-
- debug(`Creating new storage relationship for ${contentId.encode()}`)
- try {
- relationshipId = await api.assets.createAndReturnStorageRelationship(roleAddress, providerId, contentId)
- await api.assets.toggleStorageRelationshipReady(roleAddress, providerId, relationshipId, true)
- } catch (err) {
- debug(`Error creating new storage relationship ${contentId.encode()}: ${err.stack}`)
- return
- }
- } else if (!relationship.ready) {
- debug(`Updating storage relationship to ready for ${contentId.encode()}`)
-
- try {
- await api.assets.toggleStorageRelationshipReady(roleAddress, providerId, relationshipId, true)
- } catch (err) {
- debug(`Error setting relationship ready ${contentId.encode()}: ${err.stack}`)
- }
- } else {
-
-
- }
- })
- return Promise.all(allChecks)
- }
- async function syncPeriodic(api, flags, storage) {
- try {
- debug('Starting sync run...')
- await syncCallback(api, storage)
- debug('sync run complete')
- } catch (err) {
- debug(`Error in syncPeriodic ${err.stack}`)
- }
-
- setTimeout(syncPeriodic, flags.syncPeriod, api, flags, storage)
- }
- function startSyncing(api, flags, storage) {
- syncPeriodic(api, flags, storage)
- }
- module.exports = {
- startSyncing,
- }
|