我正在作业中进行一些处理,最终执行外部 shell 命令。该命令正在执行一个需要几个小时才能完成的脚本。
问题是,在我使用 spawn 启动脚本后和 detach如果我使用kill -15 信号关闭sidekiq 作业,脚本将停止执行。仅当 sidekiq 触发 spawn
命令时才会发生此行为 - 如果我在 irb 中执行此操作并关闭控制台,则不会发生此行为。所以不知何故它似乎仍然绑定(bind)到 sidekiq - 但为什么以及如何避免它?。
test.sh
#!/bin/bash
for a in `seq 1000` ; do
echo "$a "
sleep 1
done
spawn_test_job.rb
module WorkerJobs
class SpawnTestJob < CountrySpecificWorker
sidekiq_options :queue => :my_jobs, :retry => false
def perform version
logfile = "/home/deployer/test_#{version}.log"
pid = spawn(
"cd /home/deployer &&
./test.sh
",
[:out, :err] => logfile
)
Process.detach(pid)
end
end
end
我将作业排入队列WorkerJobs::SpawnTestJob.perform_async(1)
,如果我跟踪test_1.log
,我可以看到我的计数器正在运行。然而,当我发送 sidekiq Kill -15 时,计数器停止并且脚本 pid 消失。
最佳答案
经过几个小时的调试,我最终发现是 systemd 导致了这个问题。 sidekiq 内部启动的进程获得了 sidekiq cgroup
,每当您终止进程时,默认的 killmode
都是 control-group
。
deployer@srv-14:~$ ps -efj | grep test.sh
UID PID PPID PGID SID C STIME TTY TIME CMD
deployer 16679 8455 16678 8455 0 12:59 pts/0 00:00:00 grep --color=auto test.sh
deployer 24904 30861 24904 30861 0 12:52 ? 00:00:00 sh -c cd /home/deployer && ./test.sh
deployer 24906 24904 24904 30861 0 12:52 ? 00:00:00 /bin/bash ./test.sh
deployer 6382 1 6382 6382 38 12:53 ? 00:02:14 sidekiq 4.2.10 my_proj [8 of 8 busy]
deployer 7787 1 7787 7787 30 12:46 ? 00:04:07 sidekiq 4.2.10 my_proj [6 of 8 busy]
deployer 13680 1 13680 13680 29 12:49 ? 00:03:08 sidekiq 4.2.10 my_proj [8 of 8 busy]
deployer 14372 1 14372 14372 38 12:49 ? 00:03:48 sidekiq 4.2.10 my_proj [8 of 8 busy]
deployer 16719 8455 16718 8455 0 12:59 pts/0 00:00:00 grep --color=auto sidekiq
deployer 17678 1 17678 17678 38 12:50 ? 00:03:22 sidekiq 4.2.10 my_proj [8 of 8 busy]
deployer 18023 1 18023 18023 32 12:50 ? 00:02:49 sidekiq 4.2.10 my_proj [8 of 8 busy]
deployer 18349 1 18349 18349 34 12:43 ? 00:05:32 sidekiq 4.2.10 my_proj [8 of 8 busy]
deployer 18909 1 18909 18909 34 12:51 ? 00:02:53 sidekiq 4.2.10 my_proj [8 of 8 busy]
deployer 22956 1 22956 22956 39 12:01 ? 00:22:42 sidekiq 4.2.10 my_proj [8 of 8 busy]
deployer 30861 1 30861 30861 46 12:00 ? 00:27:23 sidekiq 4.2.10 my_proj [8 of 8 busy]
和
cat /proc/24904/cgroup
11:perf_event:/
10:blkio:/
9:pids:/system.slice
8:devices:/system.slice/system-my_proj\x2dsidekiq.slice
7:cpuset:/
6:freezer:/
5:memory:/
4:cpu,cpuacct:/
3:net_cls,net_prio:/
2:hugetlb:/
1:name=systemd:/system.slice/system-my_proj\x2dsidekiq.slice/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fb9682a48b899491d688929f9e90928abbc2d5889e898d92989e" rel="noreferrer noopener nofollow">[email protected]</a>
我通过指示我的 sidekiq 服务 KillMode
为 process
引用文献:
关于ruby-on-rails - sidekiq 停止时,由 sidekiq 生成的进程也会停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47417628/