sync.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 sync_callback(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 role_addr = 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 (content_id) => {
  29. let { relationship, relationshipId } = await api.assets.getStorageRelationshipAndId(providerId, content_id);
  30. // get the data object
  31. // make sure the data object was Accepted by the liaison,
  32. // don't just blindly attempt to fetch them
  33. let fileLocal;
  34. try {
  35. // check if we have content or not
  36. let stats = await storage.stat(content_id);
  37. fileLocal = stats.local;
  38. } catch (err) {
  39. // on error stating or timeout
  40. debug(err.message);
  41. // we don't have content if we can't stat it
  42. fileLocal = false;
  43. }
  44. if (!fileLocal) {
  45. try {
  46. await storage.synchronize(content_id);
  47. } catch (err) {
  48. // duplicate logging
  49. // debug(err.message)
  50. return
  51. }
  52. // why are we returning, if we synced the file
  53. return;
  54. }
  55. if (!relationship) {
  56. // create relationship
  57. debug(`Creating new storage relationship for ${content_id.encode()}`);
  58. try {
  59. relationshipId = await api.assets.createAndReturnStorageRelationship(role_addr, providerId, content_id);
  60. await api.assets.toggleStorageRelationshipReady(role_addr, providerId, relationshipId, true);
  61. } catch (err) {
  62. debug(`Error creating new storage relationship ${content_id.encode()}: ${err.stack}`);
  63. return;
  64. }
  65. } else if (!relationship.ready) {
  66. debug(`Updating storage relationship to ready for ${content_id.encode()}`);
  67. // update to ready. (Why would there be a relationship set to ready: false?)
  68. try {
  69. await api.assets.toggleStorageRelationshipReady(role_addr, providerId, relationshipId, true);
  70. } catch(err) {
  71. debug(`Error setting relationship ready ${content_id.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 ${content_id.encode()}`);
  76. }
  77. });
  78. return Promise.all(allChecks);
  79. }
  80. async function sync_periodic(api, flags, storage)
  81. {
  82. try {
  83. debug('Starting sync run...')
  84. await sync_callback(api, storage)
  85. debug('sync run complete')
  86. } catch (err) {
  87. debug(`Error in sync_periodic ${err.stack}`);
  88. }
  89. // always try again
  90. setTimeout(sync_periodic, flags.syncPeriod, api, flags, storage);
  91. }
  92. function start_syncing(api, flags, storage)
  93. {
  94. sync_periodic(api, flags, storage);
  95. }
  96. module.exports = {
  97. start_syncing: start_syncing,
  98. }