将HTTParty与delayed_job结合使用

发布于 2024-12-20 21:04:50 字数 670 浏览 2 评论 0原文

我正在尝试使用 delayed_jobHTTParty 执行此操作:

HTTParty.delay.get('http://example.com')

它成功保存到 DJ 表,如下所示:

--- !ruby/object:Delayed::PerformableMethod
object: !ruby/object:Module {}
method_name: :get
args:
- http://example.com

问题是它没有提及 HTTParty 在该处理程序中,因此当我尝试运行作业时,我收到此错误:

undefined method `get' for #<Module:0x007fff1095b9a8>

我正在使用 delayed_job (3.0.0.pre)rails 3.0.6 应用中的 delayed_job_active_record (0.2.1)httparty (0.7.7)

那么我怎样才能让delayed_job和HTTParty很好地协同工作呢?

I'm trying to do this with delayed_job and HTTParty:

HTTParty.delay.get('http://example.com')

It successfully saves to the DJ table as this:

--- !ruby/object:Delayed::PerformableMethod
object: !ruby/object:Module {}
method_name: :get
args:
- http://example.com

The problem is that it doesn't have any mention of HTTParty in that handler, so when I try to run the jobs, I get this error:

undefined method `get' for #<Module:0x007fff1095b9a8>

I'm using delayed_job (3.0.0.pre), delayed_job_active_record (0.2.1) and httparty (0.7.7) in a rails 3.0.6 app.

So how can I make delayed_job and HTTParty play nice together?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小女人ら 2024-12-27 21:04:50

将代码包装在实例方法中。

示例:

# User.rb model
include HTTParty

# Get a URL
def start_the_party(url)
  HTTParty.get(url)
end
handle_asynchronously :start_the_party

然后你只需在其他地方调用它,例如:

some_user.start_the_party

或者如果你需要访问成功的 get 请求内容

some_results = some_user.start_the_party

编辑

如果你打算将其设为类方法,那么代码将如下所示:

示例:

# User.rb model
include HTTParty

# Get a URL
class << self
  def start_the_party(url)
    HTTParty.get(url)
  end
  handle_asynchronously :start_the_party
end

然后您只需在其他地方调用它,例如:

User.start_the_party

或者如果您需要访问成功的 get 请求内容,

some_results = User.start_the_party

区别在于 User.some_method_nameUser 模型上调用,而some_user.some_method_nameUser 模型的实例(即当前用户对象)上调用。

Wrap the code in an instance method.

Example:

# User.rb model
include HTTParty

# Get a URL
def start_the_party(url)
  HTTParty.get(url)
end
handle_asynchronously :start_the_party

Then you just call it somewhere else like:

some_user.start_the_party

or if you need access to the successful get request contents

some_results = some_user.start_the_party

Edit

If you're going to make it a class method, then the code will be like this:

Example:

# User.rb model
include HTTParty

# Get a URL
class << self
  def start_the_party(url)
    HTTParty.get(url)
  end
  handle_asynchronously :start_the_party
end

Then you just call it somewhere else like:

User.start_the_party

or if you need access to the successful get request contents

some_results = User.start_the_party

The difference is that User.some_method_name is called on the User model whereas some_user.some_method_name is called on an instance of the User model (i.e. the current user object).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文