13
I need to do a series of operations involving binary files (you can’t use BD), and I need to ensure that they end successfully even in the event of failure in the middle of the operation. For this, I see no way out except to implement a system of journaling manually. I started writing a code, but I’m not sure if it’s correct (i.e. if there are no cases where a failure in the middle of the task can put it in an inconsistent state - including failure in the actual writing of the Journal).
Is there anything ready in that direction? And if there is, there is some problem in my attempt to remedy below?
def nova_tarefa(args):
refazer_journal() # Refaz o que ficou inacabado da última vez (se houver)
with open('args.json', 'w') as f:
json.dump(args, f) # Prepara as novas tarefas
with open('journal.txt', 'w') as f:
f.write('comecar\n') # Coloca as novas tarefas no journal
refazer_journal() # Faz as novas tarefas
def refazer_journal():
try:
with open('journal.txt', 'r') as f:
passos = [x.strip() for x in f.readlines() if x.strip()]
except:
if not os.path.exists('journal.txt'):
passos = []
else:
raise
if not passos: # Se não há nada inacabado, termina
return
with open('args.json', 'r') as f:
args = json.load(f)
# Realiza as tarefas que ainda não foram marcadas como concluídas
for indice,tarefa in enumerate(args['tarefas']):
if len(passos) <= indice+1:
realizar_tarefa_indepotente(tarefa)
with open('journal.txt', 'a') as f:
f.write('\ntarefa concluida\n')
with open('journal.txt', 'w') as f:
pass # Tudo foi feito com sucesso: esvazia o journal
(Note: what is written on Journal - comecar
, tarefa concluida
- it is not important, if only one letter is written on the line it is already considered that the step has been successful)
Code for testing (always worked with me - including editing the journal.txt
to enter a series of errors):
def realizar_tarefa_indepotente(tarefa):
if 'falhar' in tarefa:
raise Exception('Tarefa falhou!')
print 'Realizando tarefa: ' + tarefa['nome']
tarefa_ok = {'tarefas':[{'nome':'foo'}, {'nome':'bar'}, {'nome':'baz'}]}
falha_3 = {'tarefas':[{'nome':'foo'}, {'nome':'bar'}, {'nome':'baz','falhar':True}]}
falha_2 = {'tarefas':[{'nome':'foo'}, {'nome':'bar','falhar':True}, {'nome':'baz'}]}
falha_1 = {'tarefas':[{'nome':'foo','falhar':True}, {'nome':'bar'}, {'nome':'baz'}]}
falha_1_3 = {'tarefas':[{'nome':'foo','falhar':True}, {'nome':'bar'}, {'nome':'baz','falhar':True}]}
Note: it is my understanding that code review (code review) is part of our focus (is on topic) currently. If anyone does not agree, please manifest yourself in the goal.
– mgibsonbr
I didn’t want to put this as an answer, since it’s just a link, but I find it interesting to document. That solution satisfy you?
– Felipe Avelar
@Felipeavelar Most of the features I found when searching for "python Journal" referred not to journaling [in the context of file system] but to the implementation of some kind of "diary"... I believe that this link is also about this - since it is a plugin for Plone (CMS system).
– mgibsonbr