unit icon indicating copy to clipboard operation
unit copied to clipboard

Run C++ Application with NGINX Unit

Open junsoo999 opened this issue 2 years ago • 9 comments

Hello, I am newbie in this filed, and I am working on binding my C++ application and NGINX Unit to implement the fast Web Application Server.

But C++ language is not supported in NGINX Unit, so I made the wrapper function with other languages (Go, Python, Java). And it was confirmed that the wrapper function work well alone.

However, when I use NGINX Unit. it always aborted in C++ side. I passed all the environment variables like LD_LIBRARY_PATH, but I still get the Segmentation Fault. I realized that all the Segmentation Fault occurs at File I/O, which is work well without NGINX Unit.

Can I get some advise to handle this problem? To avoid the permission issue, I ran the NGINX Unit in "user": "root" but it did not help. Segmentation Fault or exceptions are called when the C++ code open some files...

junsoo999 avatar May 16 '23 06:05 junsoo999

Hi @junsoo999 thanks for reaching out.

So you are basically saying you are implementing the libunit.a functionality from out C example app in C++? Just wanted to be clear. The best way to get started with this is a github repo where you put you good so other can have a look on it.

Another area I would suggest is maybe porting your C++ to Webassembly? We are working on a Webassembly runtime for Unit and this could be easier for you to migrate.

tippexs avatar May 16 '23 09:05 tippexs

I'm working on documenting libunit, which provides a C API to write C applications with unit. All other language modules are more or less based on this API, since of course they need to interface with C at some point. I recommend that you use this libunit.

I have a repository with a simple program (extracted from the C example app that @tippexs mentioned), with a Makefile and all the boilerplate to be able to compile it independently. I don't have that repo in a public server, but I'll hopefully publish it later this week (probably on Sunday or so). That should help you build a similar C++ app, since C++ is mostly compatible with C.

alejandro-colomar avatar May 17 '23 07:05 alejandro-colomar

Thank you for your response!

I think I should wait for your libunit C example, which is similar with my case. It would be great if there are some exmaple codes for binding NGINX Unit and C/C++ applications.

junsoo999 avatar May 17 '23 09:05 junsoo999

Hi!

I've pushed the changes. Please check the repo at http://www.alejandro-colomar.es/src/alx/alx/nginx/unit-c-app.git

git clone git://www.alejandro-colomar.es/src/alx/alx/nginx/unit-c-app.git

Then you can use this example JSON:

$ cat c.json 
{
	"listeners": {
		"*:80": {
			"pass": "applications/c"
		}
	},
	"applications": {
		"c": {
			"type": "external",
			"working_directory": "/home/alx/tmp/unit/c/",
			"executable": "/home/alx/src/nginx/unit-c-app/tmp/unit_c_app",
			"user": "alx",
			"arguments": ["-t", "0"]
		}
	}
}

Please write if you find any issues. I'll try to improve that repo to make it easy to write new C/C++ applications or modules based on it.

I'll also send you in the following days the drafts for the manual pages I'm writing.

Cheers, Alex

alejandro-colomar avatar May 20 '23 13:05 alejandro-colomar

Oh, and I forgot to mention (I'll need to document this in that repo): You'll need to install libunit from the NGINX Unit repo prior to compiling the app in my repo. To do that, run the following:

./configure
make -j build/lib/libunit.a 
sudo make libunit-install 

alejandro-colomar avatar May 20 '23 13:05 alejandro-colomar

Thanks to the documentation, I finally integrate my C++ application with libunit.a library.

Thank you for your kind documentation!

junsoo999 avatar May 25 '23 16:05 junsoo999

I have one additional question while implementing C++ application.

The nxt_unit_app_test.c example code calls ready_handler when the unit reloads. Are there any functions that do the opposite?

I think there is also a function called close_handler, but it seems to be used in a websocket, so I wonder if there is another function.

junsoo999 avatar May 26 '23 06:05 junsoo999

@junsoo999 sorry for the long delay! We have released the ability to run WebAssembly modules on Unit.

@alejandro-colomar can you answer the remaining open questions.

tippexs avatar Sep 06 '23 07:09 tippexs

@junsoo999

The nxt_unit_app_test.c example code calls ready_handler when the unit reloads. Are there any functions that do the opposite?

Yes, callback.quit is a hook to quit the application gracefully. But it's also optional.

hongzhidao avatar Oct 17 '23 13:10 hongzhidao