fs#renameSync JavaScript Examples

The following examples show how to use fs#renameSync. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: outbox-store.js    From medusa with MIT License 6 votes vote down vote up
async flushFile(filePath, flushOperation) {
    const now = `${Date.now()}-${process.pid}`
    let success = false
    let contents = ``
    try {
      if (!existsSync(filePath)) {
        return true
      }
      // Unique temporary file name across multiple concurrent Medusa instances
      const newPath = `${this.bufferFilePath}-${now}`
      renameSync(filePath, newPath)
      contents = readFileSync(newPath, `utf8`)
      unlinkSync(newPath)

      // There is still a chance process dies while sending data and some events are lost
      // This will be ok for now, however
      success = await flushOperation(contents)
    } catch (e) {
      if (isTruthy(MEDUSA_TELEMETRY_VERBOSE)) {
        console.error("Failed to perform file flush", e)
      }
    } finally {
      // if sending fails, we write the data back to the log
      if (!success) {
        if (isTruthy(MEDUSA_TELEMETRY_VERBOSE)) {
          console.error(
            "File flush did not succeed - writing back to file",
            success
          )
        }
        this.appendToBuffer(contents)
      }
    }
    return true
  }