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

Cron Bundle Laravel Package

bordeux/cron-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require bordeux/cron-bundle
    

    Add to config/bundles.php:

    Bordeaux\CronBundle\BordeauxCronBundle::class => ['all' => true],
    
  2. Basic Configuration Edit app/config/config.yml:

    bordeaux_cron:
        jobs:
            my_job:
                command: "app/console my:command"
                schedule: "*/5 * * * *"
    
  3. First Use Case Run the cron manager manually to test:

    php app/console bordeaux:cron:run
    

    Verify logs in var/log/cron.log (ensure monolog is configured).


Implementation Patterns

Workflow Integration

  1. Symfony Console Commands

    • Register a command (e.g., app/console my:command) and reference it in bordeaux_cron.yml:
      jobs:
          my_job:
              command: "app/console my:command --option=value"
              schedule: "@hourly"
      
  2. Sonata Admin Integration

    • Extend SonataAdminBundle to trigger cron jobs via UI:
      // src/Bordeaux/CronBundle/Admin/CronJobAdmin.php
      class CronJobAdmin extends AbstractAdmin {
          protected function configureFormFields(FormMapper $formMapper) {
              $formMapper->add('schedule', 'sonata_type_model_list', [
                  'by_reference' => false,
                  'btn_list' => ['run' => ['link' => 'run']],
              ]);
          }
      }
      
  3. Dynamic Scheduling

    • Use environment variables or API calls to update schedules:
      # .env
      CRON_SCHEDULE_MY_JOB="*/10 * * * *"
      
      Load via config.yml:
      bordeaux_cron:
          jobs:
              my_job:
                  command: "app/console my:command"
                  schedule: "%env(CRON_SCHEDULE_MY_JOB)%"
      
  4. Logging and Monitoring

    • Extend the logger to track job execution:
      // src/Bordeaux/CronBundle/Logger/CronLogger.php
      class CronLogger extends AbstractLogger {
          public function logExecution($jobName, $status) {
              // Custom logic (e.g., database log, Slack alert)
          }
      }
      

Gotchas and Tips

Pitfalls

  1. Deprecated Symfony Versions

  2. Sonata Admin Dependency

    • Requires SonataAdminBundle. If not using Sonata, the UI features are unavailable.
  3. Cron Daemon Requirement

    • The bundle assumes a system cron daemon is configured. For testing, use:
      * * * * * php /path/to/console bordeaux:cron:run >> /dev/null 2>&1
      
  4. Schedule Syntax

    • Uses standard cron syntax. Test schedules with:
      php app/console bordeaux:cron:validate "*/5 * * * *"
      

Debugging

  1. Log Configuration Ensure monolog is set up in app/config/config.yml:

    monolog:
        handlers:
            main:
                type:   stream
                path:   "%kernel.logs_dir%/cron.log"
                level:  debug
    
  2. Job Execution Order

    • Jobs run in alphabetical order by name. Rename or use weights:
      bordeaux_cron:
          jobs:
              z_high_priority_job: { command: "...", schedule: "..." }
              a_low_priority_job:  { command: "...", schedule: "..." }
      
  3. Environment-Specific Schedules Override schedules per environment (e.g., config_test.yml):

    bordeaux_cron:
        jobs:
            my_job:
                schedule: "@daily"  # Overrides config.yml
    

Extension Points

  1. Custom Job Classes Extend Bordeaux\CronBundle\Job\AbstractJob for reusable logic:

    class MyCustomJob extends AbstractJob {
        protected function execute() {
            // Custom logic
            return $this->success("Done!");
        }
    }
    

    Register in config.yml:

    bordeaux_cron:
        jobs:
            custom_job:
                class: AppBundle\Job\MyCustomJob
                schedule: "* * * * *"
    
  2. Pre/Post Hooks Implement Bordeaux\CronBundle\Event\CronEvent listeners:

    // src/AppBundle/EventListener/CronListener.php
    class CronListener {
        public function onCronRun(CronEvent $event) {
            if ($event->getJobName() === 'my_job') {
                // Pre/post logic
            }
        }
    }
    

    Bind in services.yml:

    services:
        app.cron_listener:
            class: AppBundle\EventListener\CronListener
            tags:
                - { name: kernel.event_listener, event: bordeaux.cron.run, method: onCronRun }
    
  3. Database-Backed Schedules Store schedules in a database table (e.g., cron_jobs) and fetch them dynamically:

    // src/Bordeaux/CronBundle/Provider/DatabaseJobProvider.php
    class DatabaseJobProvider implements JobProviderInterface {
        public function getJobs() {
            return $this->entityManager->getRepository(CronJob::class)->findAll();
        }
    }
    

    Configure in config.yml:

    bordeaux_cron:
        job_provider: app.database_job_provider
    
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.
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
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