在我的Rails应用程序中使用Sidekiq和Algoliasearch时,我遇到了一个奇怪的问题。
每当我启动sidekiq时,它会自动执行我使用Algoliasearch配置的所有操作:
# A post model
include AlgoliaSearch
algoliasearch if: :published?, enqueue: :trigger_sidekiq_worker do
attributes :title, :subtitle, :content, :cached_votes_score, :cached_votes_total
# the `searchableAttributes` (formerly known as attributesToIndex) setting defines the attributes
# you want to search in: here `title`, `subtitle` & `description`.
# You need to list them by order of importance. `description` is tagged as
# `unordered` to avoid taking the position of a match into account in that attribute.
searchableAttributes ['title', 'subtitle', 'unordered(content)']
# the `customRanking` setting defines the ranking criteria use to compare two matching
# records in case their text-relevance is equal. It should reflect your record popularity.
customRanking ['desc(cached_votes_score)', 'desc(cached_votes_total)']
end
private
def self.trigger_sidekiq_worker(record, remove)
::Algolia::Blog::PostsWorker.perform_async(record.id, remove)
end
日志:
$ bundle exec sidekiq
m,
`$b
.ss, $$: .,d$
`$$P,d$P' .,md$P"'
,$$$$$bmmd$$$P^'
.d$$$$$$$$$$P'
$$^' `"^$$$' ____ _ _ _ _
$: ,$$: / ___|(_) __| | ___| | _(_) __ _
`b :$$ \___ \| |/ _` |/ _ \ |/ / |/ _` |
$$: ___) | | (_| | __/ <| | (_| |
$$ |____/|_|\__,_|\___|_|\_\_|\__, |
.d$$ |_|
Signal TTIN not supported
Signal TSTP not supported
Signal USR1 not supported
Signal USR2 not supported
2017-04-25T18:50:30.134Z 5596 TID-c0loo INFO: Running in ruby 2.3.3p222 (2016-11-21 revision 56859) [i386-mingw32]
2017-04-25T18:50:30.134Z 5596 TID-c0loo INFO: See LICENSE and the LGPL-3.0 for licensing details.
2017-04-25T18:50:30.135Z 5596 TID-c0loo INFO: Upgrade to Sidekiq Pro for more features and support: http://sidekiq.org
2017-04-25T18:50:30.136Z 5596 TID-c0loo INFO: Booting Sidekiq 5.0.0 with redis options {:url=>nil}
2017-04-25T18:50:30.138Z 5596 TID-c0loo INFO: Starting processing, hit Ctrl-C to stop
2017-04-25T18:50:30.207Z 5596 TID-1nla70 Algolia::Blog::PostsWorker JID-262b2f8613a197637d05caf3 INFO: start
2017-04-25T18:50:30.539Z 5596 TID-1nla70 Algolia::Blog::PostsWorker JID-262b2f8613a197637d05caf3 INFO: fail: 0.333 sec
2017-04-25T18:50:30.540Z 5596 TID-1nla70 WARN: {"context":"Job raised exception","job":{"class":"Algolia::Blog::PostsWorker","args":[1,false],"retry":true,"queue":"default","jid":"262b2f8613a197637d05caf3","created_at":1493146072.768356,"enqueued_at":1493146072.768356,"error_message":"Couldn't find Blog::Post with 'id'=1","error_class":"ActiveRecord::RecordNotFound","failed_at":1493146230.538659,"retry_count":0},"jobstr":"{\"class\":\"Algolia::Blog::PostsWorker\",\"args\":[1,false],\"retry\":true,\"queue\":\"default\",\"jid\":\"262b2f8613a197637d05caf3\",\"created_at\":1493146072.768356,\"enqueued_at\":1493146072.768356}"}
2017-04-25T18:50:30.541Z 5596 TID-1nla70 WARN: ActiveRecord::RecordNotFound: Couldn't find Blog::Post with 'id'=1
我不知道这种行为是否有意,因为algolia似乎只是尝试索引id为1的记录。据我了解,它应该只在创建,删除或更新记录后进行索引。
工人失败后重新开始,所以我积累了大量的内存。在某些情况下,即使我的所有类和命名空间都被正确命名,我也会获得LoadError
。
我不知道问题是与Algolia还是Sidekiq有关。
答案 0 :(得分:3)
我的猜测是,它在队列中有一个工作来索引一个不再存在的对象。
队列应该正确处理它,我不确定它为什么没有。
您可以尝试清除Sidekiq队列吗? 要清除所有队列,请运行:
Sidekiq::Queue.all.each &:clear
在控制台中,然后再试一次。