craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.25k stars 630 forks source link

[3.x]: Console methods called in web #11829

Closed boboldehampsink closed 2 years ago

boboldehampsink commented 2 years ago

What happened?

Description

Undefined constant "STDOUT"

See this Sentry log: https://sentry.io/share/issue/e795939f38dd4c4e858becf37dc14158/

Craft CMS version

Craft Pro 3.7.52

PHP version

8.1.9

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

-

brandonkelly commented 2 years ago

Thanks for reporting that! Just released Craft 3.7.53.1 and 4.2.3 with a fix for that.

dgrigg commented 5 months ago

Seeing this error in Craft 4.5.14 Console::startProgress (u causes the Undefined constant "STDOUT" error. Only happens on our production server. The code is running inside a Queued Job. This is to output progress to the CLI, separate from the BaseJob setProgress.

The odd part is that if I run ./craft crm/registrations && ./craft queue/run it runs fine in the terminal, but if I only do ./craft crm/registrations and let the crontab for the QueueManager pick it up I see an error in Craft CP

brandonkelly commented 4 months ago

@dgrigg Can you search for the error in storage/logs/ and post the stack trace?

dgrigg commented 4 months ago

It only happens if the Queue runs from the CP panel. Executing ./craft queue/run from the CLI runs fine. I ended up creating a Console Trait that wrapped the functions with this if (defined('STDOUT') and added that to the Jobs to get around the issue. Any of the output functions in yii\helpers\Console throw the error.

queue-2024-05-14.log

brandonkelly commented 4 months ago

Looks like it’s getting called from a module file:

#2 /var/www/thermacell/modules/crm/jobs/ProductDataJob.php(122): yii\helpers\BaseConsole::startProgress(0, 366, 'PRODUCTS: 366 ')

So you’ll need to adjust that code yourself.

Queue jobs should not assume that they will be run from a CLI request.

dgrigg commented 4 months ago

Yes, I fixed the code. The issue is that it's throwing that error in the first place. I guess the Queue process does not define STDOUT anywhere?

brandonkelly commented 4 months ago

Your code is calling a method on the Console helper, which should only ever be invoked on a CLI request. It’s expected that errors will occur if you do that from a web request. Nothing in the queue or built-in queue jobs is built to assume that it will be run from a CLI request.

dgrigg commented 4 months ago

Got it. Makes sense. Thanks.