forked from nushell/nu_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob.nu
More file actions
78 lines (71 loc) · 1.88 KB
/
job.nu
File metadata and controls
78 lines (71 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# spawn task to run in the background
#
# please note that a fresh nushell is spawned to execute the given command
# So it doesn't inherit current scope's variables, custom commands, alias definition, except env variables which value can convert to string.
#
# Note that the closure to spawn can't take arguments. And it only supports something like this: { echo 3 }, it have no parameter list.
#
# e.g:
# spawn { echo 3 }
export def spawn [
command: closure # the command to spawn
--label(-l): string # the label of comand
--group(-g): string # the group name to spawn
] {
let config_path = $nu.config-path
let env_path = $nu.env-path
let source_code = (view source $command | str trim -l -c '{' | str trim -r -c '}')
mut args = [
$"nu --config \"($config_path)\" --env-config \"($env_path)\" -c '($source_code)'",
]
if $label != null {
$args = ($args | prepend ["--label", $label])
}
if $group != null {
$args = ($args | prepend ["--group", $group])
}
let job_id = (pueue add -p $args)
{"job_id": $job_id}
}
export def log [
id: int # id to fetch log
] {
pueue log $id -f --json
| from json
| transpose -i info
| flatten --all
| flatten --all
| flatten status
}
# get job's stdout.
export def output [
id: int # id to fetch job's stdout
] {
log $id | get output.0
}
# get job running status
export def status [
--detailed(-d) # need to get detailed stauts?
] {
let output = (
pueue status --json
| from json
| get tasks
| transpose -i status
| flatten
| flatten status
)
if not $detailed {
$output | select id label group Done? status? start? end?
} else {
$output
}
}
# kill specific job
export def kill (id: int) {
pueue kill $id
}
# clean job log
export def clean () {
pueue clean
}