为什么 Meteor 每次执行某个 Meteor.call 时都会重新启动?

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

(这个问题被标记为可能是重复的,但它不是(据我所知),因为另一个问题是关于如何写入本地磁盘,这不是我的问题。我可以写入服务器磁盘没有问题,但这样做会导致 Meteor 重新启动。)

我的客户端代码中有一个 Meteor.call(),在将 Google Cloud Text-to-Speech 文件保存到服务器并创建一个 link 到客户端中的声音文件。但是,每次我调用 Meteor 时,Meteor 服务器都会在完成任务后约 2 秒后自行重启。这是客户端代码:

(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";
  }
});

这是服务器代码的样子(抱歉篇幅和冗余——我不是真正的程序员):

(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;
  }
}

重新启动不会强制重新加载页面(我只在 meteor 控制台上看到它们),但我认为它们会减慢速度。

我不知道你的 tempSoundPath 值是多少,但它的行为就像你正在将文件写入你的项目目录(服务器/...中的某处)。

Meteor 将此视为源代码更改,并重建并重新启动服务器。

这甚至在生产环境中也行不通,因为您可能没有文件系统的写入权限。

您最好将文件写入数据库,或者将它们传递到 S3 存储桶。有像 ostrio::files 这样的流星包可以帮助你。