最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Flask的Celery的log中显示:Stale pidfile exists – Removing it和指定celerybeat.pid位置

celery crifan 4555浏览 0评论

之前用supervisor和gunicorn把Flask的app部署到在线环境,其中包括Celery。

而之前就注意到:

本地PyCharm+终端调试Flask的app时,运行Celery的worker就在项目根目录中生成过

celerybeat.pid

就想要去掉。

后来在线上的log:

logs/celery-beat-robotDemo_CeleryBeat-stderr.log

中又发现:

Stale pidfile exists – Removing it

所以需要去:

指定Celery的pidfile

且要搞清楚:

此处celery,除了worker还有beat:

conf/supervisor/supervisord_server.conf

<code>[program:robotDemo_CeleryWorker]
command=/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/bin/celery worker -A resources.tasks.celery
directory=/xxx/robotDemo

[program:robotDemo_CeleryBeat]
command=/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/bin/celery beat -A resources.tasks.celery -s /xxx/robotDemo/runtime/celerybeat-schedule
directory=/xxx/server/robotDemo
</code>

所以要清楚这个pidfile是针对哪个的,还是针对celery整体的

感觉是要特殊指定pidfile的位置,估计就可以了。

celery pidfile location

“Stale pidfile exists. Removing it.” message in Celery 3.0 · Issue #923 · celery/celery

Detach pidlock test must release pidfile. Closes #923 · celery/celery@d932783

Celery 3.0 and “Stale pidfile exists. Removing it.” message

linux – Stale PID file after reboot – Stack Overflow

How to change celerybeat.pid file path? · Issue #3828 · celery/celery

好像是通过:

<code>command=/path/to/project/bin/celery beat -A celeryapp --loglevel=INFO --pidfile="/var/run/celery/celerybeat.pid"
</code>

去指定celery的pid

Daemonization — Celery 4.2.0 documentation

<code>$ celery multi start worker1 \
    -A proj \
    --pidfile="$HOME/run/celery/%n.pid" \
    --logfile="$HOME/log/celery/%n%I.log"

$ celery multi restart worker1 \
    -A proj \
    --logfile="$HOME/log/celery/%n%I.log" \
    --pidfile="$HOME/run/celery/%n.pid

$ celery multi stopwait worker1 --pidfile="$HOME/run/celery/%n.pid"
</code>

“* CELERY_CREATE_DIRS

Always create directories (log directory and pid file directory). Default is to only create directories when no custom logfile/pidfile set.

* CELERY_CREATE_RUNDIR

Always create pidfile directory. By default only enabled when no custom pidfile location set.”

去加上配置试试:

本来打算把celery的pid放到项目的runtime子目录中的

后来发现:

http://docs.celeryproject.org/en/latest/userguide/daemonizing.html

<code>CELERYD_PID_FILE="/var/run/celery/%n.pid"
</code>

都是放在/var/run中的

包括supervisor也是

所以那就都去放到/var/run/celery.pid中

不过对于此处Mac本地调试,也要先去看看Mac是否有/var/run目录

是有的:

<code>➜  xxx git:(master) ✗ls /var/run/   
WDCCBKeyMonitor                                  cupsd                                            resolv.conf
WDCCBKeyMonitor.comm                             diskarbitrationd.pid                             socketfilterfw.launchd
WDCCBKeyMonitor.pub                              displaypolicyd                                   syslog
appfwd.pid                                       fudinit                                          syslog.pid
automount.initialized                            hdiejectd.pid                                    systemkeychaincheck.done
com.apple.AssetCache                             mDNSResponder                                    systemkeychaincheck.socket
com.apple.WindowServer.didRunThisBoot            mds                                              usbmuxd
com.apple.loginwindow.didRunThisBoot             mds.pid                                          utmpx
com.apple.softwareupdate.availableupdatesupdated portmap.socket                                   vpncontrol.sock
cron.pid                                         pppconfd                                         wifi
➜  xxx git:(master) ✗
</code>

不过又看到:

celery beat -A resources.tasks.celery -s runtime/celerybeat-schedule

中-s指定的文件,是可以生成的:

所以也要去顺带搞清楚:

celery beat 的-s参数的逻辑和位置

celery beat -s

celery beat parameter

celery.beat — Celery 3.1.7 文档

celery.bin.beat — Celery 4.2.0 documentation

“-s, –schedule

Path to the schedule database. Defaults to celerybeat-schedule. The extension ‘.db’ may be appended to the filename. Default is {default}.”

巧了也看到了:

http://docs.celeryproject.org/en/latest/reference/celery.bin.beat.html#cmdoption-celery-beat-pidfile

“–pidfile

File used to store the process pid. Defaults to celerybeat.pid.

The program won’t start if this file already exists and the pid is still alive.”

就是此处的默认项目根目录下的:

celerybeat.pid

文件,正要去指定位置呢。

现在清楚了:

之前想要指定文件celerybeat.pid的位置 

-》 其实只是celery的beat的参数而已

-》和celery的worker没关系

那正好再去看看celery的worker有哪些参数:

celery.bin.worker — Celery 4.2.0 documentation

结果也是有–pidfile的

http://docs.celeryproject.org/en/latest/reference/celery.bin.worker.html#cmdoption-celery-worker-pidfile

“–pidfile

Optional file used to store the process pid.

The program won’t start if this file already exists and the pid is still alive.”

不清楚celery的worker和beat,都有–pidfile

是分别不同,还是用同一个文件?

celery worker beat –pidfile

celery worker beat  what is pidfile

celery   what is pidfile

Celery Beat daemonization with systemd (clarify docs?) · Issue #4304 · celery/celery

好像是,需要使用到:systemd的system service去daemonize,才会用到:

celery/daemonizing.rst at fcec01f6e041a70e5ddd061beba5fccb32d74e24 · celery/celery

才会去设置–pidfile?

celery – Disable pidfile for celerybeat – Stack Overflow

其中想要禁止pidfile的话,可以用:

<code>--pidfile=
</code>

即:设置为空,即可禁止。

不过提到了:celerybeat需要–pidfile,而celeryd不需要

django – Running celery as daemon does not create PID file – Stack Overflow

至此,有点懂了:

之前看到官网一直提到的:

Daemonizing

指的是用systemd,变成了系统可以通过:

systemctl或service去控制celeryd

其中可以指定–pidfile

而此处的,supervisor配置文件中的:

<code>[program:robotDemo_CeleryWorker]
command=/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/bin/celery worker -A resources.tasks.celery
[program:robotDemo_CeleryBeat]
command=/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/bin/celery beat -A resources.tasks.celery -s /xxx/robotDemo/runtime/celerybeat-schedule
</code>

都只是celery,不是celeryd

然后其中对于celery worker,好像不需要指定–pidfile

而对于celery beat,才需要指定–pidfile

其中:celery的worker和beat,都支持–pidfile这个参数而已

debian – Celery Django celerybeat.pid permissions – Stack Overflow

Celery教程————-以守护进程方式运行worker – CSDN博客

上面文章感觉是翻译自官网教程:

celery/daemonizing.rst at fcec01f6e041a70e5ddd061beba5fccb32d74e24 · celery/celery

django celery daemon does work: it can’t create pid file – Stack Overflow

至此,先去只给beat添加指定pidfile:

conf/supervisor/supervisord_server.conf

<code>[program:robotDemo_CeleryBeat]
command=/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/bin/celery beat -A resources.tasks.celery --pidfile /var/run/celerybeat.pid -s /xxx/runtime/celerybeat-schedule
</code>

看看效果:

supervisorctl stop all

supervisorctl reload

supervisorctl restart all

然后再去看看:

log中果然没了:Stale pidfile exists

对应的新生成了:

/var/run/celerybeat.pid

文件:

<code>[root@xx-general-01 logs]# ls -lh /var/run/celerybeat.pid
-rw-r--r-- 1 root root 6 Aug 30 10:42 /var/run/celerybeat.pid
</code>

至此,算是基本上实现了我们要的效果了。

【总结】

此处,感觉是:

celery的worker不需要指定–pidfile

celery的beat需要指定–pidfile:

对应的supervisor中

conf/supervisor/supervisord_server.conf

配置是:

<code>[program:robotDemo_CeleryWorker]
command=/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/bin/celery worker -A resources.tasks.celery
...
[program:robotDemo_CeleryBeat]
command=/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/bin/celery beat -A resources.tasks.celery --pidfile /var/run/celerybeat.pid -s /xxx/runtime/celerybeat-schedule
</code>

其中用:

<code>--pidfile /var/run/celerybeat.pid
</code>

指定了celery的beat的pidfile的位置。

-》这样:

  • log中就不会有 Stale pidfile exists – Removing it 提示了。

    • 而之前会有这个提示,估计是:

    • 在线环境用supervisorctl stop all,退出celery后,自动删除了服务器上的celerybeat.pid

    • 但是此处用fab代码部署时,又把Mac本地项目根目录下的celerybeat.pid传到服务器上了

    • 所以服务器中supervisorctl restart all,重启celery时,检测到旧的,没用的,过期的celerybeat.pid,所以才提示Stale pidfile exists – Removing it

  • 以及不会在当前项目根目录下生成这个celerybeat.pid了

转载请注明:在路上 » 【已解决】Flask的Celery的log中显示:Stale pidfile exists – Removing it和指定celerybeat.pid位置

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.208 seconds, using 22.20MB memory