Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Crontask Bundle Laravel Package

axiolab/crontask-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require axiolab/crontask-bundle
    

    Add the bundle to config/bundles.php (Symfony) or AppKernel.php (legacy):

    Axiolab\CrontaskBundle\AxiolabCrontaskBundle::class => ['all' => true],
    
  2. Configure: Edit config/packages/axiolab_crontask.yaml (Symfony 4.4+) or app/config/config.yml (legacy):

    axiolab_crontask:
        commands:
            - 'app:my-cron-job'
            - 'app:another-job'
        schedule: '*/5 * * * *'  # Default: every 5 minutes
    
  3. First Use Case: Create a Symfony command (e.g., src/Command/MyCronJobCommand.php):

    namespace App\Command;
    
    use Symfony\Component\Console\Command\Command;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Output\OutputInterface;
    
    class MyCronJobCommand extends Command
    {
        protected static $defaultName = 'app:my-cron-job';
    
        protected function execute(InputInterface $input, OutputInterface $output): int
        {
            $output->writeln('Running cron job!');
            // Your logic here
            return Command::SUCCESS;
        }
    }
    

    Register the command in config/services.yaml:

    services:
        App\Command\MyCronJobCommand:
            tags: ['console.command']
    

Implementation Patterns

Workflows

  1. Task Registration:

    • Define commands in axiolab_crontask.yaml under commands.
    • Use wildcards (e.g., app:cron-*) to group related tasks.
  2. Dynamic Scheduling:

    • Override the default schedule per task:
      axiolab_crontask:
          commands:
              app:high-priority-job: '@hourly'
              app:low-priority-job: '0 3 * * *'  # Daily at 3 AM
      
  3. Logging and Output:

    • Redirect command output to a file by appending > /path/to/log.txt 2>&1 to the command in the config.
    • Use Symfony’s LoggerInterface in commands for structured logging.
  4. Environment-Specific Configs:

    • Use %kernel.environment% in YAML to switch schedules:
      schedule: '%env(CRON_SCHEDULE)%'
      
    • Load environment variables via .env (e.g., CRON_SCHEDULE="*/10 * * * *").
  5. Integration with Symfony Events:

    • Dispatch events before/after execution via Symfony’s event dispatcher:
      use Symfony\Component\EventDispatcher\EventDispatcherInterface;
      
      class MyCronJobCommand extends Command {
          public function __construct(private EventDispatcherInterface $dispatcher) {}
      
          protected function execute(InputInterface $input, OutputInterface $output): int {
              $this->dispatcher->dispatch(new PreCronEvent());
              // ...
          }
      }
      
  6. Dependency Injection:

    • Inject services into commands for reuse:
      use App\Service\MyService;
      
      class MyCronJobCommand extends Command {
          public function __construct(private MyService $service) {}
      }
      

Gotchas and Tips

Pitfalls

  1. Outdated Package:

    • Last release in 2017 may lack compatibility with modern Symfony (5.4+). Test thoroughly.
    • Check for deprecated Symfony components (e.g., Console\Command changes in Symfony 6+).
  2. Cron Syntax Errors:

    • Invalid schedules (e.g., */9 * * * *) will silently fail. Validate with crontab.guru.
    • Ensure the server’s cron daemon is configured to run the Symfony command:
      * * * * * /path/to/php /path/to/bin/console axiolab:crontask:run
      
  3. Logging Overhead:

    • Commands run via the bundle may not inherit Symfony’s default logging. Explicitly configure loggers:
      # config/packages/monolog.yaml
      handlers:
          cron:
              type: stream
              path: var/log/cron.log
              level: debug
      
  4. Command Not Found:

    • Verify commands are tagged as console.command in services.yaml.
    • Clear cache after adding new commands:
      php bin/console cache:clear
      
  5. Race Conditions:

    • The bundle doesn’t handle overlapping executions. Use locks (e.g., flock or symfony/lock) in commands:
      use Symfony\Component\Lock\LockFactory;
      
      $lock = $lockFactory->createLock('my_cron_job_lock', 300);
      if (!$lock->acquire()) {
          throw new \RuntimeException('Lock acquired by another process');
      }
      

Tips

  1. Testing:

    • Mock the CrontaskManager in tests:
      $manager = $this->createMock(CrontaskManager::class);
      $manager->method('run')->willReturn(true);
      $this->container->set('axiolab_crontask.manager', $manager);
      
  2. Extending the Bundle:

    • Override the CrontaskManager service to add custom logic:
      # config/services.yaml
      services:
          App\Service\CustomCrontaskManager:
              decorates: 'axiolab_crontask.manager'
              arguments: ['@.inner']
      
  3. Performance:

    • For long-running tasks, use Symfony’s Process component to run commands asynchronously:
      use Symfony\Component\Process\Process;
      
      $process = new Process(['php', 'bin/console', 'app:long-job']);
      $process->start();
      
  4. Debugging:

    • Enable verbose output for the bundle’s command:
      php bin/console axiolab:crontask:run --verbose
      
    • Check the last_execution field in the database (if using the last_execution_time feature).
  5. Alternatives:

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed