Twisted: spawn an application daemon from another application daemon - twisted

I have a Twisted application daemon which is started by twistd. This daemon acts as a daemon manager to manage many other Twisted application daemons. I can send the manager a "new" command via TCP and the manager should spawn(fork) another Twisted application daemon. My question is how can I do that? After fork, I think I should stop the manager's reactor loop, close opened file descriptors(logging file), then run the new Twisted application daemon. I want to know the details of how to do these things properly.

You should use the reactor's spawnProcess method. See http://twistedmatrix.com/documents/current/core/howto/process.html for details on its use.
You should not fork or do any of those other steps yourself. They are difficult to do correctly, and spawnProcess already does them for you.
If you're curious about how these things are done, you can browse the various implementations.

Related

Running a Raku Cro app as a persistent service

I'd like to run a perl6/raku Cro app as a service behind a frontend webserver.
Just running cro run won't handle restarting after segfaults & reboots.
Previously with perl5 I've used FastCGI - however Cro::HTTP::Server's Cro::HTTP::Server.new().start() idiom doesn't look compatible with FastCGI::Native's while $fcgi.accept() {} example.
The service.p6 generated by cro stub does have a SIGINT handler, however I'm unsure whether this is sufficient to point to it in a systemctl service, i.e.
[Service]
ExecStart = /path/to/service.p6
How are people currently hosting Cro apps?
cro run is intended as a development tool, not a deployment one, and so is indeed not a good choice for hosting the services.
All of the Cro services I directly take care of are containerized (some guidance on that here) and then run on a hosted Kubernetes cluster. Kubernetes takes care of the automatic restarts, rolling out new versions, etc. I'm also aware of docker-compose being used in place of Kubernetes, which I guess works, though I believe that's also considered as primarily a development tool.
Setting it up as a systemctl service should also work fine, provided that's configured to always restart. However, it seems that you'd want to handle SIGTERM for the clean shutdown to work instead of SIGINT (nothing wrong with handling both).
I do also place a frontend web server in front of Cro (using Apache, though nginx would be a fine choice too), and also use that to do some caching of static content also (using content-control in my routes to describe the cachability).

init.d values for separate redis-server instances

I need to clear a concept. I have two redis servers running on a single VM. Server#1 connects via TCP, server#2 connects via a UNIX socket. I'm on the cusp of converting the TCP server to UNIX as well.
An excerpt from the init.d script for server#1 is:
DAEMON=/usr/bin/redis-server
DAEMON_ARGS=/etc/redis/redis.conf
NAME=redis-server
DESC=redis-server
RUNDIR=/var/run/redis
PIDFILE=$RUNDIR/redis-server.pid
The comparable excerpt from the init.d script for server#2 is (which has its own config):
DAEMON=/usr/bin/redis-server
DAEMON_ARGS=/etc/redis/redis-2.conf
NAME=redis2-server
DESC=redis2-server
RUNDIR=/var/run/redis
PIDFILE=$RUNDIR/redis2-server.pid
Both servers are currently up and running. My question is: how come DAEMON is kept the same for both servers? Why wasn't a separate executable needed?
I configured the two servers using config from various internet forums. While it works, I've failed to understand the significance of the DAEMON value, given it remains the same for both server instances. Is it because the executable is fed different config files, and this the same DAEMON is able to handle multiple server instances? Being a beginner, I'd really like some expert opinion on this. Thanks in advance.
Open terminal (or cmd). Now open it again. You have two copies open, but they are both using the same executable.
You're doing the same with redis: DAEMON is just saying where to find the program, and since you're happy to use the same version of redis for both, you can use the same path for both DAEMON values, each instance of which has its own ID stored in the PIDFILE, which is why they need to be different paths or they will interfere with each other.

VB.NET/WIN32/WMI Best way to monitor opening network connections

I'm developing my own antimalware application. I have successfully implemented file system and process monitoring and now I'm looking for "some kind of firewall feature". I have already done some research and found iphlpapi.dll, netstat -o parsing.
Exactly, I would like to receive event with following data, when new IP (TCP/UDP/whatever) connection is opened.
Remote IP address
Protocol (TCP/UDP/...)
Port
ID of process which opened connection
I don't want to run timer which still checks netstat output, because it's not effective. I need to suspend process (I know how to suspend process) fastly, when connection to malicious IP is detected.
I suspect that besides writing a driver the only way to do this is Event Tracing for Windows (ETW). Check .NET System.Diagnostics.Eventing
About ETW https://msdn.microsoft.com/en-us/library/windows/desktop/aa363668(v=vs.85).aspx
In the ETW you can start from the following three providers:
Microsoft-Windows-NDIS-PacketCapture
Microsoft-Windows-TCPIP
Microsoft-Windows-Networking-Correlation

Calling CFRunLoop family on a Cocoa application

I have a cocoa application in which I would like to use UNIX domain sockets to communicate with a system-wide daemon.
In a first step, my client will connect to the daemon and then listen for incoming log statements from the daemon.
The example I found, CFLocalServer from Apple, uses CFSockets to communicate between two command line utilities. I was able to set the daemon part of the code nicely, because I am free to decide what function to call in my daemon ([[NSRunLoop currentLoop] run] or CFRunLoopRun()). However, I have doubts about the client, which is a cocoa application.
In the code sample, the client first create them, connects, adds them as source to the current CFRunLoop for listening operations and then calls CFRunLoopRun(); until cancelled by CFRunLoopStop(CFRunLoopGetCurrent());
My question is: can I do all that inside a block on a serial dispatch queue created by an Objective-C cocoa application without interfering with the NSRunLoop in my application's main thread?
Yes, you can do that, although you may want to simply attach the socket to the main run loop and let it run normally (i.e. don't call CFRunLoopRun() since its already running).
I would also look at GCDAsyncSocket which is a very nice wrapper for all of this kind of work if your needs are complicated. It doesn't currently have support for unix domain sockets, but you can use jdiehl's socketUN branch which adds it.

start/stop tomcat using maven

How can i start/stop remote tomcat using maven. I am using cargo plugin which helps me in deploying the application , but doesn't provides the functionality to start/stop the remote tomcat.
Indeed, You can NOT start and stop Tomcat running remotely using Cargo, only deploy and undeploy your web application.
Actually, to my knowledge, there is currently nothing allowing to do this out of the box.
As explained here, the only way to make server "A" start or stop a service like Tomcat when the request comes from client "B" is that yet-another service needs to be available and already running on server "A". [...] and I don't know if such a service is available.
In this message, someone is describing such a solution (based on a socket listener) that you could maybe use (by doing some telnet through maven) but the message is quite old so it's likely outdated and the link pointing to the code seems to be dead. I didn't check out the whole thread, maybe there are other ideas.
If you are using windows, remote service sharing is another possible solution as described here. But, again, this would require some work on your side.
From a security standpoint, it's only possible in this way...
Linux: use an SCP or script via SSH Client (putty), then '$CATALINA_HOME/bin/shutdown.sh'
Windows: use sc command, like "sc \192.168.10.10 stop tomcat6"
Quick and clean!
You can try to use the maven tomcat plugin or if it does not give you everything you need, you can always use an ant task here is a reference on the task
You can use Cargo Daemon web-application. It runs on the remote machine and can start/stop tomcat for you (as well as deploy an app). You just need to configure Cargo plugin and call mvn:daemon-start. Here is the link: http://cargo.codehaus.org/Cargo+Daemon. It's easier to start with provided Cargo Daemon archetype: http://cargo.codehaus.org/Maven2+Archetypes#Maven2Archetypes-daemon
Try this useful Plugin
Afterwards try this:
mvn tomcat:start
and
mvn tomcat:stop