I am trying to get the output of the command scheduled:summary function of laravel 5 and echo it in the browser, what i am currently doing is the following
public function scheduledCommands()
{
$output = new BufferedOutput;
Artisan::call('scheduled:summary',array(),$output);
return $output->fetch();
}
When i call this function i get this following error :
Use of undefined constant STDOUT - assumed 'STDOUT'
This method works for any other command just fine but this one, also when i execute it from CLI everything works fine and i get a table like this with my scheduled commands:
I don't understand what is different with this command, any further on why i get this error will be appreciated.
Related
I need to run a node script inside my Laravel application.
I've tried using the Symfony Process, but it kill the process after the PHP execution and I need to keep the node process running, so my next step was try to use the shell_exec and exec functions. It's worked nice in the tinker and in Tinkerwell, but when I try to use inside the Laravel class it gives the following error 500:
production.ERROR: Call to undefined function shell_exec() {"exception":"[object] (Error(code: 0): Call to undefined function shell_exec() at ...
As it is a VPS I set my php.ini to disable_functions= but it still not working inside Laravel, only if I use it outside the framework.
Thanks in advance and sorry for my bad English.
Unfortunately this can't be done with exec() (nor system() or shell_exec()).
But if you install the pcntl extension, define something like:
<?php
function detached_exec($cmd) {
$pid = pcntl_fork();
if ($pid == 0) {
// Childe process main-loop goes here.
posix_setsid();
exec($cmd);
exit(0);
} else if ($pid == -1) {
// Process fork failed.
return FALSE;
}
return $pid;
}
And use like:
// Edit this:
$cmd = 'node --version';
$pid = detached_exec($cmd);
if($pid === FALSE) {
echo 'exec failed';
}
I ended giving up. I've developed an workaround.
Instead calling the node file by PHP, I made a node script whit express who receives a POST request and start the code.
Now I can put my node code in another server and it will keep running with no problems and make my system scalable.
so I got this code:
public static function postAllTasks()
{
$allTasks = Tasks::getTasksForToday();
foreach ($allTasks as $task) {
$delay = DB::table('jobs')->count()*20;
PostTasks::dispatch((string)$task->task)->delay($delay);
}
}
It is working fine on my computer just fine, but as soon as it is on the server it returns the error:
Error: Call to undefined function App\Http\Controllers\Traits\curl_init() in /var/www/html/app/Http/Controllers/Traits/TaskTrait.php:89
Stack trace:
#0 /var/www/html/app/Http/Controllers/Traits/TaskTrait.php(224)...
The problem is, that the PHP curl extension is active and visible + when I run exactly the same PHP artisan command, which doesn't use the dispatch/worker but just execute the Job (so it is using curl anyway) right away, everything works.
When I use:
QUEUE_CONNECTION=sync
instead of
QUEUE_CONNECTION=database
It works too, but I need it to use the database in order to calculate the delay between each job.
Laravel Framework 8.55.0
PHP 7.4.3
I made loads of changes within my code and unfortunately kind of lost track what were the last one.. but I guess one of them created this weird problem.
When I'm trying to run php artisan serve, I get this error -
Parse error: parse error in /Users/Guest/admanager/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php on line 500
And the function that returns the error in helpers.php looks like this -
function factory()
{
$factory = app(EloquentFactory::class);
$arguments = func_get_args();
if (isset($arguments[1]) && is_string($arguments[1])) {
return $factory->of($arguments[0], $arguments[1])->times($arguments[2] ? null); // specifically, this is line 500
} elseif (isset($arguments[1])) {
return $factory->of($arguments[0])->times($arguments[1]);
}
return $factory->of($arguments[0]);
}
Please change as per below code,
$arguments[2] ? null TO $arguments[2] ?? null
The problem was that I just uninstalled xampp and it downgraded my locally running php version, hence the artisan serve didn't know how to handle that.
I've updated my crontab using crontab -e to run the Laravel task scheduler every minute. It wasn't working so I set it to add its output to a log file, like so:
* * * * * php /path/to/project/artisan schedule:run >> cron_output.log
Now the log contains the following message:
Parse error: parse error in /path/to/project/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php on line 233
This is the method it points to (I've commented next to line 233):
function cache()
{
$arguments = func_get_args();
if (empty($arguments)) {
return app('cache');
}
if (is_string($arguments[0])) {
return app('cache')->get($arguments[0], $arguments[1] ?? null); //line 233
}
if (! is_array($arguments[0])) {
throw new Exception(
'When setting a value in the cache, you must pass an array of key / value pairs.'
);
}
if (! isset($arguments[1])) {
throw new Exception(
'You must specify an expiration time when setting a value in the cache.'
);
}
return app('cache')->put(key($arguments[0]), reset($arguments[0]), $arguments[1]);
}
I don't understand this issue, as Laravel runs normally otherwise. PHP version is 7.2 (based on php -v and also phpinfo()) so that shouldn't be a problem. Is it possible that somehow the crontab is using an older version of PHP? If so how do I fix this? Any other ideas?
I'm using Laravel 5.1 to create a console based application. During development I would like to display the exception trace when an error occurs. However, even if I use -v -vv or -vvv option in php artisan, I don't get an exception trace for my custom commands. I set APP_DEBUG=true in my .env, still no exception trace.
Output of php artisan some:unknowncommand is:
[InvalidArgumentException]
There are no commands defined in the "some" namespace.
Output of php artisan -v some:unknowncommand is:
[InvalidArgumentException]
There are no commands defined in the "some" namespace.
Exception trace:
() at /Users/dirkpostma/Dropbox/Domains/dpepp/vendor/symfony/console/Application.php:501
Symfony\Component\Console\Application->findNamespace() at /Users/dirkpostma/Dropbox/Domains/dpepp/vendor/symfony/console/Application.php:535
Symfony\Component\Console\Application->find() at /Users/dirkpostma/Dropbox/Domains/dpepp/vendor/symfony/console/Application.php:192
Symfony\Component\Console\Application->doRun() at /Users/dirkpostma/Dropbox/Domains/dpepp/vendor/symfony/console/Application.php:126
...
Now, I created a very simple console command called dp:test, with following handle function:
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
generate error here
}
Output of php artisan dp:test is:
[Symfony\Component\Debug\Exception\FatalErrorException]
syntax error, unexpected 'error' (T_STRING)
Output of php artisan -v dp:test is the same.
Output of php artisan -vvv dp:test is the same.
The log file DOES show the exception trace, so somehow it should be possible to display it in cli. I don't even see the filename and linenumer where the error occurs... How can I take care of this?
Thanks in advance!
EDIT:
Is digged a bit further. In case I use this in my Command:
public function handle()
{
throw new \Exception("test exception");
}
and I issue the command php artisan -v dp:test, the error trace IS printed. The trace is only not printed when the exception is thrown due to a PHP error. In Illuminate/Foundation/Bootstrap/HandleExceptions.php method bootstrap PHP errors are converted to Exceptions. When this occurs, an exception is thrown, but the -v is somehow ignored when printing. This is very inconvenient because it makes debugging CLI apps hard.
I think the solution can be found in vendor/symfony/console/Application.php, method renderException.
I'm going to dig further later, unless someone else can point the solution faster than me :-)
I found reason why -v is ignored:
in Illuminate/Foundation/Bootstrap/HandleExceptions.php, the renderForConsole method instantiates a ConsoleOutput object, with default settings, not taking into account the verbosity settings the user asked for:
protected function renderForConsole($e)
{
$this->getExceptionHandler()->renderForConsole(new ConsoleOutput, $e);
}
For this reason, whatever -v -vv or -vvv is set, the $output->getVerbosity() in vendor/symfony/console/Application.php is always lower than OutputInterface::VERBOSITY_VERBOSE, for that reason, the stack trace is not printed.
I probably start an issue on github for this, because I think it's much more convenient if errors are shown in CLI if user sets -v.
You can set the verbosity to whatever level you'd like by adding the following use statement:
use Symfony\Component\Console\Output\OutputInterface;
And then adding this to the top of your handle function:
$this->output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);
See the documentation for symfony console here http://symfony.com/doc/current/console/verbosity.html
for more information.
If you look at Illuminate\Console\Command class you will see the definition of the function that writes the error string to the console:
/**
* Write a string as error output.
*
* #param string $string
* #return void
*/
public function error($string)
{
$this->output->writeln("<error>$string</error>");
}
Now that you see it only writes the error string, you can play just a little bit with it to achieve what you want. You can get the function call back trace and output as many lines and files backwards as you wish. Just create class that extends command, override the function and make all of your commands to inherit that command class:
/**
* Write a string as error output.
*
* #param string $string
* #return void
*/
public function error($string)
{
$this->output->writeln("<error>$string</error>");
$trace = debug_backtrace();
foreach ($trace as $t)
{
$this->output->writeln("Trace : " . $t['file'] . " on line " . $t['line'] . " function " . $t['function']);
}
}