■ ■ ■ ■ ■ ■
changedetectionio/update_worker.py
| skipped 64 lines |
65 | 65 | | if uuid in list(self.datastore.data['watching'].keys()): |
66 | 66 | | |
67 | 67 | | changed_detected = False |
68 | | - | contents = "" |
| 68 | + | contents = b'' |
69 | 69 | | screenshot = False |
70 | 70 | | update_obj= {} |
71 | 71 | | xpath_data = False |
| 72 | + | process_changedetection_results = True |
| 73 | + | |
72 | 74 | | now = time.time() |
73 | 75 | | |
74 | 76 | | try: |
| skipped 5 lines |
80 | 82 | | raise Exception("Error - returned data from the fetch handler SHOULD be bytes") |
81 | 83 | | except PermissionError as e: |
82 | 84 | | self.app.logger.error("File permission error updating", uuid, str(e)) |
| 85 | + | process_changedetection_results = False |
83 | 86 | | except content_fetcher.ReplyWithContentButNoText as e: |
84 | 87 | | # Totally fine, it's by choice - just continue on, nothing more to care about |
85 | 88 | | # Page had elements/content but no renderable text |
| 89 | + | # Backend (not filters) gave zero output |
86 | 90 | | self.datastore.update_watch(uuid=uuid, update_obj={'last_error': "Got HTML content but no text found."}) |
| 91 | + | process_changedetection_results = False |
| 92 | + | |
87 | 93 | | except FilterNotFoundInResponse as e: |
88 | | - | err_text = "Filter '{}' not found - Did the page change its layout?".format(str(e)) |
89 | | - | c = 0 |
90 | | - | if self.datastore.data['watching'].get(uuid, False): |
| 94 | + | err_text = "Warning, filter '{}' not found".format(str(e)) |
| 95 | + | self.datastore.update_watch(uuid=uuid, update_obj={'last_error': err_text, |
| 96 | + | # So that we get a trigger when the content is added again |
| 97 | + | 'previous_md5': ''}) |
| 98 | + | |
| 99 | + | # Only when enabled, send the notification |
| 100 | + | if self.datastore.data['watching'][uuid].get('filter_failure_notification_send', False): |
91 | 101 | | c = self.datastore.data['watching'][uuid].get('consecutive_filter_failures', 5) |
92 | | - | c += 1 |
| 102 | + | c += 1 |
| 103 | + | # Send notification if we reached the threshold? |
| 104 | + | threshold = self.datastore.data['settings']['application'].get('filter_failure_notification_threshold_attempts', |
| 105 | + | 0) |
| 106 | + | print("Filter for {} not found, consecutive_filter_failures: {}".format(uuid, c)) |
| 107 | + | if threshold > 0 and c >= threshold: |
| 108 | + | self.send_filter_failure_notification(uuid) |
| 109 | + | c = 0 |
| 110 | + | self.datastore.update_watch(uuid=uuid, update_obj={'consecutive_filter_failures': c}) |
93 | 111 | | |
94 | | - | # Send notification if we reached the threshold? |
95 | | - | threshold = self.datastore.data['settings']['application'].get('filter_failure_notification_threshold_attempts', 0) |
96 | | - | print("Filter for {} not found, consecutive_filter_failures: {}".format(uuid, c)) |
97 | | - | if threshold >0 and c >= threshold: |
98 | | - | self.send_filter_failure_notification(uuid) |
99 | | - | c = 0 |
| 112 | + | process_changedetection_results = True |
100 | 113 | | |
101 | | - | self.datastore.update_watch(uuid=uuid, update_obj={'last_error': err_text, |
102 | | - | 'consecutive_filter_failures': c}) |
103 | 114 | | except content_fetcher.EmptyReply as e: |
104 | 115 | | # Some kind of custom to-str handler in the exception handler that does this? |
105 | 116 | | err_text = "EmptyReply - try increasing 'Wait seconds before extracting text', Status Code {}".format(e.status_code) |
| skipped 3 lines |
109 | 120 | | err_text = "Screenshot unavailable, page did not render fully in the expected time - try increasing 'Wait seconds before extracting text'" |
110 | 121 | | self.datastore.update_watch(uuid=uuid, update_obj={'last_error': err_text, |
111 | 122 | | 'last_check_status': e.status_code}) |
| 123 | + | process_changedetection_results = False |
112 | 124 | | except content_fetcher.PageUnloadable as e: |
113 | 125 | | err_text = "Page request from server didnt respond correctly" |
114 | 126 | | self.datastore.update_watch(uuid=uuid, update_obj={'last_error': err_text, |
| skipped 1 lines |
116 | 128 | | except Exception as e: |
117 | 129 | | self.app.logger.error("Exception reached processing watch UUID: %s - %s", uuid, str(e)) |
118 | 130 | | self.datastore.update_watch(uuid=uuid, update_obj={'last_error': str(e)}) |
| 131 | + | # Other serious error |
| 132 | + | process_changedetection_results = False |
| 133 | + | else: |
| 134 | + | # Mark that we never had any failures |
| 135 | + | update_obj['consecutive_filter_failures'] = 0 |
119 | 136 | | |
120 | | - | else: |
| 137 | + | # Different exceptions mean that we may or may not want to bump the snapshot, trigger notifications etc |
| 138 | + | if process_changedetection_results: |
121 | 139 | | try: |
122 | 140 | | watch = self.datastore.data['watching'][uuid] |
123 | 141 | | fname = "" # Saved history text filename |
| skipped 3 lines |
127 | 145 | | # A change was detected |
128 | 146 | | fname = watch.save_history_text(contents=contents, timestamp=str(round(time.time()))) |
129 | 147 | | |
130 | | - | # Generally update anything interesting returned |
131 | | - | update_obj['consecutive_filter_failures'] = 0 |
132 | 148 | | self.datastore.update_watch(uuid=uuid, update_obj=update_obj) |
133 | 149 | | |
134 | 150 | | # A change was detected |
| skipped 59 lines |
194 | 210 | | self.app.logger.error("Exception reached processing watch UUID: %s - %s", uuid, str(e)) |
195 | 211 | | self.datastore.update_watch(uuid=uuid, update_obj={'last_error': str(e)}) |
196 | 212 | | |
197 | | - | finally: |
| 213 | + | |
198 | 214 | | # Always record that we atleast tried |
199 | 215 | | self.datastore.update_watch(uuid=uuid, update_obj={'fetch_time': round(time.time() - now, 3), |
200 | 216 | | 'last_checked': round(time.time())}) |
| skipped 16 lines |