James Hughes
2006-Nov-16 21:49 UTC
[Backgroundrb-devel] Assigning to results - missing something
Hi, I''m trying to convert an app to use 0.2.0, and having a little trouble understanding how to use the results hash. Formerly my worker had an instance variable, @file_stats which was initialized to an empty hash; I have replaced @file_stats with results[:file_hash]. Actually my initial pass at this was just to do a s/@file_stats/results[:file_hash]/. Which gets me the following: results[:file_stats] = {} logger.debug " Generating checksum for #{path}..." results[:file_stats][path][:checksum] Digest::MD5.hexdigest(File.read(path)) logger.debug " ...done" logger.debug " Getting file size for #{path}..." results[:file_stats][path][:size] = File.size(path) logger.debug results[:file_stats][path][:size] So as soon as I attempt the checksum assignment above, bgrb disappears. I see the ''generating checksum'' message, but it never says ''...done''. Jason Sydes has intimated to me in another thread that I can''t do what I''m attempting above. But a hash is a hash is a hash, right? Or is it? James ps. I initially thought this was an issue with the results worker not starting properly as it doesn''t announce it''s presence in the log. But I''m seeing it in the process list, so I assume I can discount that possibility.
* James Hughes (hughes.james at gmail.com) [061117 08:47]:> Hi, > > I''m trying to convert an app to use 0.2.0, and having a little trouble > understanding how to use the results hash. > > Formerly my worker had an instance variable, @file_stats which was > initialized to an empty hash; I have replaced @file_stats with > results[:file_hash]. Actually my initial pass at this was just to do a > s/@file_stats/results[:file_hash]/. Which gets me the following: > > results[:file_stats] = {} > logger.debug " Generating checksum for #{path}..." > results[:file_stats][path][:checksum] > Digest::MD5.hexdigest(File.read(path)) > logger.debug " ...done" > logger.debug " Getting file size for #{path}..." > results[:file_stats][path][:size] = File.size(path) > logger.debug results[:file_stats][path][:size]give this a try: file_stats = {} checksum = Digest::MD5.hexdigest(File.read(path)) file_stats[path][:checksum] = checksum results[:file_stats] = file_stats It''s not quite a regular hash. The results method returns a hash, which include the current data for the particular worker. We then inject a singleton override for assignment on the top level hash key, so that when you do assignment, this will be sent to the results worker and merged with the results data for the worker. It might be possible to do the same with values (including sub hashes), but then you loose a lot of visibility, since objects with singleton methods don''t de/serialize easily - this is why you need to call #to_hash on results to actually see the data. We might find a better way to do this in a later release. For now you just have to live with temporary nested hashes, which you assign directly to results[:key]. This is documented in the README in trunk. /skaar -- ---------------------------------------------------------------------- |\|\ where in the | s_u_b_s_t_r_u_c_t_i_o_n | | >=========== W.A.S.T.E. | genarratologies |/|/ (_) is the wisdom | skaar at waste.org ----------------------------------------------------------------------