我正在尝试使用rufus-scheduler gem和salesforcebulk gem定期将数据导入rails。
我成功地将数据从一个salesforce自定义对象导入到我的数据库中,但是当我尝试导入其他对象时遇到了麻烦:
scheduler caught exception:
Encoding::UndefinedConversionError: "\xC3" from ASCII-8BIT to UTF-8: INSERT INTO "channels" ("created_at", "name", "updated_at") VALUES (?, ?, ?)
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:208:in `encode'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:208:in `type_cast'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:259:in `block (2 levels) in exec_query'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:258:in `map'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:258:in `block in exec_query'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:242:in `exec_query'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:63:in `exec_insert'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:90:in `insert'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/relation.rb:66:in `insert'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/persistence.rb:367:in `create'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/timestamp.rb:58:in `create'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/callbacks.rb:268:in `block in create'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:403:in `_run__3666243914465906250__create__2439689705720496844__callbacks'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_create_callbacks'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/callbacks.rb:268:in `create'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/persistence.rb:348:in `create_or_update'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/callbacks.rb:264:in `block in create_or_update'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:414:in `_run__3666243914465906250__save__2439689705720496844__callbacks'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/callbacks.rb:264:in `create_or_update'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/persistence.rb:84:in `save'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/validations.rb:50:in `save'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/attribute_methods/dirty.rb:22:in `save'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/transactions.rb:259:in `block (2 levels) in save'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/transactions.rb:208:in `transaction'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/transactions.rb:259:in `block in save'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/transactions.rb:270:in `rollback_active_record_state!'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/transactions.rb:258:in `save'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:296:in `find_or_instantiator_by_attributes'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/activerecord-3.2.13/lib/active_record/dynamic_matchers.rb:52:in `method_missing'
/Users/arturodiaz/Drive/Code/partnerportal2/agripa/config/initializers/task_scheduler.rb:121:in `block (2 levels) in <top (required)>'
/Users/arturodiaz/Drive/Code/partnerportal2/agripa/config/initializers/task_scheduler.rb:120:in `each'
/Users/arturodiaz/Drive/Code/partnerportal2/agripa/config/initializers/task_scheduler.rb:120:in `block in <top (required)>'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/rufus-scheduler-2.0.19/lib/rufus/sc/jobs.rb:231:in `call'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/rufus-scheduler-2.0.19/lib/rufus/sc/jobs.rb:231:in `trigger_block'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/rufus-scheduler-2.0.19/lib/rufus/sc/jobs.rb:191:in `block in trigger'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/rufus-scheduler-2.0.19/lib/rufus/sc/scheduler.rb:415:in `call'
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/rufus-scheduler-2.0.19/lib/rufus/sc/scheduler.rb:415:in `block in trigger_job'
================================================================================
以下是成功检索和导入的对象的代码:
scheduler.at("2013-07-03 11:17:15 -0700 ") do
client = SalesforceBulk::Client.new(username: 'XXXXXXXXX', password: 'XXXXXXXXX')
client.authenticate
job = client.add_job(:query, :Paquete__c)
batch = client.add_batch(job.id, "SELECT Id, Name, Capacidad__c, Organizacion__c FROM Paquete__c")
job = client.close_job(job.id)
sleep 5.0
batch = client.batch_info(job.id, batch.id)
results = client.batch_result(job.id, batch.id)
results.each do |d|
Paquete.find_or_create_by_serie_and_id_and_version(id: d[:Id],
serie: d[:Name],
account_id: d[:Organizacion__c],
capacidad: d[:Capacidad__c])
end
puts "Done... Paquetes"
end
以下是非工作代码:
scheduler.at("2013-07-03 11:40:00 -0700 ") do
client = SalesforceBulk::Client.new(username: 'XXXXXXXXXXXX', password: 'XXXXXXXXXXX')
client.authenticate
job = client.add_job(:query, :Account)
batch = client.add_batch(job.id, "SELECT Id, Name, Type FROM Account")
job = client.close_job(job.id)
sleep 5.0
batch = client.batch_info(job.id, batch.id)
results = client.batch_result(job.id, batch.id)
results.each do |d|
Account.find_or_create_by_id_and_name(id: d[:Id], name: d[:Name],type: d[:Type])
end
puts "Done... Accounts"
end
我已经尝试在salesforcebulk gem的batch_result方法中更改CSV解析器中的编码,但没有成功。
我觉得很奇怪,一段代码正在运行而另一段却没有。也许这是关于特定桌子的事情。
答案 0 :(得分:0)
您应该关注错误:
Encoding::UndefinedConversionError: "\xC3" from ASCII-8BIT to UTF-8:
INSERT INTO "channels" ("created_at", "name", "updated_at") VALUES (?, ?, ?)
/Users/arturodiaz/.rvm/gems/ruby-2.0.0-p195@agripa/gems/
activerecord-3.2.13/lib/active_record/connection_adapters/
sqlite_adapter.rb:208:in `encode'
在你的名字列中似乎有一个~n(如在Jalapeo中),sqlite适配器不喜欢它。可能您的Salesforce批量gem需要某种本地化设置。
这与rufus-scheduler无关。