123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- 'use strict';
- const debug = require('debug')('joystream:sync');
- async function sync_callback(api, config, storage)
- {
- debug('Starting sync run...');
-
-
-
- const knownContentIds = await api.assets.getKnownContentIds() || [];
- const role_addr = api.identities.key.address
- const providerId = api.storageProviderId
-
- const allChecks = knownContentIds.map(async (content_id) => {
- let { relationship, relationshipId } = await api.assets.getStorageRelationshipAndId(providerId, content_id);
- let fileLocal;
- try {
-
- let stats = await storage.stat(content_id);
- fileLocal = stats.local;
- } catch (err) {
-
- debug(err.message);
-
- fileLocal = false;
- }
- if (!fileLocal) {
- try {
- await storage.synchronize(content_id);
- } catch (err) {
- debug(err.message)
- }
- return;
- }
- if (!relationship) {
-
- debug(`Creating new storage relationship for ${content_id.encode()}`);
- try {
- relationshipId = await api.assets.createAndReturnStorageRelationship(role_addr, providerId, content_id);
- await api.assets.toggleStorageRelationshipReady(role_addr, providerId, relationshipId, true);
- } catch (err) {
- debug(`Error creating new storage relationship ${content_id.encode()}: ${err.stack}`);
- return;
- }
- } else if (!relationship.ready) {
- debug(`Updating storage relationship to ready for ${content_id.encode()}`);
-
- try {
- await api.assets.toggleStorageRelationshipReady(role_addr, providerId, relationshipId, true);
- } catch(err) {
- debug(`Error setting relationship ready ${content_id.encode()}: ${err.stack}`);
- }
- } else {
-
-
- }
- });
- await Promise.all(allChecks);
- debug('sync run complete');
- }
- async function sync_periodic(api, config, storage)
- {
- try {
- await sync_callback(api, config, storage);
- } catch (err) {
- debug(`Error in sync_periodic ${err.stack}`);
- }
-
- setTimeout(sync_periodic, config.get('syncPeriod'), api, config, storage);
- }
- function start_syncing(api, config, storage)
- {
- sync_periodic(api, config, storage);
- }
- module.exports = {
- start_syncing: start_syncing,
- }
|