7 Replies Latest reply on Mar 17, 2014 8:31 AM by 25c49980-cb2e-4113-a5a7-2f0078c85330

    Anyone know how to use rollback_objects with the on_sync_update_error method?

    25c49980-cb2e-4113-a5a7-2f0078c85330

      I'm struggling with how to handle updates that we want to rollback.  The documentation (Rhomobile | SyncEngine API) refers to rollback_objects (must be specified for :rollback action. Contains the attributes for the objects before a failed update and send by the server.) but doesn't give any more than that.

       

      Has anyone done this?

       

      Does anyone have an example they could share?

       

      Many thanks

       

      John

        • Re: Anyone know how to use rollback_objects with the on_sync_update_error method?
          Kutir Mobility

          Hi John,

           

          I will try locally and get back to you.



          Visnupriya R

          Kutir Mobility

            • Re: Anyone know how to use rollback_objects with the on_sync_update_error method?
              Kutir Mobility

              John,

              Try as shown below in the sync notification callback method

              if @params["status"] == "error"
                  SyncEngine.on_sync_update_error( @params['source_name'],@params['server_errors']['update-error'], :rollback,                                                          @params['server_errors']['update-rollback'] )
              end
              

               

              Visnupriya R

              Kutir Mobility

              1 of 1 people found this helpful
                • Re: Re: Anyone know how to use rollback_objects with the on_sync_update_error method?
                  25c49980-cb2e-4113-a5a7-2f0078c85330

                  Thanks very much.  Think this is what I wanted to know.

                   

                  For those wondering, you need to use the scrollbar to see the important part of the response above!  @params['server_errors']['update-rollback'] )

                   

                  Having got that implemented, I now seem to get an error in the on_sync_update_error method when I try to action them:

                   

                  I 03/14/2014 16:09:27:002 000024b4                  APP| Params: {"__rho_object"=>{"__rho_inline"=>"0"}, "rho_callback"=>"1"}
                  I 03/14/2014 16:09:27:114 000024b4                  APP| ===============================================================
                  I 03/14/2014 16:09:27:210 000024b4                  APP| Update error {"1"=>{"message"=>"Rhoconnect::Model::Exception", "attributes"=>{"sku"=>"test1"}}, "2"=>{"attributes"=>{"sku"=>"test2"}, "message"=>"Rhoconnect::Model::Exception"}}
                  I 03/14/2014 16:09:27:285 000024b4                  APP| Update rollback {"1"=>{"attributes"=>{"name"=>"Acme", "industry"=>"Electronics", "brand"=>"B1"}}, "2"=>{"attributes"=>{"name"=>"Best", "industry"=>"Software", "brand"=>"B2"}}}
                  I 03/14/2014 16:09:27:352 000024b4                  APP| ===============================================================
                  I 03/14/2014 16:09:27:427 000024b4                  APP| Processing update error 1 {"message"=>"Rhoconnect::Model::Exception", "attributes"=>{"sku"=>"test1"}}
                  I 03/14/2014 16:09:27:510 000024b4                  APP| Processing update error 2 {"attributes"=>{"sku"=>"test2"}, "message"=>"Rhoconnect::Model::Exception"}
                  I 03/14/2014 16:09:27:577 000024b4                  APP| retryArray [{"1"=>{"message"=>"Rhoconnect::Model::Exception", "attributes"=>{"sku"=>"test1"}}}, {"2"=>{"attributes"=>{"sku"=>"test2"}, "message"=>"Rhoconnect::Model::Exception"}}]
                  I 03/14/2014 16:09:27:644 000024b4                  APP| on_sync_update_error Exception: #<NoMethodError: undefined method `[]' for nil:NilClass>
                  I 03/14/2014 16:09:27:718 000024b4                  APP| App error: undefined method `[]' for nil:NilClass
                   C:/MotorolaRhoMobileSuite4.0.0/ruby/lib/ruby/gems/1.9.1/gems/rhodes-4.0.4/lib/framework/rhom/rhom_object_factory.rb:1228:in `block in on_sync_update_error'
                  C:/MotorolaRhoMobileSuite4.0.0/ruby/lib/ruby/gems/1.9.1/gems/rhodes-4.0.4/lib/framework/rhom/rhom_object_factory.rb:1219:in `each'
                  C:/MotorolaRhoMobileSuite4.0.0/ruby/lib/ruby/gems/1.9.1/gems/rhodes-4.0.4/lib/framework/rhom/rhom_object_factory.rb:1219:in `on_sync_update_error'
                  C:/MotorolaRhoMobileSuite4.0.0/ruby/lib/ruby/gems/1.9.1/gems/rhoconnect-client-4.0.4/ext/rhoconnect-client/RhoConnectClient.rb:61:in `on_sync_update_error'
                  C:/Users/J24472/Documents/motosample2/errorlogClient/app/Settings/controller.rb:142:in `sync_notify'
                  C:/MotorolaRhoMobileSuite4.0.0/ruby/lib/ruby/gems/1.9.1/gems/rhodes-4.0.4/lib/framework/rho/rhocontroller.rb:101:in `serve'
                  C:/MotorolaRhoMobileSuite4.0.0/ruby/lib/ruby/gems/1.9.1/gems/rhodes-4.0.4/lib/framework/rho/rhoapplication.rb:217:in `serve'
                  C:/MotorolaRhoMobileSuite4.0.0/ruby/lib/ruby/gems/1.9.1/gems/rhodes-4.0.4/lib/framework/rho/rho.rb:906:in `serve'
                  E 03/14/2014 16:09:28:028 00002564                  Net| An error occured connecting to the server: 500 returned.
                  I 03/14/2014 16:09:28:028 000024b4           HttpServer| GC Start.
                  

                   

                  Any thoughts?

                    • Re: Anyone know how to use rollback_objects with the on_sync_update_error method?
                      Kutir Mobility

                      John,

                      What is your Rhodes and Rhoconnect version?

                      Could you please post your Rhoconnect code snippet?

                       

                      Visnupriya R

                      Kutir Mobility

                        • Re: Re: Anyone know how to use rollback_objects with the on_sync_update_error method?
                          25c49980-cb2e-4113-a5a7-2f0078c85330

                          I'm trying this on Windows 7

                           

                          Versions are as follows....

                           

                          C:\TestFiles>gem list | grep rhodes
                          rhodes (4.0.4, 4.0.2, 4.0.0.9, 4.0.0)
                          rhodes-translator (0.0.4)
                          
                          
                          C:\TestFiles>gem list | grep rhoconnect
                          rhoconnect (4.0.3, 4.0.1, 4.0.0)
                          rhoconnect-adapters (1.0.6)
                          rhoconnect-client (4.0.4, 4.0.2, 4.0.0.9, 4.0.0)
                          

                           

                          Rhconnect test snippet....

                           

                            def update(update_hash)
                              
                              raise "Test"
                              
                              rescue Exception => e
                                p "Error Syncing Fred"
                                raise Rhoconnect::Model::Exception.new
                            end
                          

                           

                          Rhomobile Client Snippet....

                           

                          elsif status == "error"
                          
                                if @params['server_errors'] && @params['server_errors']['create-error']
                                  Rho::RhoConnectClient.on_sync_create_error(
                                  @params['source_name'], @params['server_errors']['create-error'].keys, :recreate )
                                end
                          
                                if @params['server_errors'] && @params['server_errors']['update-error']
                          
                                  puts "==============================================================="
                                  puts "Update error #{@params['server_errors']['update-error']}"
                                  puts "Update rollback #{@params['server_errors']['update-rollback']}"
                                  puts "==============================================================="
                          
                                  rollbackArray = []
                                  rollbackDataArray = []
                                  ignoreArray = []
                                  retryArray = []
                          
                                  @params['server_errors']['update-error'].each do |key, value|
                          
                                    puts "Processing update error #{key} #{value}"
                          
                                    # If we are to locally remove then log and add to remove array
                                    if value['message'].include?("Update had Error action - R")
                          
                                      rollbackArray[rollbackArray.length] = key
                                      rollbackDataArray[rollbackDataArray.length] = ['server_errors']['update-rollback'][key]
                          
                                      RhoLog.error("Eon Sync", "Locally rolling back update error #{key} #{value}")
                          
                                    # If we are to ignore (not tell user) then log and add to ignore array AND retry array as we still want to try again
                                    elsif value['message'].include?("Update had Error action - I")
                                      ignoreArray[ignoreArray.length] = key
                                      retryArray[retryArray.length] = {key => value}
                                      RhoLog.error("Eon Sync", "Ignoring update error #{key} #{value}")
                                      # Otherwise just add to the retry array
                                    else
                                      retryArray[retryArray.length] =  {key => value}
                                    end
                          
                                  end
                          
                                  if rollbackArray.length > 0
                                    puts "rbdata #{rollbackDataArray}"
                                    puts "rbarray #{rollbackArray}"
                                    Rho::RhoConnectClient.on_sync_update_error( @params['source_name'], rollbackArray, :rollback, rollbackDataArray)
                                  end
                          
                                  if retryArray.length > 0
                                    puts "retryArray #{retryArray}"
                                    Rho::RhoConnectClient.on_sync_update_error( @params['source_name'], retryArray, :retry)
                                  end
                          
                          
                                end
                          

                           

                          Thanks again.

                           

                          John

                            • Re: Anyone know how to use rollback_objects with the on_sync_update_error method?
                              Kutir Mobility

                              John,

                              Try

                              Rho::RhoConnectClient.on_sync_update_error( @params['source_name'], @params['server_errors']['update-error'], :rollback, @params['server_errors']['update-rollback'])
                              Rho::RhoConnectClient.on_sync_update_error( @params['source_name'], @params['server_errors']['update-error'], :retry)
                              

                              instead of

                              Rho::RhoConnectClient.on_sync_update_error( @params['source_name'], rollbackArray, :rollback, rollbackDataArray)
                              Rho::RhoConnectClient.on_sync_update_error( @params['source_name'], retryArray, :retry) 
                              

                              The rollbackArray and retryArray are arrays. so it is throwing error.

                              Let me know if you need more explanation.

                               

                               

                              Visnupriya R

                              Kutir Mobility

                              1 of 1 people found this helpful
                                • Re: Re: Anyone know how to use rollback_objects with the on_sync_update_error method?
                                  25c49980-cb2e-4113-a5a7-2f0078c85330

                                  Thanks again - I think my confusion was the inconsistency that seems to have appeared in Rhodes 4.0 whereby:

                                   

                                  Create seems to expect an array (hash.keys):

                                   

                                     if @params['server_errors'] && @params['server_errors']['create-error']
                                          Rho::RhoConnectClient.on_sync_create_error(
                                            @params['source_name'], @params['server_errors']['create-error'].keys, :recreate )
                                     end
                                  

                                   

                                  Update expects a hash (in fact two hashes when rolling back):

                                   

                                        if @params['server_errors'] && @params['server_errors']['update-error']
                                          Rho::RhoConnectClient.on_sync_update_error(
                                            @params['source_name'], @params['server_errors']['update-error'], :retry )
                                        end
                                  

                                   

                                  ..and the Rhodes 4.0 documentation doesn't seem to feature either.  The 2.2 docs do, but are quite vague regarding what the 'objects' are.  I'm not certain if the .keys call changed for 4.0.

                                   

                                  Creating a new client project in RhoStudio (which generates the code referenced above) allows you to see this (alas I wasn't clever enough to spot it), though if you've upgraded from 2.2, you may not necessarily have seen that.

                                   

                                  You answers have (I think) sorted me out, but I couldn't just supply the objects (hashes) that came into the error handler as we wanted to retry some records and roll-back others.

                                   

                                  Again, many thanks for your help.

                                   

                                  John