sync.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * This file is part of the storage node for the Joystream project.
  3. * Copyright (C) 2019 Joystream Contributors
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. 'use strict'
  19. const debug = require('debug')('joystream:sync')
  20. async function syncCallback(api, storage) {
  21. // The first step is to gather all data objects from chain.
  22. // TODO: in future, limit to a configured tranche
  23. // FIXME this isn't actually on chain yet, so we'll fake it.
  24. const knownContentIds = (await api.assets.getKnownContentIds()) || []
  25. const roleAddress = api.identities.key.address
  26. const providerId = api.storageProviderId
  27. // Iterate over all sync objects, and ensure they're synced.
  28. const allChecks = knownContentIds.map(async contentId => {
  29. // eslint-disable-next-line prefer-const
  30. let { relationship, relationshipId } = await api.assets.getStorageRelationshipAndId(providerId, contentId)
  31. // get the data object
  32. // make sure the data object was Accepted by the liaison,
  33. // don't just blindly attempt to fetch them
  34. let fileLocal
  35. try {
  36. // check if we have content or not
  37. const stats = await storage.stat(contentId)
  38. fileLocal = stats.local
  39. } catch (err) {
  40. // on error stating or timeout
  41. debug(err.message)
  42. // we don't have content if we can't stat it
  43. fileLocal = false
  44. }
  45. if (!fileLocal) {
  46. try {
  47. await storage.synchronize(contentId)
  48. } catch (err) {
  49. // duplicate logging
  50. // debug(err.message)
  51. return
  52. }
  53. // why are we returning, if we synced the file
  54. return
  55. }
  56. if (!relationship) {
  57. // create relationship
  58. debug(`Creating new storage relationship for ${contentId.encode()}`)
  59. try {
  60. relationshipId = await api.assets.createAndReturnStorageRelationship(roleAddress, providerId, contentId)
  61. await api.assets.toggleStorageRelationshipReady(roleAddress, providerId, relationshipId, true)
  62. } catch (err) {
  63. debug(`Error creating new storage relationship ${contentId.encode()}: ${err.stack}`)
  64. return
  65. }
  66. } else if (!relationship.ready) {
  67. debug(`Updating storage relationship to ready for ${contentId.encode()}`)
  68. // update to ready. (Why would there be a relationship set to ready: false?)
  69. try {
  70. await api.assets.toggleStorageRelationshipReady(roleAddress, providerId, relationshipId, true)
  71. } catch (err) {
  72. debug(`Error setting relationship ready ${contentId.encode()}: ${err.stack}`)
  73. }
  74. } else {
  75. // we already have content and a ready relationship set. No need to do anything
  76. // debug(`content already stored locally ${contentId.encode()}`);
  77. }
  78. })
  79. return Promise.all(allChecks)
  80. }
  81. async function syncPeriodic(api, flags, storage) {
  82. try {
  83. debug('Starting sync run...')
  84. await syncCallback(api, storage)
  85. debug('sync run complete')
  86. } catch (err) {
  87. debug(`Error in syncPeriodic ${err.stack}`)
  88. }
  89. // always try again
  90. setTimeout(syncPeriodic, flags.syncPeriod, api, flags, storage)
  91. }
  92. function startSyncing(api, flags, storage) {
  93. syncPeriodic(api, flags, storage)
  94. }
  95. module.exports = {
  96. startSyncing,
  97. }