sync.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.createStorageRelationship(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. }
  65. } else if (!relationship.ready) {
  66. debug(`Updating storage relationship to ready for ${contentId.encode()}`)
  67. // update to ready. (Why would there be a relationship set to ready: false?)
  68. try {
  69. await api.assets.toggleStorageRelationshipReady(roleAddress, providerId, relationshipId, true)
  70. } catch (err) {
  71. debug(`Error setting relationship ready ${contentId.encode()}: ${err.stack}`)
  72. }
  73. } else {
  74. // we already have content and a ready relationship set. No need to do anything
  75. // debug(`content already stored locally ${contentId.encode()}`);
  76. }
  77. })
  78. return Promise.all(allChecks)
  79. }
  80. async function syncPeriodic(api, flags, storage) {
  81. try {
  82. debug('Starting sync run...')
  83. const chainIsSyncing = await api.chainIsSyncing()
  84. if (chainIsSyncing) {
  85. debug('Chain is syncing. Postponing sync run.')
  86. return setTimeout(syncPeriodic, flags.syncPeriod, api, flags, storage)
  87. }
  88. const recommendedBalance = await api.providerHasMinimumBalance(300)
  89. if (!recommendedBalance) {
  90. debug('Warning: Provider role account is running low on balance.')
  91. }
  92. const sufficientBalance = await api.providerHasMinimumBalance(100)
  93. if (!sufficientBalance) {
  94. debug('Provider role account does not have sufficient balance. Postponing sync run!')
  95. return setTimeout(syncPeriodic, flags.syncPeriod, api, flags, storage)
  96. }
  97. await syncCallback(api, storage)
  98. debug('sync run complete')
  99. } catch (err) {
  100. debug(`Error in syncPeriodic ${err.stack}`)
  101. }
  102. // always try again
  103. setTimeout(syncPeriodic, flags.syncPeriod, api, flags, storage)
  104. }
  105. function startSyncing(api, flags, storage) {
  106. syncPeriodic(api, flags, storage)
  107. }
  108. module.exports = {
  109. startSyncing,
  110. }