Skip to content

Instantly share code, notes, and snippets.

@zahedkamal87
Forked from dsferruzza/README.md
Created June 22, 2022 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zahedkamal87/f340e229812664b71bc6f47b0484c749 to your computer and use it in GitHub Desktop.
Save zahedkamal87/f340e229812664b71bc6f47b0484c749 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 <david.sferruzza@gmail.com>
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