sync.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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, config, storage)
  21. {
  22. debug('Starting sync run...');
  23. // The first step is to gather all data objects from chain.
  24. // TODO: in future, limit to a configured tranche
  25. // FIXME this isn't actually on chain yet, so we'll fake it.
  26. const knownContentIds = await api.assets.getKnownContentIds() || [];
  27. const role_addr = api.identities.key.address
  28. const providerId = api.storageProviderId
  29. // Iterate over all sync objects, and ensure they're synced.
  30. const allChecks = knownContentIds.map(async (content_id) => {
  31. let { relationship, relationshipId } = await api.assets.getStorageRelationshipAndId(providerId, content_id);
  32. let fileLocal;
  33. try {
  34. // check if we have content or not
  35. let stats = await storage.stat(content_id);
  36. fileLocal = stats.local;
  37. } catch (err) {
  38. // on error stating or timeout
  39. debug(err.message);
  40. // we don't have content if we can't stat it
  41. fileLocal = false;
  42. }
  43. if (!fileLocal) {
  44. try {
  45. await storage.synchronize(content_id);
  46. } catch (err) {
  47. debug(err.message)
  48. }
  49. return;
  50. }
  51. if (!relationship) {
  52. // create relationship
  53. debug(`Creating new storage relationship for ${content_id.encode()}`);
  54. try {
  55. relationshipId = await api.assets.createAndReturnStorageRelationship(role_addr, providerId, content_id);
  56. await api.assets.toggleStorageRelationshipReady(role_addr, providerId, relationshipId, true);
  57. } catch (err) {
  58. debug(`Error creating new storage relationship ${content_id.encode()}: ${err.stack}`);
  59. return;
  60. }
  61. } else if (!relationship.ready) {
  62. debug(`Updating storage relationship to ready for ${content_id.encode()}`);
  63. // update to ready. (Why would there be a relationship set to ready: false?)
  64. try {
  65. await api.assets.toggleStorageRelationshipReady(role_addr, providerId, relationshipId, true);
  66. } catch(err) {
  67. debug(`Error setting relationship ready ${content_id.encode()}: ${err.stack}`);
  68. }
  69. } else {
  70. // we already have content and a ready relationship set. No need to do anything
  71. // debug(`content already stored locally ${content_id.encode()}`);
  72. }
  73. });
  74. await Promise.all(allChecks);
  75. debug('sync run complete');
  76. }
  77. async function sync_periodic(api, config, storage)
  78. {
  79. try {
  80. await sync_callback(api, config, storage);
  81. } catch (err) {
  82. debug(`Error in sync_periodic ${err.stack}`);
  83. }
  84. // always try again
  85. setTimeout(sync_periodic, config.get('syncPeriod'), api, config, storage);
  86. }
  87. function start_syncing(api, config, storage)
  88. {
  89. sync_periodic(api, config, storage);
  90. }
  91. module.exports = {
  92. start_syncing: start_syncing,
  93. }