Coder Social home page Coder Social logo

Comments (11)

bcoe avatar bcoe commented on June 27, 2024 1

@chsnt the problem isn't the Translate client, I believe it's this code:

     getFiles(dir, function (err, files) {

          pbSubr.start(files.length, 0);        

            files.forEach( file => {
              fs.pathExists(file.replace('data\\boards\\', 'data\\boards-ru\\'))
              .then(exists => { 
                if(!exists) { 
                    translatorWithErrorHandle (file); 
                } else {                  
                  pbSubr.increment()
                  pbSubr.update(undefined, {dir: file.split('\\')[3]});
                }
              })
                                
            });

        });

This is going to open all files in parallel, so if there are many files in dir you might hit a maximum amount of concurrent open files at the operating system level.

I would potentially rewrite this like:

     getFiles(dir, async function (err, files) {

          pbSubr.start(files.length, 0);        
          for (const file of files) {
              const exists = await fs.pathExists(file.replace('data\\boards\\', 'data\\boards-ru\\'))
              . if(!exists) { 
                  await translatorWithErrorHandle (file); 
                } else {                  
                  pbSubr.increment()
                  await pbSubr.update(undefined, {dir: file.split('\\')[3]});
                }
              })
          } 
        });

☝️ this is pseudo code that would translate a file one at a time, assuming that pbSubr.update returns a promise.

As an alternative to async/await you might also look at p-queue which allows you to control the amount of concurrency happening in your program.

In general, you don't want to perform a large amount of async work in a loop, because you'll create a lot of open handles at once.

from nodejs-translate.

bcoe avatar bcoe commented on June 27, 2024

@chsnt this indicates that there are too many open file handles in your application, is there a chance you are initializing the client in a loop? Could you share a snippet of the code you are running?

from nodejs-translate.

chsnt avatar chsnt commented on June 27, 2024

@bcoe

main.js

require('dotenv').config({ debug: process.env.GOOGLE_APPLICATION_CREDENTIALS })
const translate = require('./translate');
...
translate (text, targetLang)
...

translate.js

const {Translate} = require('@google-cloud/translate').v2;

const tr = (text, target) => {
  return new Promise ((resolve, reject)=>{
    translate.translate(text, target)
    .then(([result])=>resolve(result))
    .catch(err=>{
        console.error(err); 
        reject(err)
    })
  })
      
}

module.exports = tr;

from nodejs-translate.

bcoe avatar bcoe commented on June 27, 2024

@chsnt what does the following do:

require('dotenv').config({ debug: process.env.GOOGLE_APPLICATION_CREDENTIALS })

I could you try running translate.js in isolation, with GOOGLE_APPLICATION_CREDENTIALS set to the path of your credentials file?

from nodejs-translate.

chsnt avatar chsnt commented on June 27, 2024

@bcoe

I could you try running translate.js in isolation, with GOOGLE_APPLICATION_CREDENTIALS set to the path of your credentials file?

i run this

require('dotenv').config({ debug: process.env.GOOGLE_APPLICATION_CREDENTIALS })
const {Translate} = require('@google-cloud/translate').v2;

const tr = (text, target) => {
  return new Promise ((resolve, reject)=>{
    translate.translate(text, target)
    .then(([result])=>resolve(result))
    .catch(err=>{
        console.error(err); 
        reject(err)
    })
  })      
}
  
tr('hello', 'ru')
.then(res => console.log(res))
.catch(err => console.log(err))

and it works correctly.

But when i run it without this string

require('dotenv').config({ debug: process.env.GOOGLE_APPLICATION_CREDENTIALS })

i geting this error

Error: Unable to detect a Project Id in the current environment.

from nodejs-translate.

bcoe avatar bcoe commented on June 27, 2024

try:

const {Translate} = require('@google-cloud/translate').v2;
const translate = Translate({projectId: 'your-project-id'})
const tr = (text, target) => {
  return new Promise ((resolve, reject)=>{
    translate.translate(text, target)
    .then(([result])=>resolve(result))
    .catch(err=>{
        console.error(err); 
        reject(err)
    })
  })      
}
  
tr('hello', 'ru')
.then(res => console.log(res))
.catch(err => console.log(err))

Providing GOOGLE_APPLICATION_CREDENTIALS with a path to your credentials, rather than using dotenv.

dotenv simply loads a local .env file, with environment variables, I just want to eliminate this as a potential cause of your issues.

from nodejs-translate.

chsnt avatar chsnt commented on June 27, 2024

In my system environment variables
GOOGLE_APPLICATION_CREDENTIALS=d:\Projects\translator\translator-22f1b569e00b.json

  • translator-22f1b569e00b.json is my key-file (in .env-file : GOOGLE_APPLICATION_CREDENTIALS=D:\Projects\translator\translator-22f1b569e00b.json )

i try this code:

const {Translate} = require('@google-cloud/translate').v2;
const translate = Translate({projectId: 'your-project-id'})
const tr = (text, target) => {
  return new Promise ((resolve, reject)=>{
    translate.translate(text, target)
    .then(([result])=>resolve(result))
    .catch(err=>{
        console.error(err); 
        reject(err)
    })
  })      
}
  
tr('hello', 'ru')
.then(res => console.log(res))
.catch(err => console.log(err))

and got error:

Error: Unable to detect a Project Id in the current environment.    

 at D:\Projects\translator\node_modules\google-auth-library\build\src\auth\googleauth.js:90:31
 at processTicksAndRejections (internal/process/task_queues.js:93:5)

from nodejs-translate.

bcoe avatar bcoe commented on June 27, 2024

@chsnt could you also try setting the environment variable GCLOUD_PROJECT to the name of the project the GOOGLE_APPLICATION_CREDENTIALS.

I'm not sure why dotenv would not be throwing the same error, but I believe you might find setting this environment variable explicitly will do the trick.

from nodejs-translate.

chsnt avatar chsnt commented on June 27, 2024

@bcoe
Sory, i try it again

const {Translate} = require('@google-cloud/translate').v2;
const translate = Translate({projectId: 'your-project-id'})
const tr = (text, target) => {
  return new Promise ((resolve, reject)=>{
    translate.translate(text, target)
    .then(([result])=>resolve(result))
    .catch(err=>{
        console.error(err); 
        reject(err)
    })
  })      
}
  
tr('hello', 'ru')
.then(res => console.log(res))
.catch(err => console.log(err))

and it works correctly (for single use).

But when i use this code like a module for multiple call i had error again:

EMFILE: too many open files, open 'd:\Projects\translator\translator-22f1b569e00b.json'
[Error: EMFILE: too many open files, open 'd:\Projects\translator\translator-22f1b569e00b.json'] {
  errno: -4066,
  code: 'EMFILE',
  syscall: 'open',
  path: 'd:\\Projects\\translator\\translator-22f1b569e00b.json'
}

(i dont use dotenv anymore)

from nodejs-translate.

bcoe avatar bcoe commented on June 27, 2024

@chsnt could you provide code that demonstrates how you are running this logic multiple times; my hunch is that there's a chance you're creating a Translate client in a loop. Make sure you only create a single translate instance, and then call this multiple times (rather than creating a new translate client for each request.

from nodejs-translate.

chsnt avatar chsnt commented on June 27, 2024

@bcoe
Thanks for an answer
I use translate like this:

const translateG = require('./translate');
...
translateG (text, targetLang)  // multiple call (its wrong?)
...

(full code https://gist.github.com/chsnt/0d669b0a9509086dce829f8d0ae7257c )

translate.js

const {Translate} = require('@google-cloud/translate').v2;
const translate = Translate({projectId: 'your-project-id'})
const tr = (text, target) => {
  return new Promise ((resolve, reject)=>{
    translate.translate(text, target)
    .then(([result])=>resolve(result))
    .catch(err=>{
        console.error(err); 
        reject(err)
    })
  })      
}

module.exports = tr;

from nodejs-translate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.