Class: EventMachine::FileWatch

Inherits:
Connection show all
Defined in:
lib/em/file_watch.rb

Overview

Note:

On Mac OS X, file watching only works when kqueue is enabled

Utility class that is useful for file monitoring. Supported events are

  • File is modified

  • File is deleted

  • File is moved

See Also:

Constant Summary collapse

Cmodified =
'modified'.freeze
Cdeleted =
'deleted'.freeze
Cmoved =
'moved'.freeze

Instance Attribute Summary

Attributes inherited from Connection

#signature

Instance Method Summary collapse

Methods inherited from Connection

#associate_callback_target, #close_connection, #close_connection_after_writing, #comm_inactivity_timeout, #comm_inactivity_timeout=, #connection_completed, #detach, #error?, #get_cipher_bits, #get_cipher_name, #get_cipher_protocol, #get_idle_time, #get_outbound_data_size, #get_peer_cert, #get_peername, #get_pid, #get_proxied_bytes, #get_sni_hostname, #get_sock_opt, #get_sockname, #get_status, #initialize, new, #notify_readable=, #notify_readable?, #notify_writable=, #notify_writable?, #pause, #paused?, #pending_connect_timeout, #pending_connect_timeout=, #post_init, #proxy_completed, #proxy_incoming_to, #proxy_target_unbound, #reconnect, #resume, #send_data, #send_datagram, #send_file_data, #set_sock_opt, #ssl_handshake_completed, #ssl_verify_peer, #start_tls, #stop_proxying, #stream_file_data, #unbind

Constructor Details

This class inherits a constructor from EventMachine::Connection

Instance Method Details

#file_deletedObject

This method is abstract.
Note:

On Linux (with inotify), this method will not be called until all open file descriptors to the file have been closed.

Will be called when the file is deleted. Supposed to be redefined by subclasses. When the file is deleted, stop_watching will be called after this to make sure everything is cleaned up correctly.



56
57
# File 'lib/em/file_watch.rb', line 56

def file_deleted
end

#file_modifiedObject

This method is abstract.

Will be called when the file is modified. Supposed to be redefined by subclasses.



45
46
# File 'lib/em/file_watch.rb', line 45

def file_modified
end

#file_movedObject

This method is abstract.

Will be called when the file is moved or renamed. Supposed to be redefined by subclasses.



62
63
# File 'lib/em/file_watch.rb', line 62

def file_moved
end

#pathString

Note:

Current implementation does not pick up on the new filename after a rename occurs.

Returns the path that is being monitored.

Returns:

  • (String)

See Also:



38
39
40
# File 'lib/em/file_watch.rb', line 38

def path
  @path
end

#receive_data(data) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/em/file_watch.rb', line 21

def receive_data(data)
  case data
  when Cmodified
    file_modified
  when Cdeleted
    file_deleted
  when Cmoved
    file_moved
  end
end

#stop_watchingObject

Discontinue monitoring of the file.

This involves cleaning up the underlying monitoring details with kqueue/inotify, and in turn firing Connection#unbind. This will be called automatically when a file is deleted. User code may call it as well.



69
70
71
# File 'lib/em/file_watch.rb', line 69

def stop_watching
  EventMachine::unwatch_filename(@signature)
end