Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

javascript - 脚本不会自行停止(Script won't stop itself)

I have two scripts, let's call them "first" and "second".

(我有两个脚本,我们称它们为“第一个”和“第二个”。)

I have no issue with "first" script, here how it's looks:

(我对“第一个”脚本没有任何疑问,这里看起来是这样的:)

  const fetch = require('node-fetch');

  console.log(typeof fetch); // prints: function

  fetch(URLHERE)
    .then(res => res.json())
    .then(res => {

      console.log(res);

    });

And here is a "second" script which won't stop itself after it's run

(这是一个“第二”脚本,运行后不会停止)

  const APIWRAPPER = require('APIWRAPPER');
  const api = new APIWRAPPER(TOKEN);

  console.log(typeof api); // prints: object

  api.find(QUERY)
    .then(json => {

      console.log(json);

    });

Both scripts outputs same thing.

(这两个脚本输出相同的内容。)

But when I running "second" script it won't stop.

(但是,当我运行“第二”脚本时,它不会停止。)

I have to press Ctrl + C in my win terminal.

(我必须在赢终端中按Ctrl +C。)

And you might ask, is it such a big issue for me to press Ctrl + C once in a while?

(您可能会问,偶尔按Ctrl + C对我来说有太大问题吗?)

No it's not, but I'm trying to trigger this script with GitHub Action and it's just endlessly running leaving me one option: cancel it.

(不,不是,但是我试图通过GitHub Action触发此脚本,并且它不断运行,给我一个选择:取消它。)

PS I'm using node run first and node run second to trigger those scripts.

(PS我正在node run first node run second node run first node run second以触??发这些脚本。)

package.json:

(package.json:)

{
  ...
  "scripts": {
    "first": "node ./scripts/first.js",
    "second": "node ./scripts/second.js"
  },
}

What I tried:

(我试过的)

  const APIWRAPPER = require('APIWRAPPER');
  const api = new APIWRAPPER(TOKEN);

  console.log(typeof api); // prints: object

  api.find(QUERY)
    .then(json => {

      console.log(json);

      process.exit(0); // Adding this

    });

But I'm not sure if it's good practice, it was just first thing I found.

(但是我不确定这是否是好习惯,这只是我发现的第一件事。)

  ask by Bromer translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I don't really understand your scripts but I know that you can auto cancel your pipeline by setting a specific timeout by default :

(我不太了解您的脚本,但是我知道您可以通过默认设置特定的超时来自动取消管道:)

job.<id>.timeout-minutes sets a timeout for a whole job

(job.<id>.timeout-minutes设置整个作业的超时)

job.<id>.steps.timeout-minutes sets a timeout for a single step

(job.<id>.steps.timeout-minutes设置单个步骤的超时)

Your scenario:

(您的情况:)

my-job:
 runs-on:  ubuntu-latest
 timeout-minutes: 30

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...