Amber icon indicating copy to clipboard operation
Amber copied to clipboard

[Feature] Spawning and terminating processes

Open FedericoCeratto opened this issue 1 year ago • 15 comments

One of the features that keeps me in bash land is the quick (and dirty) process management for development, e.g.:

./my_service &
sleep 1
run_local_tests_against_my_service
killall my_service

Having simple unobtrusive process management in Amber would be very nice!

FedericoCeratto avatar Jun 11 '24 09:06 FedericoCeratto

@FedericoCeratto that’d be totally on the list to do. Great feature request!

Ph0enixKM avatar Jun 11 '24 12:06 Ph0enixKM

Probably a good idea to have a way to refer to the spawned process by ID rather than the brute-force killall <name>. Something like:

let pid = spawn $ some command $
...do some stuff...
kill pid

Sod-Almighty avatar Jul 07 '24 23:07 Sod-Almighty

Probably a good idea to have a way to refer to the spawned process by ID rather than the brute-force killall <name>. Something like:

let pid = spawn $ some command $
...do some stuff...
kill pid

maybe as builtin so every command executed in Amber in the script you have the PID saved in a array (internally) so from Amber side you can kill any process you executed.

Mte90 avatar Jul 09 '24 08:07 Mte90

But most commands run synchronously.

I suppose it might make sense for commands that fork themselves and run asynchronously as a daemon. docker for example, or sshd.

Perhaps this syntax?

$ sshd $ as pid1
....some code....
kill pid1

$ docker run -d somecontainer $ as pid2 failed {
  echo "oh no"
  exit 1
}
....some code....
kill pid2

Sod-Almighty avatar Jul 09 '24 09:07 Sod-Almighty

But most commands run synchronously.

That's true, save the pid it will be very nice for the cases you mentioned.

Mte90 avatar Jul 09 '24 09:07 Mte90

The as keyword is already used to cast values. How about with?

$ sshd $ with pid1
kill pid1

$ docker run -d somecontainer $ with pid2 failed {
  echo "oh no"
  exit 1
}
kill pid2

Ph0enixKM avatar Jul 09 '24 17:07 Ph0enixKM

Mm......"with" doesn't sound right. You're not creating it with a variable, you're storing a handle. I know as is used for casting, but it's not used there for casting. Why can't it be used in both contexts?

Sod-Almighty avatar Jul 09 '24 17:07 Sod-Almighty

Mm......"with" doesn't sound right. You're not creating it with a variable, you're storing a handle. I know as is used for casting, but it's not used there for casting. Why can't it be used in both contexts?

It could be used there for casting. In fact it's used in the standard library. How about just proc, process, pid keyword instead?

Ph0enixKM avatar Jul 09 '24 19:07 Ph0enixKM

Same syntax, just pid instead of as?

$ sshd $ pid x failed {
  exit 1
}
....some code....
kill x

Sod-Almighty avatar Jul 09 '24 19:07 Sod-Almighty

@b1ek @Mte90 @FedericoCeratto @CymDeveloppement do you have better ideas for this syntax?

Ph0enixKM avatar Jul 10 '24 14:07 Ph0enixKM

I like to use just pid it is more simpler and compact.

Mte90 avatar Jul 10 '24 14:07 Mte90

Also what do you all think about this syntax that would work like the Python's with:

// Runs the following command in the background:
with $ sshd $ {
  // Do some code
} failed {
  exit 1
}
// The command `sshd` is killed here once the code inside of the `with` block is completed

Alternatively:

with $ sshd $? {
  // Do some code
}

Ph0enixKM avatar Jul 10 '24 15:07 Ph0enixKM

I like it. Perhaps with an async option for programs that don't fork:

with async $ sleep 1000 $ {     // run in background
   ...do something...
}   // command is terminated

Sod-Almighty avatar Jul 10 '24 15:07 Sod-Almighty

Any movement on this?

Sod-Almighty avatar Jan 05 '25 20:01 Sod-Almighty

I'll add it to the list for the upcoming version

Ph0enixKM avatar Jan 06 '25 13:01 Ph0enixKM