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

babymarkt/cron-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require babymarkt/cron-bundle
    

    Enable the bundle in config/bundles.php:

    Babymarkt\Symfony\CronBundle\BabymarktCronBundle::class => ['all' => true],
    
  2. Basic Configuration (config/packages/babymarkt_cron.yaml):

    babymarkt_cron:
      cronjobs:
        my_first_job: 'app:command-name'
    
  3. First Sync:

    php bin/console babymarkt-cron:sync --env=prod
    

First Use Case

Define a job to run a Symfony command (e.g., app:send-daily-emails) every hour:

babymarkt_cron:
  cronjobs:
    send_emails:
      command: 'app:send-daily-emails'
      minutes: 0
      hours: '*'

Implementation Patterns

Workflow Integration

  1. Command-Based Jobs:

    • Use Symfony console commands as cron jobs (e.g., app:generate-reports).
    • Pass arguments dynamically:
      babymarkt_cron:
        cronjobs:
          generate_reports:
            command: 'app:generate-reports'
            arguments: ['--user-id=123', '--format=csv']
      
  2. Environment-Specific Jobs:

    • Sync only for production:
      php bin/console babymarkt-cron:sync --env=prod
      
    • Exclude dev/staging environments via disabled: true in config.
  3. Logging and Output:

    • Redirect output to a file (e.g., /var/log/cron.log):
      babymarkt_cron:
        options:
          output:
            file: '/var/log/cron.log'
            append: true
      
  4. Dependency Management:

    • Ensure jobs depend on other services (e.g., database migrations):
      babymarkt_cron:
        cronjobs:
          run_migrations:
            command: 'doctrine:migrations:migrate'
            minutes: 0
            hours: 3
      

Advanced Patterns

  1. Conditional Jobs:

    • Disable jobs via config:
      babymarkt_cron:
        cronjobs:
          backup_db:
            command: 'app:backup-db'
            disabled: true  # Disabled until manually enabled
      
  2. Custom Scripts:

    • Use script option to override bin/console:
      babymarkt_cron:
        options:
          script: './custom-script.sh'
      
  3. Multi-Environment Blocks:

    • Use id to manage separate crontab blocks per environment:
      babymarkt_cron:
        options:
          id: 'myapp_prod'
      

Gotchas and Tips

Common Pitfalls

  1. Permissions:

    • Ensure the cron user (e.g., www-data) has write access to:
      • Project directory (chmod -R 755 var/).
      • Log files (if redirecting output).
    • Fix "access denied" errors by running:
      sudo crontab -u www-data -e
      
  2. Cron Syntax Errors:

    • Validate cron expressions using crontab.guru.
    • Debug with babymarkt-cron:dump before syncing:
      php bin/console babymarkt-cron:dump --env=prod
      
  3. Environment Mismatches:

    • Always specify --env when syncing to avoid mixing environments:
      php bin/console babymarkt-cron:sync --env=prod  # Never use 'dev' in production!
      
  4. Output Redirection:

    • Avoid /dev/null in production for debugging:
      babymarkt_cron:
        options:
          output:
            file: '/var/log/myapp_cron.log'
      

Debugging Tips

  1. Check Existing Crontab:

    crontab -l -u www-data  # Replace 'www-data' with your cron user
    
  2. Test Locally:

    • Use babymarkt-cron:report (requires Doctrine) to track job executions:
      php bin/console babymarkt-cron:report
      
  3. Manual Overrides:

    • Temporarily disable a job by commenting its line in /tmp/crontab.<user> (auto-generated by the bundle).

Extension Points

  1. Custom Command Handlers:

    • Extend Babymarkt\Symfony\CronBundle\Command\SyncCommand to add pre/post-sync logic.
  2. Dynamic Config:

    • Load cronjobs from a database or API by overriding the CronJobLoader service.
  3. Sudo Support:

    • Enable sudo for root-level commands:
      babymarkt_cron:
        options:
          crontab:
            sudo: true
      

Laravel-Specific Notes

  1. Artisan Commands:

    • Replace my:symfony:command with Laravel’s command:name format:
      babymarkt_cron:
        cronjobs:
          optimize: 'optimize:clear'
      
  2. Environment Variables:

    • Use Laravel’s .env variables in cronjobs by referencing them in arguments:
      babymarkt_cron:
        cronjobs:
          send_emails:
            command: 'mail:send'
            arguments: ['--queue=high', '--env=${APP_ENV}']
      
  3. Service Providers:

    • Register the bundle in config/app.php under providers:
      Babymarkt\Symfony\CronBundle\BabymarktCronBundle::class,
      
  4. Queue Workers:

    • For Laravel queue-based jobs, ensure the queue worker is running:
      php artisan queue:work --daemon
      
    • Schedule the worker itself via cron:
      babymarkt_cron:
        cronjobs:
          queue_worker:
            command: 'queue:work'
            minutes: '*'
            hours: '*'
            disabled: false
      
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.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity