tuxcoder/pid_manager

PHP的PIDManager,防止命令重复运行

dev-master 2015-06-02 23:22 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:39:11 UTC


README

描述

此捆绑包旨在防止同时运行同一个命令两次。

例如,当您使用cronjob处理大量邮件时,可能会发生旧实例仍在发送邮件,同时第二次启动命令的情况。

###功能 功能保持简单。在执行命令之前,PIDManager会将当前进程ID(PID)写入配置的文件(pid_path)。在第二次运行之前,它会检查此文件,如果进程仍在运行则返回。

###警告 目前此捆绑包仅在Linux上工作,因为它通过procfs(/proc/$pid)检测正在运行的进程。

使用方法

###使用Symfony2

在AppKernel.php中添加以下行

new TuxCoder\PIDManagerBundle\PIDManagerBundle(),

在config.yml中添加以下行

pid_manager:
  commands:
    - name: swiftmailer:spool:send
      pid_path: /run/user/symfony/mail_spool.pid
    - name: secoundCommandName
      pid_path: /path/to/otherPidFile.pid

###非Symfony2 $pidManager=new \TuxCoder\PIDManagerBundle\PIDManager(); $pidManager->setPidPath('/path/to/pid/file'); if(!$pidManager->isRunning()) { $pidManager->setRunning();

  //do some cool stuff

  $pidManager->setNotRunning();
} else {
  //error program is running
}

待办事项

  • 编写一些测试
  • 为"AllreadyRunningException"添加事件处理器。
  • 如果需要,也可以使其在Windows、Mac、BSD上可运行。