延迟作业反序列化错误,无法加载:分配器未定义为Proc(Delayed Job DeserializationError, failed to load: allocator undefined for Proc)
对于
delayed_job_active_record
gem的第4版,我想将请求延迟到外部API。 我的工作被添加到数据库和rake jobs:work
运行它并将其从数据库中删除。 但是,我的messages_controller.rb
的实际延迟代码永远不会被执行。如果我尝试在rails控制台中
Delayed::Job.last.invoke_job
,我收到以下错误:Delayed::DeserializationError: Job failed to load: allocator undefined for Proc. Handler: "--- !ruby/object:Delayed::PerformableMethod\nobject: !ruby/object:MyApp::Zendesk\n client: !ruby/object:ZendeskAPI::Client\n config: !ruby/object:ZendeskAPI::Configuration\n client_options: {}\n cache: !ruby/object:ZendeskAPI::LRUCache\n size: 1000\n store: {}\n lru: []\n url: https://redacted.zendesk.com/api/v2\n username: redacted\n password: redacted\n retry: true\n logger: !ruby/object:Logger\n progname: \n level: 0\n default_formatter: !ruby/object:Logger::Formatter\n datetime_format: \n formatter: \n logdev: !ruby/object:Logger::LogDevice\n shift_size: \n shift_age: \n filename: \n dev: !ruby/object:IO {}\n mutex: !ruby/object:Logger::LogDevice::LogDeviceMutex\n mon_owner: \n mon_count: 0\n mon_mutex: !ruby/object:Mutex {}\n callbacks:\n - !ruby/object:Proc {}\n resource_cache: {}\nmethod_name: :create_support_ticket\nargs:\n- !ruby/hash:ActionController::Parameters\n name: redacted\n email: redacted\n reason: General\n message: test\n"
尝试运行的代码是
create_support_ticket
方法:# messages_controller.rb require 'zendesk' class MessagesController < ApplicationController layout "application" # /suport/contact-us def contact_us zendesk = MyApp::Zendesk.new zendesk.delay.create_support_ticket(params[:message]) # render page respond_to do |format| flash[:notice] = "Email sent successfully!" if @sent format.html { render "pages/support/contact-us" } end end end # zendesk.rb require 'zendesk_api' module MyApp class Zendesk attr_accessor :client def initialize(*args) @client = create_client end # contact-us ticket methods def create_support_ticket(params={}) unless params.blank? || @client.blank? # get or create user_id for submitter params[:requester_id] = check_user_exists(params) begin ticket = @client.tickets.create( subject: "Support Ticket", comment: { value: params[:message] }, submitter_id: params[:requester_id], requester_id: params[:requester_id], assignee_id: 201578811, status: "new", fields: [ {id: 20887016, value: "Support"}, {id: 20966436, value: "New"}]) return ticket rescue => e Airbrake.notify e end else return false end end end end
With version 4 of
delayed_job_active_record
gem, I want to delay requests to an external API. My job is added to database, andrake jobs:work
runs it and removes it from the database. However, the actual delayed code from mymessages_controller.rb
is never executed.If I try to
Delayed::Job.last.invoke_job
in the rails console, I receive the following error:Delayed::DeserializationError: Job failed to load: allocator undefined for Proc. Handler: "--- !ruby/object:Delayed::PerformableMethod\nobject: !ruby/object:MyApp::Zendesk\n client: !ruby/object:ZendeskAPI::Client\n config: !ruby/object:ZendeskAPI::Configuration\n client_options: {}\n cache: !ruby/object:ZendeskAPI::LRUCache\n size: 1000\n store: {}\n lru: []\n url: https://redacted.zendesk.com/api/v2\n username: redacted\n password: redacted\n retry: true\n logger: !ruby/object:Logger\n progname: \n level: 0\n default_formatter: !ruby/object:Logger::Formatter\n datetime_format: \n formatter: \n logdev: !ruby/object:Logger::LogDevice\n shift_size: \n shift_age: \n filename: \n dev: !ruby/object:IO {}\n mutex: !ruby/object:Logger::LogDevice::LogDeviceMutex\n mon_owner: \n mon_count: 0\n mon_mutex: !ruby/object:Mutex {}\n callbacks:\n - !ruby/object:Proc {}\n resource_cache: {}\nmethod_name: :create_support_ticket\nargs:\n- !ruby/hash:ActionController::Parameters\n name: redacted\n email: redacted\n reason: General\n message: test\n"
The code trying to be run is the
create_support_ticket
method:# messages_controller.rb require 'zendesk' class MessagesController < ApplicationController layout "application" # /suport/contact-us def contact_us zendesk = MyApp::Zendesk.new zendesk.delay.create_support_ticket(params[:message]) # render page respond_to do |format| flash[:notice] = "Email sent successfully!" if @sent format.html { render "pages/support/contact-us" } end end end # zendesk.rb require 'zendesk_api' module MyApp class Zendesk attr_accessor :client def initialize(*args) @client = create_client end # contact-us ticket methods def create_support_ticket(params={}) unless params.blank? || @client.blank? # get or create user_id for submitter params[:requester_id] = check_user_exists(params) begin ticket = @client.tickets.create( subject: "Support Ticket", comment: { value: params[:message] }, submitter_id: params[:requester_id], requester_id: params[:requester_id], assignee_id: 201578811, status: "new", fields: [ {id: 20887016, value: "Support"}, {id: 20966436, value: "New"}]) return ticket rescue => e Airbrake.notify e end else return false end end end end
原文:https://stackoverflow.com/questions/20733076
最满意答案
如果你可以用“1st”,“2nd”,“3rd”等,下面是一些简单的代码,可以正确处理任何整数:
public static String ordinal(int i) { String[] sufixes = new String[] { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" }; switch (i % 100) { case 11: case 12: case 13: return i + "th"; default: return i + sufixes[i % 10]; } }
以下是边缘情况的一些测试:
public static void main(String[] args) { int[] edgeCases = { 0, 1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 100, 101, 102, 103, 104, 111, 112, 113, 114 }; for (int edgeCase : edgeCases) { System.out.println(ordinal(edgeCase)); } }
输出:
0th 1st 2nd 3rd 4th 5th 10th 11th 12th 13th 14th 20th 21st 22nd 23rd 24th 100th 101st 102nd 103rd 104th 111th 112th 113th 114th
If you're OK with "1st", "2nd", "3rd" etc, here's some simple code that will correctly handle any integer:
public static String ordinal(int i) { String[] sufixes = new String[] { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" }; switch (i % 100) { case 11: case 12: case 13: return i + "th"; default: return i + sufixes[i % 10]; } }
Here's some tests for edge cases:
public static void main(String[] args) { int[] edgeCases = { 0, 1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 100, 101, 102, 103, 104, 111, 112, 113, 114 }; for (int edgeCase : edgeCases) { System.out.println(ordinal(edgeCase)); } }
Output:
0th 1st 2nd 3rd 4th 5th 10th 11th 12th 13th 14th 20th 21st 22nd 23rd 24th 100th 101st 102nd 103rd 104th 111th 112th 113th 114th
相关问答
更多-
如果你可以用“1st”,“2nd”,“3rd”等,下面是一些简单的代码,可以正确处理任何整数: public static String ordinal(int i) { String[] sufixes = new String[] { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" }; switch (i % 100) { case 11: case 12: case 13: ...
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
Java或Android都支持创建序号字符串。 Android不支持创建多个字符串资源,但不支持序号。 Java nor Android have support for creating ordinal strings. Android does have support for creating plural string resources, but not ordinals.
-
在Java中将Integer转换为int的更好方法(可能为null)?(A better way to convert Integer (may be null) to int in Java?)[2023-07-05]
避免异常总是更好。 int i = integer != null ? integer.intValue() : -1; Avoiding an exception is always better. int i = integer != null ? integer.intValue() : -1; -
public static int convert(int n) { return Integer.valueOf(String.valueOf(n), 16); } public static void main(String[] args) { System.out.println(convert(20)); // 32 System.out.println(convert(54)); // 84 } 也就是说,将原始数字视为十六进制,然后转换为十进制。 public static ...
-
看看ByteBuffer类。 ByteBuffer b = ByteBuffer.allocate(4); //b.order(ByteOrder.BIG_ENDIAN); // optional, the initial order of a byte buffer is always BIG_ENDIAN. b.putInt(0xAABBCCDD); byte[] result = b.array(); 设置字节顺序确保result[0] == 0xAA , result[1] == 0xBB , ...
-
要将序数转换为其枚举代数,您可能需要这样做: ReportTypeEnum value = ReportTypeEnum.values()[ordinal]; 请注意数组边界。 请注意,每次调用values() ,都会返回一个新克隆的数组,这样可能会以负面的方式影响性能。 如果要经常调用,可能需要缓存数组。 关于如何缓存values()代码示例 。 这个答案被编辑成包括意见内的反馈 To convert an ordinal into its enum represantation you might w ...
-
有多种方式: String.valueOf(number) (我的偏好) "" + number (我不知道编译器如何处理它,也许它像上面那样有效) Integer.toString(number) There are multiple ways: String.valueOf(number) (my preference) "" + number (I don't know how the compiler handles it, perhaps it is as efficient as the abo ...
-
将日期转换为序数python?(Convert date to ordinal python?)[2022-10-06]
你需要在日期字符串上使用strptime ,指定格式,然后你可以调用date对象的toordinal方法: >>> from datetime import datetime as dt >>> d = dt.strptime('2010-03-01', '%Y-%m-%d').date() >>> d datetime.date(2010, 3, 1) >>> d.toordinal() 733832 在这种情况下对date方法的调用是多余的,并且仅保留用于使对象与date对象而不是date对象一致。 ... -
从Float转换为整数(Convert from Float to integer)[2022-04-21]
double y = 9.37923929732232; int a = (int)y; double y = 9.37923929732232; int a = (int)y;