# Need help with a timing? problem

**URL:** <https://www.ruby-forum.com/t/need-help-with-a-timing-problem/80635>\
**Category:** Ruby\
**Created:** [December 29, 2006, 3:36pm UTC](https://www.ruby-forum.com/t/need-help-with-a-timing-problem/80635 "2006-12-29T15:36:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 29, 2006, 3:36pm UTC](https://www.ruby-forum.com/t/need-help-with-a-timing-problem/80635/1 "2006-12-29T15:36:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 29, 2006, 3:46pm UTC](https://www.ruby-forum.com/t/need-help-with-a-timing-problem/80635/2 "2006-12-29T15:46:16Z")

</div>

On 12/29/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

It’s needed.

thanks,

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 29, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/need-help-with-a-timing-problem/80635/3 "2006-12-29T16:56:34Z")

</div>

removed\_email\_address@domain.invalid wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 29, 2006, 7:15pm UTC](https://www.ruby-forum.com/t/need-help-with-a-timing-problem/80635/4 "2006-12-29T19:15:19Z")

</div>

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, :password, :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:ADO:Provider=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::DatabaseError => 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::DIR\_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()
