sync.js 3.4 KB

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