Merge lp:~julien-spautz/cerbere/watchdog-refactor into lp:~elementary-pantheon/cerbere/cerbere

Proposed by Julien Spautz
Status: Merged
Approved by: Cody Garver
Approved revision: 68
Merged at revision: 47
Proposed branch: lp:~julien-spautz/cerbere/watchdog-refactor
Merge into: lp:~elementary-pantheon/cerbere/cerbere
Diff against target: 126 lines (+57/-46)
1 file modified
src/Watchdog.vala (+57/-46)
To merge this branch: bzr merge lp:~julien-spautz/cerbere/watchdog-refactor
Reviewer Review Type Date Requested Status
Victor Martinez (community) Approve
David Gomes (community) Approve
Review via email: mp+186937@code.launchpad.net

Commit message

Refactored the code in Watchdog.vala to eliminate the nested if clauses and make it more readable.

Description of the change

Refactored the code in Watchdog.vala to eliminate the nested if clauses and make it more readable.

None of the behavior should have changed, and the API is also untouched.

I commited each small refactoring step so you can see that they don't have any side-effects.

To post a comment you must log in.
Revision history for this message
Victor Martinez (victored) wrote :

Hey, nice work!

+1 for improving readability. This could use further improvements.

- "requires" is being used for things that can, and are expected to fail at runtime, instead of static contract definition. It is not a programming error to call "add_process" and pass the name of a process that is already monitored. This can happen if a user adds the same string twice to monitored-processed in GSettings. Instead, the method should just do nothing, and/or print a warning, but assertions are not applicable here.

- For consistency, since there is "is_new_command", the statements "processes.unset (command)" should be replaced by a new method "remove_command" (preferrably "unmonitor_command") that does it, so that nobody accesses the hashmap directly.

- Diff line 91 should be: "if (is_new_command (command)) { ...".

- Diff line 32 should be: "return !processes.has_key (command);". Same style change is desired fo diff line 112.

- Diff line 102: use "%u" instead of "%s" to avoid calling "to_string" for "max_crashes".

- Instead of "is_new_command" and "remove_command" (not there yet), I would suggest using "is_command_monitored" and "unmonitor_command", respectively.

review: Needs Fixing
62. By Julien Spautz

replace 'processes.unset (command)' with 'unmonitor_command (command)'

63. By Julien Spautz

fixed strings and use ! instead of == false

64. By Julien Spautz

use is_new_command instead of \!processes.has_key (command)

65. By Julien Spautz

turn pre condition into simple fallthrough if clause

66. By Julien Spautz

add monitor_and_run_command method

67. By Julien Spautz

replace is_new_command with \!command_is_monitored

Revision history for this message
Julien Spautz (julien-spautz) wrote :

Fixed everything you said (hopefully) and additionally created a monitor_and_run_command () method.

Revision history for this message
David Gomes (davidgomes) wrote :

Should be WatchDog not Watchdog.

review: Needs Fixing
Revision history for this message
Victor Martinez (victored) wrote :

Julien, I like the changes, thank you!

Even though it's implied, for a future branch it would be nice if you could explicitly define the methods as "private" to keep the code consistent.

David, since Julien didn't write that class definition, can you propose a separate branch for that?

review: Approve
Revision history for this message
David Gomes (davidgomes) wrote :

Agreed, will do that when I get home.

review: Approve
68. By Julien Spautz

add explicit 'private' for consistency

Revision history for this message
Julián Unrrein (junrrein) wrote :

Is this ready for landing?

Revision history for this message
Victor Martinez (victored) wrote :

It is ready for landing. I've already proof-read the changes and the control flow remains the same, but written in a nicer way.

review: Approve
Revision history for this message
Victor Martinez (victored) wrote :

We can replace diff lines 100-105 with "if (!command_is_monitored) assert_not_reached ();", since the warning is not visible and doesn't add any value (I should have never added it).

That is for a separate branch though. Anyone up to propose another merge with that change?

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/Watchdog.vala'
2--- src/Watchdog.vala 2012-11-22 23:12:01 +0000
3+++ src/Watchdog.vala 2013-11-09 18:17:48 +0000
4@@ -34,64 +34,75 @@
5 processes = new Gee.HashMap<string, ProcessWrapper> ();
6 }
7
8- public void add_process (string command) {
9- if (command.strip () == "")
10- return;
11-
12- // Check if a process for this command has already been created
13- if (processes.has_key (command))
14- return;
15-
16+ public void add_process (string command)
17+ requires (is_valid_command (command)) {
18+
19+ if (command_is_monitored (command)) {
20+ warning ("Command '%s' is already being monitored.", command);
21+ return;
22+ }
23+
24+ monitor_and_run_command (command);
25+ }
26+
27+ private bool is_valid_command (string command) {
28+ return command.strip () != "";
29+ }
30+
31+ private bool command_is_monitored (string command) {
32+ return processes.has_key (command);
33+ }
34+
35+ private void monitor_and_run_command (string command) {
36 var process = new ProcessWrapper (command);
37 processes[command] = process;
38-
39 process.exited.connect (on_process_exit);
40-
41 process.run_async ();
42 }
43
44 /**
45- * Process exit handler.
46- *
47- * Respawning occurs here. If the process has crashed more times than max_crashes, it's not
48- * respawned again. Otherwise, it is assumed that the process exited normally and the crash
49- * count is reset to 0, which means that only consecutive crashes are counted.
50+ * Respawning occurs here. If the process has crashed more times than
51+ * max_crashes, it's not respawned again. Otherwise, it is assumed that the
52+ * process exited normally and the crash count is reset to 0, which means
53+ * that only consecutive crashes are counted.
54 */
55 private void on_process_exit (ProcessWrapper process, bool normal_exit) {
56- if (normal_exit) {
57- // Reset crash count. We only want to count consecutive crashes, so that
58- // if a normal exit is detected, we reset the counter to 0.
59+ if (normal_exit)
60 process.reset_crash_count ();
61- }
62
63- bool remove_process = false;
64 string command = process.command;
65
66- // if still in the process list, relaunch if possible
67- if (command in App.settings.process_list) {
68- // Check if the process is still present in the map since it could have been removed
69- if (processes.has_key (command)) {
70- // Check if the process already exceeded the maximum number of allowed crashes.
71- uint max_crashes = App.settings.max_crashes;
72-
73- if (process.crash_count <= max_crashes) {
74- process.run_async (); // Reload right away
75- } else {
76- warning ("'%s' exceeded the maximum number of crashes allowed (%s). It won't be launched again", command, max_crashes.to_string ());
77- remove_process = true;
78- }
79- } else {
80- // If a process is not in the map, it means it wasn't re-launched after it exited, so in theory
81- // this code is never reached.
82- critical ("Please file a bug at http://launchpad.net/cerbere and attach your .xsession-errors and .xsession-errors.old files.");
83- }
84- } else {
85+ if (is_not_in_settings (command)) {
86 warning ("'%s' is no longer in settings (not monitored)", command);
87 process.reset_crash_count ();
88- remove_process = true;
89- }
90-
91- if (remove_process)
92- processes.unset (command);
93- }
94-}
95\ No newline at end of file
96+ unmonitor_command (command);
97+ return;
98+ }
99+
100+ if (!command_is_monitored (command)) {
101+ critical ("Please file a bug at http://launchpad.net/cerbere and " +
102+ "attach your .xsession-errors and .xsession-errors.old " +
103+ "files.");
104+ return;
105+ }
106+
107+ uint max_crashes = App.settings.max_crashes;
108+
109+ if (process.crash_count > max_crashes) {
110+ warning ("'%s' exceeded the maximum number of crashes allowed " +
111+ "(%u). It won't be launched again", command, max_crashes);
112+ unmonitor_command (command);
113+ return;
114+ }
115+
116+ process.run_async ();
117+ }
118+
119+ private bool is_not_in_settings (string command) {
120+ return !(command in App.settings.process_list);
121+ }
122+
123+ private void unmonitor_command (string command) {
124+ processes.unset (command);
125+ }
126+}

Subscribers

People subscribed via source and target branches

to all changes: