3slab/vdm-library-ftp-transport-bundle

Vdm Ftp 传输

2.0.1 2024-01-02 16:17 UTC

This package is auto-updated.

Last update: 2024-08-31 00:27:59 UTC


README

不再维护:请使用 VdmLibraryFlysystemTransportBundle,配合 VdmLibraryBundle v3.x

此源代码可以收集来自 FTP 服务器的数据。

配置参考

framework:
    messenger:
        transports:
            consumer:
                dsn: "sftp://user:password@sftp.fr:2222"
                retry_strategy:
                    max_retries: 0
                options:
                    monitoring:
                        enabled: true
                    mode: move
                    ftp_options:
                        dirpath: path/to/your/files/
                        storage: path/to/your/storage/

自定义 ftp 执行器

自定义 ftp 执行器允许您自定义调用 ftp 服务器的方式。如果您需要在文件上执行不同的操作,则这是必需的。

只需在您的项目中创建一个扩展 Vdm\Bundle\LibraryFtpTransportBundle\Executor\AbstractFtpExecutor 的类。它将自动替换默认执行器。

如果您有两个自定义执行器。只有一个将被使用,第二个将被忽略。

namespace App\FtpExecutor;

use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Envelope;
use Vdm\Bundle\LibraryBundle\Model\Message;
use Vdm\Bundle\LibraryFtpTransportBundle\Executor\AbstractFtpExecutor;
use Vdm\Bundle\LibraryBundle\Stamp\StopAfterHandleStamp;

class CustomFtpExecutor implements AbstractFtpExecutor
{
    /** 
     * @var LoggerInterface 
    */
    private $logger;

    public function __construct(LoggerInterface $logger) 
    {
        parent::__construct();
        $this->logger = $logger;
    }

    public function execute(array $files): iterable
    {
        $files = array_filter($files, function($file) {
            return (isset($file['type']) && $file['type'] === 'file');
        });

        foreach ($files as $key => $file) {
            $file = $this->ftpClient->get($file);
            $message = new Message($file);

            yield $this->getEnvelope($files, $key, $message);
        }

        yield new Envelope(new Message(""), [new StopAfterHandleStamp()]);
    }

    private function getEnvelope(array $files, int $key, Message $message): Envelope
    {
        $stamps = [];

        // Put the stop stamp on the last file
        if (array_key_last($files) === $key) {
            $stamps = [new StopAfterHandleStamp()];
        }

        return new Envelope($message, $stamps);
    }
}

您的自定义执行器需要做两件重要的事情

  • 产生一个带有 VDM 消息实例的新信封
  • 如果您想在处理完最后一个文件后停止,请在产生的信封中添加一个 StopAfterHandleStamp 标记(如果不添加,消息传递器的工作循环会再次执行它)。

注意:多亏了产生系统,您可以在执行函数中实现循环,并一次返回一个项目

注意:您可以在自定义执行器中保持状态,以便如果再次执行,则适应您的 ftp 调用

监控

如果您启用监控,它将跟踪以下指标

  • FTP 文件主体的大小
  • 计数 ftp 错误