mirror of
https://github.com/gechandesu/runcmd.git
synced 2026-01-23 14:54:13 +03:00
feat: contexts support
This commit is contained in:
35
examples/command_with_cancel.v
Normal file
35
examples/command_with_cancel.v
Normal file
@@ -0,0 +1,35 @@
|
||||
import context
|
||||
import runcmd
|
||||
import time
|
||||
|
||||
fn main() {
|
||||
// Create context with cancel.
|
||||
mut bg := context.background()
|
||||
mut ctx, cancel := context.with_cancel(mut bg)
|
||||
|
||||
// Create new command with context.
|
||||
mut cmd := runcmd.with_context(ctx, 'sleep', '120')
|
||||
|
||||
// Start a command.
|
||||
println('Start command!')
|
||||
cmd.start()!
|
||||
|
||||
// Sleep a bit for demonstration.
|
||||
time.sleep(1 * time.second)
|
||||
|
||||
// Cancel command.
|
||||
//
|
||||
// In a real application, cancel() might be initiated by the user.
|
||||
// For example, a command might take too long to execute and need
|
||||
// to be canceled.
|
||||
//
|
||||
// See also command_with_timeout.v example.
|
||||
println('Cancel command!')
|
||||
cancel()
|
||||
|
||||
// Wait for command.
|
||||
cmd.wait()!
|
||||
|
||||
// Since command has been terminated, the state would be: `signal: 15 (SIGTERM)`
|
||||
println('Child state: ${cmd.state}')
|
||||
}
|
||||
46
examples/command_with_cancel_custom.v
Normal file
46
examples/command_with_cancel_custom.v
Normal file
@@ -0,0 +1,46 @@
|
||||
import context
|
||||
import runcmd
|
||||
import time
|
||||
|
||||
fn main() {
|
||||
// Create context with cancel.
|
||||
mut bg := context.background()
|
||||
mut ctx, cancel := context.with_cancel(mut bg)
|
||||
|
||||
// Create new command as usual.
|
||||
mut cmd := runcmd.new('sleep', '120')
|
||||
|
||||
// Set the context...
|
||||
cmd.ctx = ctx
|
||||
|
||||
// ...and custom command cancel function.
|
||||
cmd.cancel = fn [mut cmd] () ! {
|
||||
if cmd.process != none {
|
||||
println('Killing ${cmd.process.pid()}!')
|
||||
cmd.process.kill()!
|
||||
}
|
||||
}
|
||||
|
||||
// Start a command.
|
||||
println('Start command!')
|
||||
cmd.start()!
|
||||
|
||||
// Sleep a bit for demonstration.
|
||||
time.sleep(1 * time.second)
|
||||
|
||||
// Cancel command.
|
||||
//
|
||||
// In a real application, cancel() might be initiated by the user.
|
||||
// For example, a command might take too long to execute and need
|
||||
// to be canceled.
|
||||
//
|
||||
// See also command_with_timeout.v example.
|
||||
println('Cancel command!')
|
||||
cancel()
|
||||
|
||||
// Wait for command.
|
||||
cmd.wait()!
|
||||
|
||||
// Since command has been killed, the state would be: `signal: 9 (SIGKILL)`
|
||||
println('Child state: ${cmd.state}')
|
||||
}
|
||||
27
examples/command_with_timeout.v
Normal file
27
examples/command_with_timeout.v
Normal file
@@ -0,0 +1,27 @@
|
||||
import context
|
||||
import runcmd
|
||||
import time
|
||||
|
||||
fn main() {
|
||||
// Create context with cancel.
|
||||
mut bg := context.background()
|
||||
mut ctx, _ := context.with_timeout(mut bg, 10 * time.second)
|
||||
|
||||
// Create new command with context.
|
||||
mut cmd := runcmd.with_context(ctx, 'sleep', '120')
|
||||
|
||||
// Start a command.
|
||||
started := time.now()
|
||||
println('Start command at ${started}')
|
||||
cmd.start()!
|
||||
|
||||
// Wait for command.
|
||||
cmd.wait()!
|
||||
|
||||
// The `sleep 120` command would run for two minutes without a timeout.
|
||||
// But in this example, it will time out after 10 seconds.
|
||||
println('Command finished after ${time.now() - started}')
|
||||
|
||||
// Since command has been terminated, the state would be: `signal: 15 (SIGTERM)`
|
||||
println('Child state: ${cmd.state}')
|
||||
}
|
||||
Reference in New Issue
Block a user