cron

Running 'php artisan schedule:run' for Laravel in Kubernetes CronJobs

I am working on integrating a few Laravel PHP applications into a new Kubernetes architecture, and every now and then we hit a little snag. For example, the app developers noticed that when their cron job ran (php artisan schedule:run), the MySQL container in the cluster would drop an error message like:

2019-03-27T16:20:05.965157Z 1497 [Note] Aborted connection 1497 to db: 'database' user: 'myuser' host: '10.0.76.130' (Got an error reading communication packets)

In Kubernetes, I had the Laravel app CronJob set up like so:

Running Drupal Cron Jobs in Kubernetes

There are a number of things you have to do to make Drupal a first-class citizen inside a Kubernetes cluster, like adding a shared filesystem (e.g. PV/PVC over networked file share) for the files directory (which can contain generated files like image derivatives, generated PHP, and twig template caches), and setting up containers to use environment variables for connection details (instead of hard-coding things in settings.php).

But another thing which you should do for better performance and traceability is run Drupal cron via an external process. Drupal's cron is essential to many site operations, like cleaning up old files, cleaning out certain system tables (flood, history, logs, etc.), running queued jobs, etc. And if your site is especially reliant on timely cron runs, you probably also use something like Ultimate Cron to manage the cron jobs more efficiently (it makes Drupal cron work much like the extensive job scheduler in a more complicated system like Magento).

Cron Troubles in Drupal? Here's a Tip

I recently had a problem with cron on one of my Drupal sites, related to an issue I'll be posting about soon (namely, using Drupal's built-in search with more than 4.5 million entries in the search_index table). When I manually tried running cron, I got a "Cron failed" status message. I checked the log, and it said "Attempting to re-run cron while it is already running."

Apparently, something had messed up (probably due to the fact that I was messing with database tables while cron was working on them) with the previous cron job, and Drupal couldn't start a new cron job anymore. So, with the handy help of Google, I found part of a solution to my cron woes in the Drupal.org forums. Here's what I did to prevent this cron error from happening again: