当sidekiq停止时,sidekiq产生的进程将被停止

时间:2017-11-21 16:11:39

标签: ruby-on-rails ruby sidekiq

我在一个最终执行外部shell命令的作业中进行一些处理。该命令正在执行一个需要数小时才能完成的脚本。

问题是,在使用spawndetach启动脚本后,如果我使用kill -15信号关闭sidekiq作业,脚本将停止执行。仅当sidekiq触发spawn命令时才会发生此行为 - 如果我在irb中执行此操作并关闭控制台,则不会发生此行为。所以不管怎样它似乎仍然与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消失。

1 个答案:

答案 0 :(得分:1)

经过数小时的调试后,我最终发现systemd造成了这种情况。在sidekiq内部启动的流程获得了sidekiq cgroup,每当您终止流程时,默认killmodecontrol-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/my_proj-sidekiq@9.service

我通过指示我的sidekiq服务KillModeprocess

来解决问题

参考文献: