Need help with a timing? problem

Discussion in 'Ruby' started by lrlebron@gmail.com, Dec 29, 2006.

  1. Guest

    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
     
    , Dec 29, 2006
    #1
    1. Advertising

  2. Phrogz Guest

    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.
     
    Phrogz, Dec 29, 2006
    #2
    1. Advertising

  3. Guest

    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()







    Phrogz wrote:
    > 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.
     
    , Dec 29, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kload

    Strange Timing Problem

    Kload, Oct 23, 2003, in forum: VHDL
    Replies:
    2
    Views:
    609
    Kload
    Oct 23, 2003
  2. xiaobin
    Replies:
    0
    Views:
    1,439
    xiaobin
    Apr 18, 2004
  3. Frank
    Replies:
    6
    Views:
    370
    Frank
    Feb 20, 2006
  4. skipperdodle
    Replies:
    1
    Views:
    675
    skipperdodle
    Oct 17, 2007
  5. jaquesgrobler

    Timing issues !!! help help!

    jaquesgrobler, May 8, 2008, in forum: VHDL
    Replies:
    0
    Views:
    523
    jaquesgrobler
    May 8, 2008
Loading...

Share This Page