Browse Source

feat(job-regist): new version

ask for the solution to launch a script that will be monitored
pull/1850/head
nprimo 1 year ago committed by Niccolò Primo
parent
commit
c102e69e8e
  1. 35
      sh/tests/job-regist_test.sh
  2. 10
      sh/tests/solutions/job-regist.sh
  3. 26
      subjects/devops/job-regist/README.md

35
sh/tests/job-regist_test.sh

@ -2,7 +2,7 @@
# create a function to be called everytime the process exit
abort () {
rm exp.log job.log
rm exp.log job.log job.sh submitted expected
}
trap 'abort' EXIT
@ -16,7 +16,11 @@ script_dirS=$(cd -P "$(dirname "$BASH_SOURCE")" &>/dev/null && pwd)
SUBMITTED='student/job-regist.sh'
EXPECTED='solutions/job-regist.sh'
wait_bg_jobs () {
create_random_job () {
echo "sleep $((1 + $RANDOM % 10))" > job.sh
}
wait_test_case () {
while [ -n "$(jobs | grep -i running)" ]; do
echo -n "."
sleep 1
@ -26,35 +30,26 @@ wait_bg_jobs () {
# test cases
one_process () {
sleep 2 &
source $script_dirS/$1
create_random_job
bash $script_dirS/$1 job.sh
}
two_processes () {
sleep 3 &
sleep 4 &
source $script_dirS/$1
wrong_numb_arguments () {
bash $script_dirS/$1
}
one_process_and_suspend () {
sleep 5 &
source $script_dirS/$1
kill -STOP %1
sleep 2
kill -CONT %1
}
# end of test cases
challenge () {
echo "testing $1 case"
$1 $SUBMITTED &
$1 $EXPECTED &
$1 $SUBMITTED > submitted &
$1 $EXPECTED > expected &
wait_bg_jobs
wait_test_case
diff <(cat exp.log) <(cat job.log)
diff <(cat submitted) <(cat expected)
}
challenge one_process
challenge two_processes
challenge one_process_and_suspend
challenge wrong_numb_arguments

10
sh/tests/solutions/job-regist.sh

@ -2,10 +2,16 @@
IFS='
'
PID=$(jobs -l %1 | grep -Eo '[\+\-] [0-9]+' | grep -Eo '[0-9]+')
if [[ "$#" -ne 1 ]]; then
echo "Error: wrong number of arguments!"
exit 1
fi
bash "$1" &
LOG_FILE="exp.log"
while kill -0 "$PID" 2> /dev/null; do
while [[ -n $(jobs | grep -iv done) ]]; do
echo $(date +"%F %T") - $(jobs %1) >> "$LOG_FILE"
sleep 1
done

26
subjects/devops/job-regist/README.md

@ -2,32 +2,26 @@
### Instruction
Create a script, `job-regist.sh`, that will be able to monitor a specific background job.
Create a script, `job-regist.sh`, that will be able to launch and monitor a specific job.
The script needs to track the status of the first jobs spawned by the current terminal and save the status into a `job.log` file every second. It must append the new update without modifying the previous content.
The script needs to launch a specific binary and track its status saving it into a `job.log` file every second. It must append the new update without modifying the previous content.
Each update needs to be appended to the file with the current format: `YYYY-MM-DD hh:mm:ss - <job status>`
The script should stop running as soon as the job it is tracking ends.
If the script is used with a wrong number of argument it should exit with a status code `1` and print the following error message: `"Error: wrong number of arguments!"`.
### Usage
Here an example of how the script should behave:
```console
$ sleep 10 &
$ sleep 12 &
$ source job-regist.sh
$ cat job.log
2023-02-08 10:37:50 - [1]+ Running sleep 10 &
2023-02-08 10:37:51 - [1]+ Running sleep 10 &
2023-02-08 10:37:52 - [1]+ Running sleep 10 &
2023-02-08 10:37:53 - [1]+ Running sleep 10 &
2023-02-08 10:37:54 - [1]+ Running sleep 10 &
2023-02-08 10:37:55 - [1]+ Running sleep 10 &
2023-02-08 10:37:56 - [1]+ Running sleep 10 &
2023-02-08 10:37:57 - [1]+ Running sleep 10 &
2023-02-08 10:37:58 - [1]+ Running sleep 10 &
2023-02-08 10:37:59 - [1]+ Running sleep 10 &
$ cat job.sh
sleep 2
$ bash job-regist.sh job.sh
$ cat exp.log
2023-02-28 09:02:09 - [1]+ Running ...
2023-02-28 09:02:10 - [1]+ Running ...
$
```

Loading…
Cancel
Save