ibra-akv/docker-jobs-bundle

提供Docker批处理系统的Symfony包。

安装: 31

依赖者: 0

建议者: 0

安全: 0

星标: 21

关注者: 3

分支: 1

类型:symfony-bundle

6.0.1 2022-08-21 10:12 UTC

This package is auto-updated.

Last update: 2024-09-29 05:43:19 UTC


README

提供带有Docker的批处理系统的Symfony包。

使用Docker容器来运行和处理您的作业。

需要了解

此包启动的所有容器默认在主机网络上。
因此,如果您需要连接到本地数据库,可以使用常用的localhost|127.0.0.1

Docker配置

为了能够连接,必须将Docker引擎API暴露在本地端口上。

1. 编辑默认位于/lib/systemd/system/docker.servicedocker.service文件(在Debian上)

从以下内容

# /lib/systemd/system/docker.service
...
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
...

到以下内容

# /lib/systemd/system/docker.service
...
ExecStart=/usr/bin/dockerd
...
2. 编辑/etc/docker/daemon.json以在127.0.0.1:2375上暴露Docker API

在JSON文件中添加以下内容

{
  ...
  "hosts": ["fd://", "tcp://127.0.0.1:2375"]
  ...
}
3. 完全重启Docker
systemctl daemon-reload
systemctl restart docker
service docker restart

Docker镜像

如果尚未创建,您必须创建一个Docker镜像,该镜像将用于执行您的作业。

此包允许为每个作业使用不同的镜像,但在启动时未指定Docker镜像时,需要默认镜像。

安装

在安装时,必须指定与您的项目Symphony版本相对应的包的正确版本。

# Symfony 6
composer require ibra-akv/docker-jobs-bundle:~6.0

# Symfony 5
composer require ibra-akv/docker-jobs-bundle:~5.0

# Symfony 4
composer require ibra-akv/docker-jobs-bundle:~4.0

# Symfony 3
composer require ibra-akv/docker-jobs-bundle:~3.0

配置

配置过程有点长,请按照以下所有步骤操作。


作业实体

您必须创建您的作业实体并扩展BaseJob类。

<?php

namespace App\Entity;

use IterativeCode\DockerJobsBundle\Entity\BaseJob;

class Job extends BaseJob
{
}

只要您扩展了BaseJob,包就会正确工作。

包配置

一旦您准备好了作业实体和Docker镜像,最后一步是配置包。

如果尚未完成,请将包包含到您的项目中

<?php
# Symfony 4
# ./project_dir/config/bundles.php

return [
 Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
 ...
 IterativeCode\DockerJobsBundle\DockerJobsBundle::class => ['all' => true],
];

# Symfony 3
# ./project_dir/app/AppKernel.php

$bundles = array(
 new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
 new Symfony\Bundle\SecurityBundle\SecurityBundle(),
 ...
 new IterativeCode\DockerJobsBundle\DockerJobsBundle(),
)

Yaml配置

# Symfony 4
# ./project_dir/config/packages/docker_jobs.yaml

# Symfony 2 - 3
# ./project_dir/app/config/config.yml

docker_jobs:
 class:
   job: App\Entity\Job # Required

 docker:
   # The URI where Docker Engine API is exposed
   # The bundle will use this endpoint to communicate with Docker
   docker_api_endpoint: 'http://127.0.0.1:2375' # optional (default: https://:2375)

   # Default docker image ID for job execution
   # You can specify docker image when creating a job, this image will be used if no image is specified at creation.
   default_image_id: '6c39ebee77c9'         # Required

   # This parameter is required to avoid eventual errors
   # When starting a container for your job, the user will cd into this directory.
   container_working_dir: '/opt/symfony-project'        # Required

   # This parameter is used to convert docker container dates to my timezone.
   # Docker by default uses UTC+00:00 time, me who's residing in France, i need to add 1 hour to it for it to be correct (UTC+01:00).
   # You can use this parameter to add or remove hours to adapt it to your timezone.
   time_difference: '+1 hour' # Optional (default: +1 hour)
   # time_difference: '+3 hours'
   # time_difference: '-2 hours'

 runtime: # Optional
   # This is a default concurrency limit value
   # This value can be overridden in the job orchestrating command.
   concurrency_limit: 4 # Optional (default: 4)

到此为止,您应该已经导入了包并开始工作。
接下来,您需要配置作业编排命令以持续运行。
请参阅以下步骤。

下一步

  • 请参阅控制台命令以启动系统。
    此步骤对于启动作业容器的编排至关重要。

  • 请参阅监控仪表板,如果您愿意,可以激活它。

许可证