Will Lehman
Will Lehman

Reputation: 25

Why does Meteor restart every time a do a certain Meteor.call?

(This question is marked as possibly being a duplicate, but it is not (as far as I can tell), because the other question is about HOW to write to local disk, and that's not my problem. I can write to the server disk no problem, but doing so causes Meteor to restart.)

I have a Meteor.call() in my client code that works as expected in saving Google Cloud Text-to-Speech files to the server and creating a link to the sound files in the client. However, every time I make the Meteor call, the Meteor server restarts itself after about 2 seconds after finishing its tasks. Here's the client code:

(client/main.js)
Meteor.call('get.tts.links', tempSoundPath, voice, currentForeign, 
currentExample, function(error, result) {
  if (error) {
    alert('Error');
  } else {
    Session.set("links", result);
    soundLink1 = 'tempsoundfiles/' + result[0] + ".mp3";
    if (!result[1]) soundLink2 = "";
    else soundLink2 = 'tempsoundfiles/' + result[1] + ".mp3";
  }
});

Here's what the server code looks like (sorry for the length and redundancy--I'm not an actual programmer):

(server/main.js)
const textToSpeech = require('@google-cloud/text-to-speech');
....
....
'get.tts.links'(tempSoundPath, voice, currentForeign, currentExample) {
  var path = "";
  var soundFileId = "";
  var text1 = currentForeign;
  var text2 = currentExample;
  var fileId = makepass();

  const client = new textToSpeech.TextToSpeechClient();
  const request = {
    input: {
      text: text1
    },
    voice: {
      languageCode: "en-GB",
      ssmlGender: 'MALE'
    },
    audioConfig: {
      audioEncoding: 'MP3'
    },
  };
  client.synthesizeSpeech(request, (err, response) => {
    if (err) {
      console.error('ERROR:', err);
      return;
    }
    path = tempSoundPath + fileId + ".mp3";
    console.log("path = " + path);

    // Write the binary audio content to a local file

    fs.writeFile(path, response.audioContent, 'binary', err => {
      if (err) {
        console.error('ERROR:', err);
        return;
      }
    });
  });

  if (!text2 || text2 == "") {
    var result = [];
    result.push(fileId);
    return result;
  } else {
    var fileId2 = makepass();
    const request2 = {
      input: {
        text: text2
      },
      voice: {
        languageCode: voice,
        ssmlGender: 'FEMALE'
      },
      audioConfig: {
        audioEncoding: 'MP3'
      },
    };
    client.synthesizeSpeech(request2, (err, response) => {
      if (err) {
        console.error('ERROR:', err);
        return;
      }
      var path2 = tempSoundPath + fileId2 + ".mp3";

      // Write the binary audio content to a local file

      fs.writeFile(path2, response.audioContent, 'binary', err => {
        if (err) {
          console.error('ERROR:', err);
          return;
        }
      });
    });
    var result1 = [];
    result1.push(fileId, fileId2);
    return result1;
  }
}

The restarts don't force a page reload (I only see them on the meteor console), but I think they are slowing things down quite a bit.

Upvotes: 0

Views: 44

Answers (1)

Mikkel
Mikkel

Reputation: 7777

I don't know what your tempSoundPath value is, but it is behaving like you are writing files to your project directory (somewhere in server/...).

Meteor sees this as a source code change, and rebuilds and restarts the server.

This also won't even work in production, because you probably won't have write access to the file system.

You will be better off either writing files to the database, or passing them off to an S3 bucket. There are meteor packages like ostrio::files which will help you with this.

Upvotes: 2

Related Questions