为什么Merit gem使用rake作业创建无限的记录和请求?

时间:2019-01-04 13:59:59

标签: ruby-on-rails merit-gem

我们已经在我们的Rails应用程序中安装了Merit。它在开发环境中完美运行。由于我们开始使用Heroku(1月1日),因此我们的应用程序运行速度非常慢(对于简单的请求,响应时间为20秒),并且发现Merit会创建大量(数千个)记录。另外,当我们使用Heroku Scheduler(每10分钟运行一次)中的rake任务刷新等级规则时,它每秒发送1万个请求。

我们试图找出问题所在,并删除了我们的rake任务。现在,我们无法更新排名规则,并没有完整的绩效记录数据库:

窗扇:214045条记录

merit_scores:214 059条记录

我们的任务(使用Heroku Scheduler每10分钟运行一次):

task cron: :environment do
  Merit::RankRules.new.check_rank_rules
end

enter image description here

请参阅问题末尾的日志摘录。

问题:

  • 如何在不调用大量资源的情况下刷新RankRules?

  • 我们如何删除所有“垃圾邮件”功绩记录,但保留会员的“良好”记录?

非常感谢

日志:

2019-01-04T08:51:44.172494+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.172422 #4] DEBUG -- :   [1m[35m (0.6ms)[0m  [1m[31mROLLBACK[0m
2019-01-04T08:51:44.174077+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.173997 #4] DEBUG -- :   [1m[36mMerit::Score Load (0.7ms)[0m  [1m[34mSELECT "merit_scores".* FROM "merit_scores" WHERE "merit_scores"."sash_id" = $1[0m  [["sash_id", 197278]]
2019-01-04T08:51:44.175968+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.175906 #4] DEBUG -- :   [1m[35m (0.6ms)[0m  [1m[34mSELECT SUM("merit_score_points"."num_points") AS sum_num_points, "merit_score_points"."score_id" AS merit_score_points_score_id FROM "merit_score_points" WHERE "merit_score_points"."score_id" = $1 GROUP BY "merit_score_points"."score_id"[0m  [["score_id", 197283]]
2019-01-04T08:51:44.177333+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.177261 #4] DEBUG -- :   [1m[36mMemberRule Load (0.7ms)[0m  [1m[34mSELECT  "member_rules".* FROM "member_rules" WHERE "member_rules"."level" = $1 ORDER BY "member_rules"."id" ASC LIMIT $2[0m  [["level", 7], ["LIMIT", 1]]
2019-01-04T08:51:44.178815+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.178750 #4] DEBUG -- :   [1m[36mMember Load (0.7ms)[0m  [1m[34mSELECT  "members".* FROM "members" WHERE "members"."id" = $1 LIMIT $2[0m  [["id", 6508], ["LIMIT", 1]]
2019-01-04T08:51:44.180016+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.179955 #4] DEBUG -- :   [1m[35m (0.5ms)[0m  [1m[35mBEGIN[0m
2019-01-04T08:51:44.181510+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.181447 #4] DEBUG -- :   [1m[36mMerit::Sash Create (0.7ms)[0m  [1m[32mINSERT INTO "sashes" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id"[0m  [["created_at", "2019-01-04 08:51:44.180189"], ["updated_at", "2019-01-04 08:51:44.180189"]]
2019-01-04T08:51:44.184424+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.184362 #4] DEBUG -- :   [1m[36mMerit::Score Create (0.9ms)[0m  [1m[32mINSERT INTO "merit_scores" ("sash_id") VALUES ($1) RETURNING "id"[0m  [["sash_id", 197279]]
2019-01-04T08:51:44.186445+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.186385 #4] DEBUG -- :   [1m[35m (1.5ms)[0m  [1m[35mCOMMIT[0m
2019-01-04T08:51:44.192733+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.192645 #4] DEBUG -- :   [1m[35m (2.5ms)[0m  [1m[35mBEGIN[0m
2019-01-04T08:51:44.198366+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.198296 #4] DEBUG -- :   [1m[36mMember Exists (0.9ms)[0m  [1m[34mSELECT  1 AS one FROM "members" WHERE "members"."email" = $1 AND "members"."id" != $2 LIMIT $3[0m  [["email", "guest_-spvohyxduzqeade6nup_1546579648_700@example.com"], ["id", 6508], ["LIMIT", 1]]
2019-01-04T08:51:44.199244+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.199174 #4] DEBUG -- :   [1m[35m (0.5ms)[0m  [1m[31mROLLBACK[0m
2019-01-04T08:51:44.200789+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.200723 #4] DEBUG -- :   [1m[36mMerit::Score Load (0.7ms)[0m  [1m[34mSELECT "merit_scores".* FROM "merit_scores" WHERE "merit_scores"."sash_id" = $1[0m  [["sash_id", 197279]]
2019-01-04T08:51:44.202687+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.202628 #4] DEBUG -- :   [1m[35m (0.8ms)[0m  [1m[34mSELECT SUM("merit_score_points"."num_points") AS sum_num_points, "merit_score_points"."score_id" AS merit_score_points_score_id FROM "merit_score_points" WHERE "merit_score_points"."score_id" = $1 GROUP BY "merit_score_points"."score_id"[0m  [["score_id", 197284]]
2019-01-04T08:51:44.203901+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.203836 #4] DEBUG -- :   [1m[36mMemberRule Load (0.6ms)[0m  [1m[34mSELECT  "member_rules".* FROM "member_rules" WHERE "member_rules"."level" = $1 ORDER BY "member_rules"."id" ASC LIMIT $2[0m  [["level", 7], ["LIMIT", 1]]
2019-01-04T08:51:44.206512+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.206449 #4] DEBUG -- :   [1m[36mMember Load (1.7ms)[0m  [1m[34mSELECT  "members".* FROM "members" WHERE "members"."id" = $1 LIMIT $2[0m  [["id", 6509], ["LIMIT", 1]]
2019-01-04T08:51:44.207739+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.207681 #4] DEBUG -- :   [1m[35m (0.5ms)[0m  [1m[35mBEGIN[0m
2019-01-04T08:51:44.209256+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.209195 #4] DEBUG -- :   [1m[36mMerit::Sash Create (0.8ms)[0m  [1m[32mINSERT INTO "sashes" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id"[0m  [["created_at", "2019-01-04 08:51:44.207919"], ["updated_at", "2019-01-04 08:51:44.207919"]]
2019-01-04T08:51:44.211871+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.211813 #4] DEBUG -- :   [1m[36mMerit::Score Create (0.7ms)[0m  [1m[32mINSERT INTO "merit_scores" ("sash_id") VALUES ($1) RETURNING "id"[0m  [["sash_id", 197280]]
2019-01-04T08:51:44.213538+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.213468 #4] DEBUG -- :   [1m[35m (1.4ms)[0m  [1m[35mCOMMIT[0m
2019-01-04T08:51:44.214271+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.214213 #4] DEBUG -- :   [1m[35m (0.5ms)[0m  [1m[35mBEGIN[0m
2019-01-04T08:51:44.217872+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.217805 #4] DEBUG -- :   [1m[36mMember Exists (0.7ms)[0m  [1m[34mSELECT  1 AS one FROM "members" WHERE "members"."email" = $1 AND "members"."id" != $2 LIMIT $3[0m  [["email", "guest_dgx3f-xjkg7haph5hehl_1546579653_701@example.com"], ["id", 6509], ["LIMIT", 1]]
2019-01-04T08:51:44.218645+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.218583 #4] DEBUG -- :   [1m[35m (0.5ms)[0m  [1m[31mROLLBACK[0m
2019-01-04T08:51:44.220025+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.219952 #4] DEBUG -- :   [1m[36mMerit::Score Load (0.7ms)[0m  [1m[34mSELECT "merit_scores".* FROM "merit_scores" WHERE "merit_scores"."sash_id" = $1[0m  [["sash_id", 197280]]
2019-01-04T08:51:44.222254+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.222167 #4] DEBUG -- :   [1m[35m (0.9ms)[0m  [1m[34mSELECT SUM("merit_score_points"."num_points") AS sum_num_points, "merit_score_points"."score_id" AS merit_score_points_score_id FROM "merit_score_points" WHERE "merit_score_points"."score_id" = $1 GROUP BY "merit_score_points"."score_id"[0m  [["score_id", 197285]]
2019-01-04T08:51:44.223549+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.223480 #4] DEBUG -- :   [1m[36mMemberRule Load (0.6ms)[0m  [1m[34mSELECT  "member_rules".* FROM "member_rules" WHERE "member_rules"."level" = $1 ORDER BY "member_rules"."id" ASC LIMIT $2[0m  [["level", 7], ["LIMIT", 1]]
2019-01-04T08:51:44.225182+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.225106 #4] DEBUG -- :   [1m[36mMember Load (0.7ms)[0m  [1m[34mSELECT  "members".* FROM "members" WHERE "members"."id" = $1 LIMIT $2[0m  [["id", 6510], ["LIMIT", 1]]
2019-01-04T08:51:44.226602+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.226533 #4] DEBUG -- :   [1m[35m (0.6ms)[0m  [1m[35mBEGIN[0m
2019-01-04T08:51:44.228011+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.227948 #4] DEBUG -- :   [1m[36mMerit::Sash Create (0.7ms)[0m  [1m[32mINSERT INTO "sashes" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id"[0m  [["created_at", "2019-01-04 08:51:44.226764"], ["updated_at", "2019-01-04 08:51:44.226764"]]
2019-01-04T08:51:44.230748+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.230675 #4] DEBUG -- :   [1m[36mMerit::Score Create (0.7ms)[0m  [1m[32mINSERT INTO "merit_scores" ("sash_id") VALUES ($1) RETURNING "id"[0m  [["sash_id", 197281]]
2019-01-04T08:51:44.232442+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.232379 #4] DEBUG -- :   [1m[35m (1.2ms)[0m  [1m[35mCOMMIT[0m
2019-01-04T08:51:44.233187+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.233125 #4] DEBUG -- :   [1m[35m (0.5ms)[0m  [1m[35mBEGIN[0m
2019-01-04T08:51:44.237218+00:00 app[scheduler.7528]: D, [2019-01-04T08:51:44.237135 #4] DEBUG -- :   [1m[36mMember Exists (0.7ms)[0m  [1m[34mSELECT  1 AS one FROM "members" WHERE "members"."email" = $1 AND "members"."id" != $2 LIMIT $3[0m  [["email", "guest_2jm9cv8vhhzyc148re4d_1546579657_702@example.com"], ["id", 6510], ["LIMIT", 1]]

0 个答案:

没有答案