Skip to content

Instantly share code, notes, and snippets.

@dsferruzza
Last active September 30, 2024 02:32
Show Gist options
  • Save dsferruzza/e57dd3db957efe7a649325868f0024a4 to your computer and use it in GitHub Desktop.
Save dsferruzza/e57dd3db957efe7a649325868f0024a4 to your computer and use it in GitHub Desktop.
Configure a memory_limit for scheduled tasks of a Laravel app on Clever Cloud

Problems

  • some of my Laravel's scheduled tasks fail because the default memory_limit of PHP is too low
  • running php -d memory_limit=xxx artisan schedule:run does not seem to have any effect
  • there is no way to provide a custom php.ini for CLI-only on Clever Cloud

Solution

This patch adds a simple way to set memory_limit using a CLI_MEMORY_LIMIT environment variable. This new setting only applies when the Laravel app is booted from CLI (like when running php artisan ... commands) and have no effect on the memory_limit value in web context.

From 7a367f819007d76897fdb9038d89ec97a9dbd467 Mon Sep 17 00:00:00 2001
From: David Sferruzza <[email protected]>
Date: Sat, 29 Aug 2020 17:01:12 +0200
Subject: [PATCH] Add a way to change memory_limit for CLI
---
app/Providers/AppServiceProvider.php | 6 +++++-
config/app.php | 2 ++
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php
index ee8ca5b..60fa751 100755
--- a/app/Providers/AppServiceProvider.php
+++ b/app/Providers/AppServiceProvider.php
@@ -13,7 +13,11 @@ class AppServiceProvider extends ServiceProvider
*/
public function register()
{
- //
+ // If the app is run from CLI, update memory_limit
+ $cli_memory_limit = config('app.cli_memory_limit');
+ if (php_sapi_name() === 'cli' && !empty($cli_memory_limit)) {
+ ini_set('memory_limit', $cli_memory_limit);
+ }
}
/**
diff --git a/config/app.php b/config/app.php
index 8409e00..dbcef13 100755
--- a/config/app.php
+++ b/config/app.php
@@ -229,4 +229,6 @@ return [
],
+ 'cli_memory_limit' => env('CLI_MEMORY_LIMIT', null),
+
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment