Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The state file is not updated as expected when not using Watson from the CLI #213

Closed
jnsebgosselin opened this issue Jul 14, 2018 · 2 comments

Comments

@jnsebgosselin
Copy link
Contributor

From my understanding, the Watson class is instantiated every time a command is called from the CLI, so that the state of the private attributes of Watson._current and Watson._old_state are reinitialized to None after each command.

The problem occurs when using the same instance of Watson to start and stop a task in an event loop or a script. Because the Watson._old_state is never reinitialized to None between commands, its value is never updated. This prevents the update of the sate file in the save method. Below I present a MWE that illustrates this :

import os.path as osp
import os
from watson.watson import Watson

watson = Watson()

# We remove the state file if it exists:

if osp.exists(watson.state_file):
    os.remove(watson.state_file)

print('Watson is started =', watson.is_started)
print(watson._old_state, watson._current, end='\n\n')

# When starting Watson, the content of the state file is updated to
# that of Watson._current:

print('Starting Watson.')
watson.start('test')
print("Saving 'sate' file =", watson._old_state != watson._current)
print(watson._old_state, watson._current)
watson.save()
print(watson._load_json_file(watson.state_file), end='\n\n')

# When stopping, the content of the state file is not updated because
# both Watson._old_state and Watson._current equal {}:

print('Stoping Watson.')
watson.stop()
print("Saving 'sate' file =", watson._old_state != watson._current)
print(watson._old_state, watson._current)
watson.save()
print(watson._load_json_file(watson.state_file), end='\n\n')

# So when we re-initialized Watson, the Watson._current is initialized from the
# content of the state file. Therefore Watson.is_started return True, but it should
# return False:

print('Reinitializing Watson.')
watson = Watson()
print('Watson is started =', watson.is_started)
print(watson._old_state, watson._current)

This results in :

Watson is started = False
{} {}

Starting Watson.
Saving 'sate' file = True
{} {'project': 'test', 'start': <Arrow [2018-07-13T14:50:19.818903-04:00]>, 'tags': [], 'message': None}
{'project': 'test', 'start': 1531507819, 'tags': [], 'message': None}

Stoping Watson.
Saving 'sate' file = False
{} {}
{'project': 'test', 'start': 1531507819, 'tags': [], 'message': None}

Reinitializing Watson.
Watson is started = True
{'project': 'test', 'start': <Arrow [2018-07-13T14:50:19-04:00]>, 'tags': [], 'message': None} {'project': 'test', 'start': <Arrow [2018-07-13T14:50:19-04:00]>, 'tags': [], 'message': None}

Maybe a solution would be to update the value of Watson._old_state every time the state file is saved in the save method here, something like:

safe_save(self.state_file, make_json_writer(lambda: current))
self._old_state = current
@jmaupetit
Copy link
Contributor

Thanks for the report @jnsebgosselin ❤️ I didn't know about your qwatson project, it looks awesome! Can you propose a PR to review for your suggestion?

@jnsebgosselin
Copy link
Contributor Author

@jmaupetit Sure thing, I can also add a test to cover that corner case if you want.

Also, tyvm for your interest in my current little pet project :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants