检查子进程是否正在运行 [英] Checking if a child process is running

查看:120
本文介绍了检查子进程是否正在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个门户,其中一个页面列出了Octopus Deploy的发行版。但是,每个版本所需的一些信息都保存在数据存储区(共享驱动器)的zip内。

I've built a portal, one of the pages lists releases from Octopus Deploy. However, some information needed about each release is held inside its zip in a data store (shared drive).

我目前是从Octopus的API获取版本,然后检查 mongo db ,其中已经存储了已经访问过的zip文件中的信息,因此我们仅需一次读取zip文件,这是第一次创建它们。

I currently get releases from Octopus's API then check a mongo db where ve stored information from the zip files ive already accessed so we only have to read the zip files once, the first time they are created.

目前,此逻辑全部正常,但是当它第一次读取zip文件时,它完全被阻塞了。因此,我把它放在一个运行良好的子进程中。

The logic for this currently all works however when it's reading the zip files for the first time it is completely blocking. So ive put it within a child process which runs and works great.

当前,所有这些逻辑都在用户每次点击发布页面时运行。如果八达通部署的所有结果都在数据库中,则它仅返回那些结果。如果不是,它将返回结果,然后在子进程将其插入数据库后,使用socket io返回从zip文件读取的信息。

Currently, the logic for all of this runs every time a user hits the release page. If all results from octopus deploy are in the database then it just returns those results. If not it'll return the results it has then used socket io to return the information read from the zip files once the child process has inserted it into the database.

现在实际问题/问题。每次用户点击页面时,如果用户A点击页面并从八达通获得不在数据库中的版本,则每次点击都会运行该页面,它将启动子进程来读取zip。如果用户B在用户A的子进程完成之前访问该页面,他们还将从八达通那里获得一个不在数据库中的版本,因此将尝试击中该子进程以与用户A进行相同的操作。读取zip文件被阻止,它将等到从用户A读取所有zip文件,然后为用户B重复该过程。

Now the actual problem/question. As its run every time the user hits the page if user A hits the page and gets a release from octopus that is not in the database it will start the child process to read the zip. If user B hits the page before user A's child process has finished they will also get a release from octopus that is not in the database and so will try and hit the child process to do the same thing as user A. However because the code to read the zip file is blocking it'll wait until all the zip files are read from user A then repeat the proccess for user B.

我将如何检查孩子

我已经尝试设置一个'inuse'变量并在何时检查它,如果进程已经在执行zip读取,如果没有执行则执行其他操作?

Ive tried setting a 'inuse' variable and checking it when trying to perform the read zips function in the child process and also in the module that calls it. Trouble is in the child process the zip reading functions blocks it, once it's complete sets the in-use variable to false and so the queued call runs the same thing. In the module its loaded every time the user loads the page so its reset to the default value of false so the read zip function is always called.

推荐答案

检查退出事件监听器。
http://nodejs.org/api/child_process.html#child_process_event_exit

check exit event listener. http://nodejs.org/api/child_process.html#child_process_event_exit

然后,您将某种逻辑作为某种全局对象来维护状态。就像我们在启动子级时设置一个标志,并在ext事件触发时将其设置为关闭一样。

Then you make some logic as some sort of global object to maintain the state. Like we set a flag when we start the child and set it to off when ext event fires.

这篇关于检查子进程是否正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆