How to access parent global variable in child process nodejs - node.js

I have the following code:
import ChildProcess = require("child_process");
global.abc = "token";
ChildProcess.spawn("node", [path.join(process.cwd(), "./install-db.js")]);
install-db.js in this file I am not able to get global variable, How should I use global.abc in this child process

As child process is a separate entity, you can't excess your main process's global variable inside it.
Though there are ways to send data/inputs to child processes. You can use command line arguments to send data to child process.
Read more about passing arguments to child process: https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

Related

"require" sub processes; how to stop them?

I want to send a class instance to a sub process that shall operate on the class and then later stop the process.
I have used the require for the module and sent the class instance as a parameter to an init function in the required module. This works as such, but it I want not to restart the complete program I cannot find a way to this.
I have limited experience from javascript. I did check the child_process functions but I newer got it to work. Also I tried something described here on stackoverflow aswell (see code).
const myChildProgram = require("./myModule");
myClassInst = new myClass();
myChildProgram.init(myClassInst); //initialize and run sub processes this command launches other async processes.
//later in time/code
//stop all processes generated after the myChildProgram.init()
delete require.cache[require.resolve('./myModule')]; //not working
Would like to be able to stop the processes generated from the myChildProgram.init() call
It appears that you have modules and subprocesses confused. A module is a block of javascript code that has been loaded into your current node process. There is no way to stop that code from the outside unless you kill your whole node process.
If you want a module to stop doing something it was doing, then the usual solution would be to export a function from that module that, when called, would execute code within the module to stop whatever it was doing.
We can only help more specifically if you show the actual code for the module and the operation that you want to stop.

NodeJS — Fork Child Process with function string instead of file

I've looked at the documentation for the fork method, and it only describes providing a file path to the child module file.
Does anyone know if it is possible (and undocumented) to pass in the child module directly instead of via a file? Point being, I would like to dynamically generate the module, then create a child process with it.
This would not be possible -- fork() creates a completely different process that do not share context or variables with its parent process.
One option you have would be to generate the module inside the forked process, and passing it the necessary arguments via the command line or via a temporary file so that your child can run:
const data = 'something;
var childProcess = child_process.fork(__dirname + '/worker', [something]);
You can then access the arguments from the child using process.argv[2].
One limitation of that approach is that you can only pass data types, and cannot call from the worker any function in the context of its parent. You would need for that some kind of RPC between the child and the parent, which is beyond the scope of this answer.

Reshare CLONE_NEWNS after unshare

I have part of applicatoin which unshare CLONE_NEWNS to have private mount namespace in the process. Code is similar to unshare code snippet.
How to reverse effect of this unshare? I want to share the parent namespace again.
Get the original namespace fd before calling unshare(), then, after unshare(), you can switch back by calling setns(). If the original namespace is not altered by current process or by its parent process, you don't even need to get the fd beforeahead, you can get it anytime by open /proc/$ppid/ns/mnt (corresponding with your CLONE_NEWNS)

How to set a relationship from one NSManageObject to another in a different NSManagedObjectContext

Because my app is multi-threaded I use two NSManagedObjectContexts. The main context, that runs in the main thread and another context that only runs in a separate thread.
I have created a small test app. It has two Core Data Entities. Parent and Child. Parent has a one-to-many relationship to Child. Child has a 1-1 relationship to Parent.
In one test method (that runs in a separate thread) I get a Parent instance, that has been created during a run time before. So it's in the main context. I get this parent with this line of code:
Parent *tmpParent = [[parentController selectedObjects] objectAtIndex:0];
Then I create some children in the thread-context (managedObjectContextInBackground), set their parent to the tmpParent, give them a name and save the thread-context:
Child *child1 = (Child *)[NSEntityDescription insertNewObjectForEntityForName:#"Child" inManagedObjectContext:managedObjectContextInBackground];
[child1 setName:#"Homer"];
[child1 setParent:tmpParent];
Child *child2 = (Child *)[NSEntityDescription insertNewObjectForEntityForName:#"Child" inManagedObjectContext:managedObjectContextInBackground];
[child2 setName:#"Wilma"];
[child2 setParent:tmpParent];
[self saveManagedObjectContextInBackground];
If I execute that method the application crashes and says:
Illegal attempt to establish a relationship 'parent' between objects in different contexts
That's why I added this line below the tmpParent declaration:
[managedObjectContextInBackground insertObject:tmpParent];
But, the application crashes again saying:
An NSManagedObject may only be in (or observed by) a single NSManagedObjectContext
I looked through the documentation, but I couldn't find a way to solve this problem.
Question: How can I set the relationship of Child to Parent, when Parent is in a different NSManagedObjectContext, than Child ??
Your object can be loaded from another context, thus, when you create a new NSManagedObject in your other thread, nothing stops you from fetching an existing NSManagedObject which may or may not be being used in the main thread, and use that reference in order to set up your relationship.
When you exit the thread and merge the contexts, this change will get updated for the object that was until this point happily existing in memory on the main thread.

Convert call from spawn to fork-exec in C

I have code which looks like this in Linux:
return_code= spawnp(cmd, 3, fd_map, NULL, argv, environ);
I need to convert this from QNX to Linux - so I need to use fork-exec since spawn is not available in Linux.
1) How can that be done ? Is this right ?
pid = fork();
if (pid ==0) /* child */
exec(cmd, argv, environ);
2) How do I pass the parameters fd_map and "3" which are passed in spawn to exec ?
I don't know what "3" does.
If you want to change the file descriptors available to the child process, you do not do this in the call to exec or fork, but you do it between by calling close, dup2, etc. The function posix_spawn basically does this for you, and on Linux/glibc, it is implemented using fork and exec (so you can read the source code...)
pid = fork();
if (!pid) {
// close, dup2 go here
exec(...);
// error
}
The 3 indicates the number of file descriptors you are passing into the fd_map and in the spawnp() call it allows you to conveniently select only those file descriptors you want to pass along to the child process.
So after your call to fork() you will have all of the file descriptors in the child process so you can close out those file descriptors you aren't interested in and then, assuming that the file descriptors are not marked as CLOEXEC (close on exec) they will also carry through to the exec()'ed code.
Note that the fork() will fail however if your application is multi-threaded since, until recent versions, QNX doesn't support forking threaded processes.

Resources