job-runner/symfony-console-adapter

1.4.0 2024-11-12 14:15 UTC

README

Build Status Type Coverage Type Coverage Latest Stable Version License

This package provides a symfony/console adapter for JobRunner.

Installation

composer require job-runner/symfony-console-adapter

Usage

<?php

declare(strict_types=1);

use JobRunner\JobRunner\Job\CliJob;
use JobRunner\JobRunner\Job\JobList;
use JobRunner\JobRunner\CronJobRunner;
use JobRunner\JobRunner\SymfonyConsole\SymfonyConsoleEventListener;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;

require 'vendor/autoload.php';

(new SingleCommandApplication())
    ->setName('My Super Command') // Optional
    ->setVersion('1.0.0') // Optional
    ->addOption('bar', null, InputOption::VALUE_REQUIRED)
    ->setCode(function (InputInterface $input, OutputInterface $output) {
        $jobCollection = new JobList();
        $jobCollection->push(new CliJob('php ' . __DIR__ . '/tutu.php', '* * * * *'));
        $jobCollection->push(new CliJob('php ' . __DIR__ . '/titi.php', '* * * * *', 'sample'));
        $jobCollection->push(new CliJob('php ' . __DIR__ . '/titi.php', '1 1 1 1 1', 'hehe'));
        $jobCollection->push(new CliJob('php ' . __DIR__ . '/arg.php', '* * * * *'));

        $section = $output->section();

        CronJobRunner::create()
            ->withEventListener(new SymfonyConsoleEventListener($section, new Table($section)))
            ->run($jobCollection);

    })
    ->run();