Procházet zdrojové kódy

storage-node-v2: Fix temp directory deleting.

Shamil Gadelshin před 3 roky
rodič
revize
9f68890de9

+ 3 - 4
storage-node-v2/src/commands/server.ts

@@ -3,7 +3,7 @@ import { createApp } from '../services/webApi/app'
 import ApiCommandBase from '../command-base/ApiCommandBase'
 import logger, { initElasticLogger } from '../services/logger'
 import { ApiPromise } from '@polkadot/api'
-import { performSync } from '../services/sync/synchronizer'
+import { performSync, TempDirName } from '../services/sync/synchronizer'
 import sleep from 'sleep-promise'
 import rimraf from 'rimraf'
 import _ from 'lodash'
@@ -77,8 +77,7 @@ export default class Server extends ApiCommandBase {
   async run(): Promise<void> {
     const { flags } = this.parse(Server)
 
-    const tempDirName = 'temp'
-    await removeTempDirectory(flags.uploads, tempDirName)
+    await removeTempDirectory(flags.uploads, TempDirName)
 
     let elasticUrl
     if (!_.isEmpty(flags.elasticSearchHost)) {
@@ -120,7 +119,7 @@ export default class Server extends ApiCommandBase {
         workerId,
         maxFileSize,
         uploadsDir: flags.uploads,
-        tempDirName,
+        tempDirName: TempDirName,
         process: this.config,
         queryNodeUrl,
         enableUploadingAuth: !flags.disableUploadAuth,

+ 22 - 12
storage-node-v2/src/services/sync/synchronizer.ts

@@ -4,8 +4,14 @@ import { SyncTask, DownloadFileTask, DeleteLocalFileTask, PrepareDownloadFileTas
 import { WorkingStack, TaskProcessorSpawner, TaskSink } from './workingProcess'
 import _ from 'lodash'
 import fs from 'fs'
+import path from 'path'
 const fsPromises = fs.promises
 
+/**
+ * Temporary directory name for data uploading.
+ */
+export const TempDirName = 'temp'
+
 /**
  * Runs the data synchronization workflow. It compares the current node's
  * storage obligations with the local storage and fixes the difference.
@@ -30,7 +36,7 @@ export async function performSync(
   logger.info('Started syncing...')
   const [model, files] = await Promise.all([
     getStorageObligationsFromRuntime(queryNodeUrl, workerId),
-    getLocalFileNames(uploadDirectory),
+    getLocalDataObjects(uploadDirectory, TempDirName),
   ])
 
   const requiredIds = model.dataObjects.map((obj) => obj.id)
@@ -62,15 +68,6 @@ export async function performSync(
   logger.info('Sync ended.')
 }
 
-/**
- * Returns file names from the local directory.
- *
- * @param directory - local directory to get file names from
- */
-async function getLocalFileNames(directory: string): Promise<string[]> {
-  return fsPromises.readdir(directory)
-}
-
 /**
  * Creates the download preparation tasks.
  *
@@ -148,8 +145,21 @@ async function getDownloadTasks(
  *
  * @param uploadDirectory - local directory to get file names from
  */
-export async function getLocalDataObjects(uploadDirectory: string): Promise<string[]> {
+export async function getLocalDataObjects(uploadDirectory: string, tempDirName: string): Promise<string[]> {
   const localIds = await getLocalFileNames(uploadDirectory)
 
-  return localIds
+  // Filter temporary directory name.
+  const tempDirectoryName = path.parse(tempDirName).name
+  const data = localIds.filter((dataObjectId) => dataObjectId !== tempDirectoryName)
+
+  return data
+}
+
+/**
+ * Returns file names from the local directory.
+ *
+ * @param directory - local directory to get file names from
+ */
+async function getLocalFileNames(directory: string): Promise<string[]> {
+  return fsPromises.readdir(directory)
 }

+ 1 - 6
storage-node-v2/src/services/webApi/controllers/stateApi.ts

@@ -13,7 +13,6 @@ import {
 import fastFolderSize from 'fast-folder-size'
 import { promisify } from 'util'
 import fs from 'fs'
-import path from 'path'
 import NodeCache from 'node-cache'
 const fsPromises = fs.promises
 
@@ -153,11 +152,7 @@ async function getCachedLocalDataObjects(uploadsDir: string, tempDirName: string
   const entryName = 'local_data_object'
 
   if (!dataCache.has(entryName)) {
-    let data = await getLocalDataObjects(uploadsDir)
-
-    // Filter temporary directory name.
-    const tempDirectoryName = path.parse(tempDirName).name
-    data = data.filter((dataObjectId) => dataObjectId !== tempDirectoryName)
+    const data = await getLocalDataObjects(uploadsDir, tempDirName)
 
     dataCache.set(entryName, data)
   }

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 47 - 433
yarn.lock


Některé soubory nejsou zobrazeny, neboť je v těchto rozdílových datech změněno mnoho souborů