cc-4228: fixed this ticket by stomping out bugs in eventcontractor and tweaking ApiController

This commit is contained in:
Rudi Grinberg 2012-08-17 12:37:26 -04:00
parent 10a7eae498
commit 5b134d0055
6 changed files with 40 additions and 14 deletions
airtime_mvc/application/controllers
python_apps/media-monitor2

View File

@ -484,16 +484,10 @@ class ApiController extends Zend_Controller_Action
if (is_null($file)) { if (is_null($file)) {
$file = Application_Model_StoredFile::Insert($md); $file = Application_Model_StoredFile::Insert($md);
} else { } else {
// path already exist // If the file already exists we will update and make sure that
if ($file->getFileExistsFlag()) { // it's marked as 'exists'.
// file marked as exists $file->setFileExistsFlag(true);
$return_hash['error'] = "File already exists in Airtime."; $file->setMetadata($md);
return $return_hash;
} else {
// file marked as not exists
$file->setFileExistsFlag(true);
$file->setMetadata($md);
}
} }
} }
else if ($mode == "modify") { else if ($mode == "modify") {

View File

@ -29,6 +29,7 @@ class EventContractor(Loggable):
some other event in the storage was morphed into this newer one. some other event in the storage was morphed into this newer one.
Which should mean that the old event should be discarded. Which should mean that the old event should be discarded.
""" """
self.logger.info("Attempting to register: '%s'" % str(evt))
if self.event_registered(evt): if self.event_registered(evt):
old_e = self.get_old_event(evt) old_e = self.get_old_event(evt)
# TODO : Perhaps there are other events that we can "contract" # TODO : Perhaps there are other events that we can "contract"
@ -42,11 +43,20 @@ class EventContractor(Loggable):
elif isinstance(evt, DeleteFile): elif isinstance(evt, DeleteFile):
old_e.morph_into(evt) old_e.morph_into(evt)
return False return False
# Unregister the old event anyway, because we only want to keep
# track of the old one. This means that the old event cannot be
# morphed again and new events with the same path will only be
# checked against the newest event 'evt' in this case
self.unregister( old_e )
evt.add_safe_pack_hook( lambda : self.__unregister(evt) ) evt.add_safe_pack_hook( lambda : self.__unregister(evt) )
self.store[ evt.path ] = evt self.store[ evt.path ] = evt
return True # We actually added something, hence we return true. return True # We actually added something, hence we return true.
def unregister(self, evt):
evt.reset_hook()
def __unregister(self, evt): def __unregister(self, evt):
self.logger.info("Unregistering. Left: '%d'" % len(self.store.keys()))
try: del self.store[evt.path] try: del self.store[evt.path]
except Exception as e: self.unexpected_exception(e) except Exception as e:
self.unexpected_exception(e)
self.logger.info("Unregistering. Left: '%d'" % len(self.store.keys()))

View File

@ -59,6 +59,10 @@ class BaseEvent(Loggable):
self._pack_hook = lambda: None # no op self._pack_hook = lambda: None # no op
# into another event # into another event
def reset_hook(self):
self._pack_hook()
self._pack_hook = lambda: None
def exists(self): return os.path.exists(self.path) def exists(self): return os.path.exists(self.path)
@LazyProperty @LazyProperty

View File

@ -88,7 +88,7 @@ class OrganizeListener(BaseListener, pyinotify.ProcessEvent, Loggable):
dispatcher.send(signal=self.signal, sender=self, dispatcher.send(signal=self.signal, sender=self,
event=OrganizeFile(f)) event=OrganizeFile(f))
flushed += 1 flushed += 1
self.logger.info("Flushed organized directory with %d files" % flushed) #self.logger.info("Flushed organized directory with %d files" % flushed)
@IncludeOnly(mmp.supported_extensions) @IncludeOnly(mmp.supported_extensions)
def process_to_organize(self, event): def process_to_organize(self, event):

View File

@ -20,7 +20,7 @@ class ManagerTimeout(threading.Thread,Loggable):
while True: while True:
time.sleep(3) time.sleep(3)
self.manager.flush_organize() self.manager.flush_organize()
self.logger.info("Force flushed organize...") #self.logger.info("Force flushed organize...")
class Manager(Loggable): class Manager(Loggable):
""" """

View File

@ -49,4 +49,22 @@ class TestMMP(unittest.TestCase):
self.assertTrue( ev.register(e3) ) self.assertTrue( ev.register(e3) )
self.assertTrue( ev.register(e2) ) self.assertTrue( ev.register(e2) )
def test_register2(self):
ev = EventContractor()
p = 'bullshit.mp3'
events = [
NewFile( FakePyinotify(p) ),
NewFile( FakePyinotify(p) ),
DeleteFile( FakePyinotify(p) ),
NewFile( FakePyinotify(p) ),
NewFile( FakePyinotify(p) ), ]
actual_events = []
for e in events:
if ev.register(e):
actual_events.append(e)
self.assertEqual( len(ev.store.keys()), 1 )
packed = [ x.safe_pack() for x in actual_events ]
print(packed)
if __name__ == '__main__': unittest.main() if __name__ == '__main__': unittest.main()