[英]How to use forever-monitor with Electron-Angular project?
I am using Angular 2 with Electron and want to keep running a process in background to show notifications. 我将Angular 2与Electron一起使用,并希望继续在后台运行进程以显示通知。 I am using forever-monitor for that, it works only in development mode, but when I package my app using electron-packager, this stops working. 我为此使用了永远的监视器 ,它只能在开发模式下工作,但是当我使用电子打包程序打包我的应用程序时,它将停止工作。 My code looks like that: 我的代码如下所示:
main.ts 主要
exports.runBackgroundProcess = () => {
// Run a background process forever
var forever = require('forever-monitor');
var child = new(forever.Monitor)('src/assets/notification-process.js',
{
env: {ELECTRON_RUN_AS_NODE: 1},
options: []
});
child.start();
}
I wrote a function in main.ts that will run background process when called from angular component. 我在main.ts中编写了一个函数,当从角度组件中调用该函数时将运行后台进程。 Code in notification-process.js is following: notification-process.js中的代码如下:
notification-process.js notification-process.js
notifier = require('node-notifier')
notifierFun = (msg) => {
notifier.notify({
title: 'Notify Me',
message: msg,
wait: true
});
}
var CronJob = require('cron').CronJob;
new CronJob('* * * * * *', function() {
notifierFun("Message from notification process");
});
Finally I am calling the function from app.component.ts 最后我从app.component.ts调用函数
let main_js = this.electronService.remote.require("./main.js");
main_js.runBackgroundProcess();
I don't think it is a good idea to set your script in the assets directory. 我认为在资产目录中设置脚本不是一个好主意。 I would prefer it to be packaged as an extra resource. 我希望将其打包为额外的资源。
the next snippet will permit to launch your node process 下一个代码段将允许启动您的节点进程
var child_process = require('child_process');
var child = child_process.fork('notification-process.js',[],{
cwd : 'resources'
});
If it does not work once packaged, this may be involved because your files have not been packaged .To package it as an extra resource, modify package.json as follow : this will package webserver folder to resources/webserver folder: 如果打包后不起作用,则可能是因为您的文件尚未打包。要打包它作为额外的资源,请按如下所示修改package.json:这会将webserver文件夹打包到resources / webserver文件夹:
"target": [
"win": {
"target": "nsis",
"icon": "build/icon.ico",
"extraResources" : [{
"from" : "webserver",
"to" : "webserver"}
]
},
for reference, have a look at : https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options 供参考,请参阅: https : //nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options
That's how it worked: 那是这样的:
1- Moved notification-process.js file from assets folder to main directory. 1-将notification-process.js文件从assets文件夹移至主目录。
2- Changed file path in main.js: 2-更改了main.js中的文件路径:
var child = new (forever.Monitor)(path.join(__dirname, 'notification-process.js')...
Without using join, it doesn't work after packaging the app. 如果不使用join,则打包应用程序后将无法使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.