block_range_import.ts 1.1 KB

12345678910111213141516171819202122232425262728
  1. import { addBlockRange, processNext } from './joystream';
  2. import { connectUpstream } from './joystream/ws';
  3. import {findLastProcessedBlockId} from './db/models/block'
  4. import {
  5. StartBlock
  6. } from './db/models'
  7. async function main () {
  8. const api = await connectUpstream();
  9. const firstBlock:number = parseInt(process.env.START_BLOCK);
  10. const lastBlock:number = parseInt(process.env.END_BLOCK) || (await StartBlock.findAll())[0].get({plain: true}).block - 1;
  11. console.log(`[Joystream] Importing block range [${firstBlock} - ${lastBlock}] started`);
  12. const lastImportedBlockHeight = await findLastProcessedBlockId(firstBlock, lastBlock);
  13. if (lastImportedBlockHeight && lastImportedBlockHeight > 0 && lastImportedBlockHeight < lastBlock) {
  14. console.log(`[Joystream] Found last imported block ${lastImportedBlockHeight}. Resuming processing from the next one`);
  15. await addBlockRange(api, lastImportedBlockHeight + 1, lastBlock);
  16. } else {
  17. await addBlockRange(api, firstBlock, lastBlock);
  18. }
  19. processNext();
  20. }
  21. main()