Skip to content

Commit 0ab84d9

Browse files
Update README.md
1 parent df00613 commit 0ab84d9

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,101 @@ class PreventRequestsDuringMaintenance extends Middleware
7575

7676
```
7777

78+
(4) Optional: set application `RUNNING_IN_CONSOLE` (highly recommended)
79+
80+
Some Laravel service providers only register their commands if the application is being accessed through the command line (Artisan). Because we are calling Laravel scheduler from a HTTP call, that means some commands may never register, such as the Laravel Scout command:
81+
82+
```php
83+
/**
84+
* Bootstrap any application services.
85+
*
86+
* @return void
87+
*/
88+
public function boot()
89+
{
90+
if ($this->app->runningInConsole()) {
91+
$this->commands([
92+
FlushCommand::class,
93+
ImportCommand::class,
94+
IndexCommand::class,
95+
DeleteIndexCommand::class,
96+
]);
97+
98+
$this->publishes([
99+
__DIR__.'/../config/scout.php' => $this->app['path.config'].DIRECTORY_SEPARATOR.'scout.php',
100+
]);
101+
}
102+
}
103+
```
104+
105+
To circumvent this, please add the following to `public/index.php`
106+
107+
```diff
108+
/*
109+
|--------------------------------------------------------------------------
110+
| Check If Application Is Under Maintenance
111+
|--------------------------------------------------------------------------
112+
|
113+
| If the application is maintenance / demo mode via the "down" command we
114+
| will require this file so that any prerendered template can be shown
115+
| instead of starting the framework, which could cause an exception.
116+
|
117+
*/
118+
119+
if (file_exists(__DIR__.'/../storage/framework/maintenance.php')) {
120+
require __DIR__.'/../storage/framework/maintenance.php';
121+
}
122+
+
123+
+ /*
124+
+ |--------------------------------------------------------------------------
125+
+ | Manually Set Running In Console for Google Cloud Scheduler
126+
+ |--------------------------------------------------------------------------
127+
+ |
128+
+ | Some service providers only register their commands if the application
129+
+ | is running from the console. Since we are calling Cloud Scheduler
130+
+ | from the browser we must manually trick the application into
131+
+ | thinking that it is being run from the command line.
132+
+ |
133+
+ */
134+
+
135+
+ if (($_SERVER['REQUEST_URI'] ?? '') === '/cloud-scheduler-job') {
136+
+ $_ENV['APP_RUNNING_IN_CONSOLE'] = true;
137+
+ }
138+
+
139+
/*
140+
|--------------------------------------------------------------------------
141+
| Register The Auto Loader
142+
|--------------------------------------------------------------------------
143+
|
144+
| Composer provides a convenient, automatically generated class loader for
145+
| this application. We just need to utilize it! We'll simply require it
146+
| into the script here so we don't need to manually load our classes.
147+
|
148+
*/
149+
150+
require __DIR__.'/../vendor/autoload.php';
151+
```
152+
153+
Copy the code here:
154+
155+
```php
156+
/*
157+
|--------------------------------------------------------------------------
158+
| Manually Set Running In Console for Google Cloud Scheduler
159+
|--------------------------------------------------------------------------
160+
|
161+
| Some service providers only register their commands if the application
162+
| is running from the console. Since we are calling Cloud Scheduler
163+
| from the browser we must manually trick the application into
164+
| thinking that it is being run from the command line.
165+
|
166+
*/
167+
168+
if (($_SERVER['REQUEST_URI'] ?? '') === '/cloud-scheduler-job') {
169+
$_ENV['APP_RUNNING_IN_CONSOLE'] = true;
170+
}
171+
```
172+
78173
# Cloud Scheduler Example
79174

80175
Here is an example job that will run `php artisan schedule:run` every minute.

0 commit comments

Comments
 (0)