Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Need help with a timing? problem

Reply
Thread Tools

Need help with a timing? problem

 
 
lrlebron@gmail.com
Guest
Posts: n/a
 
      12-29-2006
I am running Ruby 1.8.5 and windows XP. I am working on a program that
monitors a directory and updates and updates a database based on the
changes. At the moment I have some code to handle files that are added.
Basically I have 3 methods:

Pseudo code

def monitor
event_handler(array)
end

event_handler(array)
p array
If file is added
file_added(arr)
end
end

file_added(array)
#Insert into database
end

Here's my problem. Let's say I drag 10 files into the directory. If I
only have the event_handler print the array I get all 10. However, when
the event_handler calls the file_added method I only get 5 or 6 files
inserted in the database. The code is not throwing exceptions.

Any ideas on how I can fix this? I can post the code if needed.

thanks,

Luis

 
Reply With Quote
 
 
 
 
Phrogz
Guest
Posts: n/a
 
      12-29-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I am running Ruby 1.8.5 and windows XP. I am working on a program that
> monitors a directory and updates and updates a database based on the
> changes. At the moment I have some code to handle files that are added.
> Basically I have 3 methods:
>
> Pseudo code
>
> def monitor
> event_handler(array)
> end
>
> event_handler(array)
> p array
> If file is added
> file_added(arr)
> end
> end
>
> file_added(array)
> #Insert into database
> end
>
> Here's my problem. Let's say I drag 10 files into the directory. If I
> only have the event_handler print the array I get all 10. However, when
> the event_handler calls the file_added method I only get 5 or 6 files
> inserted in the database. The code is not throwing exceptions.


You'd better post code to your file_added method, and ensure that the
same array is getting passed in, and still has 10 items in it.

 
Reply With Quote
 
 
 
 
lrlebron@gmail.com
Guest
Posts: n/a
 
      12-29-2006
The event_handler will pass 10 arrays to the file_added method. One for
each file. Here's the whole script

require 'dbi'
require 'yaml'
require 'win32/changenotify'
require 'win32/file'
include Win32

class Fsm

attr_accessor :server, :db, :user, assword, :folder_to_watch,
:count

def initialize(folder)

dbconfig = YAML::load(IO.read('config.yml'))

@server = dbconfig['server']
@db = dbconfig['db']
@user = dbconfig['user']
@password = dbconfig['password']
@folder_to_watch = folder
@count = 0
@dbh = DBI.connect("DBI:ADOrovider=SQLNCLI; Data
Source=#{@server};Database=#{@db};uid=#{@user}; pwd=#{@password};
{'NonBlocking'=>true}")

end

def tmf_insert_new_file(name, length, creation_time, directory,
extension, fullname, is_read_only, last_access_time, last_write_time,
file_type, parent_directory)
sql = "DECLARE @return_value int EXEC @return_value =
sp_UpdateMasterFileList @Name = ?, @Length = ?, @CreationTime =
?, @DirectoryName = ?, @Extension = ?, @FullName =?, @IsReadOnly =
?, @LastAccessTime = ?, @LastWriteTime = ?, @FileType =
?, @ParentDirectory = ? SELECT 'Return Value' = @return_value"

myKey = ""
sth = @dbh.execute(sql, name, length, creation_time, directory,
extension, fullname, is_read_only, last_access_time, last_write_time,
file_type, parent_directory)
myKey = sth.fetch
sth.finish

@dbh.commit()

return myKey.to_s

rescue DBI:atabaseError => e
puts "An error occurred in tmf_insert_new_file"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"

end

def monitor
filter = ChangeNotify::FILE_NAME | ChangeNotify:IR_NAME |
ChangeNotify::LAST_WRITE

cn = ChangeNotify.new(@folder_to_watch, true, filter)

cn.wait{|arr|

event_handler(arr)

} while true

rescue Exception => e
puts e
end

def event_handler(arr)

if arr[0][0].to_s.include? "added"
#puts @count+=1
#puts "Adding file " + arr[0][1].to_s
file_added(arr)
end

rescue Exception => e
puts e

end

def file_added(arr)

date_format_str = "%Y%m%d %X"
filename = arr[0][1].to_s

if filename.include?"/"
filename.gsub!("/","\\")
end

if @folder_to_watch.include?"/"
@folder_to_watch.gsub!("/","\\")
end

fullname = @folder_to_watch + filename
stat = File::Stat.new(fullname)
file_type = File.ftype(fullname).capitalize
dirname = File.dirname(fullname)

if file_type.include? "File"

myKey = tmf_insert_new_file(File.basename(filename),
stat.size, stat.ctime.strftime(date_format_str), dirname,
File.extname(fullname), fullname,0,
stat.atime.strftime(date_format_str),
stat.mtime.strftime(date_format_str), file_type,
File.dirname(fullname))

else

myKey = tmf_insert_new_file(File.basename(filename), 0,
stat.ctime.strftime(date_format_str), fullname, 'None', fullname, 0,
stat.atime.strftime(date_format_str),
stat.mtime.strftime(date_format_str), "Directory", dirname)

end

rescue Exception =>e
puts "Error in file_added method."
puts e

end

end

MyFSM = Fsm.new('C:\\')
MyFSM.monitor()







Phrogz wrote:
> (E-Mail Removed) wrote:
> > I am running Ruby 1.8.5 and windows XP. I am working on a program that
> > monitors a directory and updates and updates a database based on the
> > changes. At the moment I have some code to handle files that are added.
> > Basically I have 3 methods:
> >
> > Pseudo code
> >
> > def monitor
> > event_handler(array)
> > end
> >
> > event_handler(array)
> > p array
> > If file is added
> > file_added(arr)
> > end
> > end
> >
> > file_added(array)
> > #Insert into database
> > end
> >
> > Here's my problem. Let's say I drag 10 files into the directory. If I
> > only have the event_handler print the array I get all 10. However, when
> > the event_handler calls the file_added method I only get 5 or 6 files
> > inserted in the database. The code is not throwing exceptions.

>
> You'd better post code to your file_added method, and ensure that the
> same array is getting passed in, and still has 10 items in it.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Help, I am intermediate in Java...need help in follow case ElementX Java 9 10-01-2008 08:02 PM
Help Help. I really need some help with this =?Utf-8?B?Q2hyaXM=?= ASP .Net 3 01-31-2007 09:33 PM
re_---need help Network Adapters!!!! NEED HELP!!!! hedayatniac@gmail.com Computer Support 4 08-13-2006 01:03 AM
Please help!!! Need datagrid selection to fill textboxes...Need quick!! TN Bella ASP .Net 1 06-18-2004 01:31 AM
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments