##// END OF EJS Templates
transaction: add onclose/onabort hook for pre-close logic...
Durham Goode -
r20881:3c47677a default
parent child Browse files
Show More
@@ -42,12 +42,27 b' def _playback(journal, report, opener, e'
42 opener.unlink(journal)
42 opener.unlink(journal)
43
43
44 class transaction(object):
44 class transaction(object):
45 def __init__(self, report, opener, journal, after=None, createmode=None):
45 def __init__(self, report, opener, journal, after=None, createmode=None,
46 onclose=None, onabort=None):
47 """Begin a new transaction
48
49 Begins a new transaction that allows rolling back writes in the event of
50 an exception.
51
52 * `after`: called after the transaction has been committed
53 * `createmode`: the mode of the journal file that will be created
54 * `onclose`: called as the transaction is closing, but before it is
55 closed
56 * `onabort`: called as the transaction is aborting, but before any files
57 have been truncated
58 """
46 self.count = 1
59 self.count = 1
47 self.usages = 1
60 self.usages = 1
48 self.report = report
61 self.report = report
49 self.opener = opener
62 self.opener = opener
50 self.after = after
63 self.after = after
64 self.onclose = onclose
65 self.onabort = onabort
51 self.entries = []
66 self.entries = []
52 self.map = {}
67 self.map = {}
53 self.journal = journal
68 self.journal = journal
@@ -126,6 +141,9 b' class transaction(object):'
126 @active
141 @active
127 def close(self):
142 def close(self):
128 '''commit the transaction'''
143 '''commit the transaction'''
144 if self.count == 1 and self.onclose is not None:
145 self.onclose()
146
129 self.count -= 1
147 self.count -= 1
130 if self.count != 0:
148 if self.count != 0:
131 return
149 return
@@ -149,6 +167,9 b' class transaction(object):'
149 self.usages = 0
167 self.usages = 0
150 self.file.close()
168 self.file.close()
151
169
170 if self.onabort is not None:
171 self.onabort()
172
152 try:
173 try:
153 if not self.entries:
174 if not self.entries:
154 if self.journal:
175 if self.journal:
General Comments 0
You need to be logged in to leave comments. Login now