Artisan command aborts with no output when reaches model query with "::all()" or "::orderBy()->get()". What is happening?

huangapple go评论68阅读模式
英文:

Artisan command aborts with no output when reaches model query with "::all()" or "::orderBy()->get()". What is happening?

问题

我必须对一个包含超过5万行的数据库表进行一些更改。给定表的模型被称为 "Question"。

我创建了一个Artisan命令脚本,通过调用获取所有行,遍历整个表:

$questions = Question::orderBy('id')->get();

脚本在我的本地机器上运行得很好,测试了对2500行的运行,一切都很顺利,所以我部署到生产环境。但是,当我尝试在生产环境运行它时,当达到 Question::orderBy('id') 时,它会突然停止,没有任何输出到CLI控制台。

我尝试将其更改为 Question::all() 以获取所有行,而不排序,但结果相同,没有输出,脚本停止运行。

我尝试使用 dd() 进行调试以查看发生了什么,它确切地停在调用 all()orderBy() 的那一行。

我尝试使用 echo 调用进行调试,恰好在调用 all()orderBy() 时,命令行命令停止运行,没有显示任何错误。

脚本还加载了 echoprint_r 调用以检查进度,所以不可能没有我知道的情况下运行。

数据库配置正确,应用正在生产环境运行,每天有成千上万的用户使用。

如果我尝试使用 find() 选择单个ID并使用 dd(),显示模型工作正常,我可以访问给定ID行的所有值。

如您在图片中所见,如果第一行是 orderBy() 调用,它在一开始就停止,没有显示任何错误。如果我尝试使用给定ID的 find(),它可以无缝工作并返回所有行的信息,因此问题不在于 "Question" 模型未被调用或类似的情况。

为了检查脚本是否能够正常工作,我甚至在本地MySQL上下载了相同数据库的转储数据,运行的机器比服务器慢得多,服务器上运行的是一台Xeon服务器,行数相同。

我不明白发生了什么。与数据库的连接正常,代码相同(部署使用Git完成,因此代码相同),该命令在我的本地机器上工作,而且 没有日志输出storage/logs/,就像没有输出到命令行一样,我不知道从哪里开始。

我甚至尝试更改Artisan命令文件的权限,但结果仍然相同。

顺便说一下,我的本地机器的 .env 也使用 "APP_ENV=production" 进行测试,所以不是这个问题。

有什么想法吗?我是否忽略了在生产环境上需要做的一些事情?

英文:

I have to do some changes in a database table with more than 50k rows. The model for the given table is called "Question".
I made an artisan command script that runs through the whole table by using a call getting all rows:

$questions = Question::orderBy('id')->get();

The script ran great at my local machine, tested it running for 2500 rows and it was flawless so I went to production, but when I'm trying to run it on production, as it reaches the Question::orderBy('id') it stops abruptly with no output to CLI console.
I tried changing it to Question::all() for getting all rows like orderBy()->get() but not ordered, but same outcome, no output and the script stops.

I tried using dd() to check what is happening and it stops exactly on the line call for all() or orderBy().

I tried debugging with echo calls and it is exactly at the line where all() or orderBy()->get() is called that the command line command stops with no errors showing.

The script is also loaded with echo and print_r calls to check the progress so there is no way it ran without me knowing.

Artisan command aborts with no output when reaches model query with "::all()" or "::orderBy()->get()". What is happening?

The database is correctly configured as the application is running on production with thousand of daily users.

If I try to select a single ID using find() and dd(), shows that the model is working fine and I can access all values of the given ID row.

Artisan command aborts with no output when reaches model query with "::all()" or "::orderBy()->get()". What is happening?

Artisan command aborts with no output when reaches model query with "::all()" or "::orderBy()->get()". What is happening?

As you can see see on the images, if the first line is the orderBy() call, it stops right at the start with zero errors showing. If I try do to a find() with a given ID, it works flawlessly and returns all the rows info so the problem is not the Question model not being called or something like that.

To check if the script would work I even downloaded the dump of the same database on my local MySQL, on a much slower machine than the server running with a Xeon, it was the same amount of rows.

I don't understand what is happening. The connection with the database is working, the same code (the deploy is made using git, so same code), the command is working on my local machine, and also there is no log output to storage/logs/ just like no output to the command line, I have no idea where to begin.

I even tried changing permissions to the artisan command file, but still same output.

By the way, my local machine .env is also using "APP_ENV=production" for testing so it is not that.

Any ideas? Am I missing something that I need to do on a production env?

答案1

得分: 1

似乎是由于您尝试检索的数据量。这可能会在生产服务器中引起“内存溢出”。尝试对结果进行分块处理:https://laravel.com/docs/10.x/eloquent#chunking-results

英文:

Seems like it is due to the amount of data you're trying to retrieve. It might be causing a 'memory overflow' in the production server. Try chunking the result: https://laravel.com/docs/10.x/eloquent#chunking-results

huangapple
  • 本文由 发表于 2023年2月16日 14:26:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468567.html
  • eloquent
  • laravel
  • laravel-artisan
  • php

如何在 :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定