nginx / unit

NGINX Unit - universal web app server - a lightweight and versatile open source server that simplifies the application stack by natively executing application code across eight different programming language runtimes.
https://unit.nginx.org
Apache License 2.0
5.27k stars 322 forks source link

How to deubg/log when writing njs scripts and so on? #1096

Open BourneSuper opened 5 months ago

BourneSuper commented 5 months ago

hi,

unit version: 1.31.1

i have some questions about scripting:

  1. how to deubg
  2. how to log variables
  3. how to share data between every requests
  4. it's complicated to update js_modules already uesd:
    a. delete 'settings' in config b. delete js_modules c. update js_modules d. update config
hongzhidao commented 5 months ago

Hi,

how to deubg

What are you going to debug? Like code execution?

how to log variables

Did you mean logging variables with njs or access Unit variables with njs?

how to share data between every requests

No such features yet, could you share more about the requirement?

it's complicated to update js_modules.

Indeed, similar to tls section, it was discussed here. https://github.com/nginx/unit/issues/195 For now, there is no clear better way on it. But we plan to make them easier to use.

BourneSuper commented 5 months ago

Hi,

how to deubg

What are you going to debug? Like code execution?

how to log variables

Did you mean logging variables with njs or access Unit variables with njs?

how to share data between every requests

No such features yet, could you share more about the requirement?

it's complicated to update js_modules.

Indeed, similar to tls section, it was discussed here. #195 For now, there is no clear better way on it. But we plan to make them easier to use.

1 & 2 : debug and logging help to konw if a njs script was working or at which line it was stucked.

  1. 'share data between every requests' can make request counting, then make a feature of rate limiting by ip.
  2. control API is RESTFUL but too pure. After all we are not CRUD with pure resources.
lcrilly commented 5 months ago
  1. it's complicated to update js_modules already used

If you use the unitc command line tool to edit a js_module, it will do the necessary steps for you.

$ unitc /js_modules/foo edit
hongzhidao commented 5 months ago

1 & 2 : debug and logging help to konw if a njs script was working or at which line it was tucked.

I would suggest you try it and let me know if we missed something on it. If the JS doesn't work, there is error message thrown like js exception: ....

BourneSuper commented 5 months ago
  1. it's complicated to update js_modules already used

If you use the unitc command line tool to edit a js_module, it will do the necessary steps for you.

$ unitc /js_modules/foo edit

It is s useful tool. It starts nano editor. Can it start vim editor?

BourneSuper commented 5 months ago

1 & 2 : debug and logging help to konw if a njs script was working or at which line it was tucked.

I would suggest you try it and let me know if we missed something on it. If the JS doesn't work, there is error message thrown like js exception: ....

It can throw a exception when error occurs. If there was no syntax error, how could I know if a variable was assigned a right value. I there a funtion like r.log in nginx?

hongzhidao commented 5 months ago

how could I know if a variable was assigned a right value.

What does variable mean here, could you show an example?

I there a funtion like r.log in nginx?

Unit hasn't introduced the request object r yet, we'll extend the njs module features, but it's not clear yet for now.

BourneSuper commented 5 months ago

how could I know if a variable was assigned a right value.

What does variable mean here, could you show an example?

I there a funtion like r.log in nginx?

Unit hasn't introduced the request object r yet, we'll extend the njs module features, but it's not clear yet for now.

A variable is a type of one certain programming language. Its value can change during codes running. For javascript,var count = 0; count is a variable.

lcrilly commented 5 months ago

[unitc] is a useful tool. It starts nano editor. Can it start vim editor?

Yes,

$ unitc --help | grep -i edit
  EDIT                # Opens the URI contents in $EDITOR

Set the EDITOR environment variable to vim.

BourneSuper commented 5 months ago

[unitc] is a useful tool. It starts nano editor. Can it start vim editor?

Yes,

$ unitc --help | grep -i edit
  EDIT                # Opens the URI contents in $EDITOR

Set the EDITOR environment variable to vim.

I don't unstand the help description. cmdline params often start with - or --

sudo  unitc /js_modules/util vim
unitc: ERROR: Invalid option (vim)

sudo  unitc /js_modules/util vi
unitc: ERROR: Invalid option (vi)

sudo  unitc vim /js_modules/util 
unitc: ERROR: Invalid option (vim)

sudo  unitc EDITOR=vim /js_modules/util 
unitc: ERROR: Invalid option (EDITOR=vim)

sudo  unitc $EDITOR=vim /js_modules/util 
unitc: ERROR: Invalid option (=vim)

sudo  unitc EDIT=vim /js_modules/util 
unitc: ERROR: Invalid option (EDIT=vim)
lcrilly commented 5 months ago

Try this

export EDITOR=vim
sudo unitc /js_modules/util edit

Note that edit is one of the general options to unitc which does not require a - prefix. You can think of edit like other HTTP methods used by the Unit Control API (e.g. GET, DELETE, PUT).

BourneSuper commented 5 months ago

Try this

export EDITOR=vim
sudo unitc /js_modules/util edit

Note that edit is one of the general options to unitc which does not require a - prefix. You can think of edit like other HTTP methods used by the Unit Control API (e.g. GET, DELETE, PUT).

this environment variable didn't work. '''shell echo $EDITOR vim ''' I tried vim, Vim, vi, Vi. It still started with nano editor.

lcrilly commented 5 months ago

this environment variable didn't work. '''shell echo $EDITOR vim ''' I tried vim, Vim, vi, Vi. It still started with nano editor.

Very strange. This is how the tool determines which editor to use. You will see it does not assume nano, in fact it falls back to vi as a last resort. https://github.com/nginx/unit/blob/master/tools/unitc#L258

What is the result of this command in a bash shell?

test "$EDITOR" && printf '%s' "$EDITOR" || command -v editor || command -v vim || echo vi

Does it change when you run with sudo?

BourneSuper commented 5 months ago

this environment variable didn't work. '''shell echo $EDITOR vim ''' I tried vim, Vim, vi, Vi. It still started with nano editor.

Very strange. This is how the tool determines which editor to use. You will see it does not assume nano, in fact it falls back to vi as a last resort. https://github.com/nginx/unit/blob/master/tools/unitc#L258

What is the result of this command in a bash shell?

test "$EDITOR" && printf '%s' "$EDITOR" || command -v editor || command -v vim || echo vi

Does it change when you run with sudo?

I debugged this shell script. $EDITOR is empty. It can't get $EDITOR in the first line. So it fallbacked to command -v editor and started nano.

if I wrote a new test script, $EDITOR equals 'vim'. Something wrong with unitc but I don't know where.

Fortunately, it's not C language, so I can modify it to this."

EDITOR=$(test command -v vim || echo vi)
ac000 commented 5 months ago

With sudo you can try either of the following arguments to preserve the environment

-E, --preserve-env Indicates to the security policy that the user wishes to pre‐ serve their existing environment variables. The security pol‐ icy may return an error if the user does not have permission to preserve the environment.

--preserve-env=list Indicates to the security policy that the user wishes to add the comma-separated list of environment variables to those pre‐ served from the user's environment. The security policy may return an error if the user does not have permission to pre‐ serve the environment. This option may be specified multiple times.

BourneSuper commented 5 months ago

With sudo you can try either of the following arguments to preserve the environment

-E, --preserve-env Indicates to the security policy that the user wishes to pre‐ serve their existing environment variables. The security pol‐ icy may return an error if the user does not have permission to preserve the environment.

--preserve-env=list Indicates to the security policy that the user wishes to add the comma-separated list of environment variables to those pre‐ served from the user's environment. The security policy may return an error if the user does not have permission to pre‐ serve the environment. This option may be specified multiple times.

That's right.

sudo -E unitc /js_modules/util edit

or

su root
unitc /js_modules/util edit
lcrilly commented 5 months ago

Sorry for the extra steps @BourneSuper - I was testing with macOS which seems to inherit $EDITOR with sudo(8).

If you are creating a configuration in a local development environment, you may find it more convenient to start unitd with a TCP control socket so that you don't need to use sudo(8) to access the control API.

Use the unitd --control option from the command line, or for Linux-systemd systems add something like this to /etc/default/unit

DAEMON_ARGS="--control 127.0.0.1:8081"

The unitc tool will discover the control socket automatically.