tourze/symfony-lock-command-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tourze/symfony-lock-command-bundle

Composer 安装命令:

composer require tourze/symfony-lock-command-bundle

包简介

A Symfony bundle that provides locking mechanism for console commands to prevent concurrent execution

README 文档

README

English | 中文

Latest Version Total Downloads

A Symfony bundle that provides locking mechanism for console commands to prevent concurrent execution.

Features

  • Automatic command locking based on command class and input parameters
  • Prevents multiple instances of the same command from running simultaneously
  • Easy integration with Symfony console commands
  • Configurable lock duration (default: 60 minutes)
  • Automatic lock release after command completion or termination
  • Intelligent lock key generation using command class name and input parameters
  • Graceful command disabling when lock cannot be acquired
  • Comprehensive logging for lock acquisition and release events

Installation

You can install the package via composer:

composer require tourze/symfony-lock-command-bundle

Quick Start

  1. Register the bundle in your config/bundles.php:
return [
    // ...
    Tourze\LockCommandBundle\LockCommandBundle::class => ['all' => true],
];
  1. Create a lockable command by extending LockableCommand:
<?php

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Tourze\LockCommandBundle\Command\LockableCommand;

class YourCommand extends LockableCommand
{
    protected static $defaultName = 'app:your-command';

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        // Your command logic here
        $output->writeln('Command is running...');
        
        // Simulate some work
        sleep(5);
        
        $output->writeln('Command completed successfully!');
        return Command::SUCCESS;
    }
}

The command will automatically be locked based on the command class name and input parameters.

How It Works

  • When a command extends LockableCommand, the bundle automatically generates a unique lock key based on the command class name and input parameters
  • Lock keys are generated using MD5 hash of JSON-encoded arguments and options
  • Lock duration is set to 60 minutes by default
  • If a lock cannot be acquired, the command is disabled and a warning is logged
  • Locks are automatically released when the command terminates

Configuration

The bundle uses Symfony's Lock component with the default lock factory. You can configure the lock store in your Symfony configuration:

# config/packages/lock.yaml
framework:
    lock:
        # Configure your preferred lock store
        # Examples: flock, semaphore, redis, etc.
        default: 'flock'

Advanced Usage

Custom Lock Key Generation

You can override the getLockKey() method to customize lock key generation:

<?php

use Symfony\Component\Console\Input\InputInterface;
use Tourze\LockCommandBundle\Command\LockableCommand;

class CustomLockCommand extends LockableCommand
{
    protected static $defaultName = 'app:custom-lock';

    public function getLockKey(InputInterface $input): ?string
    {
        // Custom lock key logic
        $userId = $input->getArgument('user-id');
        return 'custom_command_' . $userId;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        // Command logic
        return Command::SUCCESS;
    }
}

Returning Null to Disable Locking

If you want to conditionally disable locking for certain scenarios, return null from getLockKey():

public function getLockKey(InputInterface $input): ?string
{
    // Disable locking for admin users
    if ($input->getOption('admin')) {
        return null;
    }
    
    return parent::getLockKey($input);
}

Testing

Run the tests with:

./vendor/bin/phpunit packages/symfony-lock-command-bundle/tests

Test Commands

The bundle includes the following test commands for development and testing purposes:

  • test:lockable - A test command that demonstrates the locking functionality. This command is used in the test suite to verify that the lock mechanism works correctly.

Contributing

Please see CONTRIBUTING.md for details.

License

The MIT License (MIT). Please see License File for more information.

tourze/symfony-lock-command-bundle 适用场景与选型建议

tourze/symfony-lock-command-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.04k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 04 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 tourze/symfony-lock-command-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tourze/symfony-lock-command-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 tourze/symfony-lock-command-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.04k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 22
  • 依赖项目数: 7
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-08