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

View File

@ -484,16 +484,10 @@ class ApiController extends Zend_Controller_Action
if (is_null($file)) {
$file = Application_Model_StoredFile::Insert($md);
} else {
// path already exist
if ($file->getFileExistsFlag()) {
// file marked as exists
$return_hash['error'] = "File already exists in Airtime.";
return $return_hash;
} else {
// file marked as not exists
$file->setFileExistsFlag(true);
$file->setMetadata($md);
}
// If the file already exists we will update and make sure that
// it's marked as 'exists'.
$file->setFileExistsFlag(true);
$file->setMetadata($md);
}
}
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.
Which should mean that the old event should be discarded.
"""
self.logger.info("Attempting to register: '%s'" % str(evt))
if self.event_registered(evt):
old_e = self.get_old_event(evt)
# TODO : Perhaps there are other events that we can "contract"
@ -42,11 +43,20 @@ class EventContractor(Loggable):
elif isinstance(evt, DeleteFile):
old_e.morph_into(evt)
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) )
self.store[ evt.path ] = evt
return True # We actually added something, hence we return true.
def unregister(self, evt):
evt.reset_hook()
def __unregister(self, evt):
self.logger.info("Unregistering. Left: '%d'" % len(self.store.keys()))
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
# into another event
def reset_hook(self):
self._pack_hook()
self._pack_hook = lambda: None
def exists(self): return os.path.exists(self.path)
@LazyProperty

View File

@ -88,7 +88,7 @@ class OrganizeListener(BaseListener, pyinotify.ProcessEvent, Loggable):
dispatcher.send(signal=self.signal, sender=self,
event=OrganizeFile(f))
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)
def process_to_organize(self, event):

View File

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

View File

@ -49,4 +49,22 @@ class TestMMP(unittest.TestCase):
self.assertTrue( ev.register(e3) )
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()