Skip to content

Commit 3e018bf

Browse files
author
matthias_schaub
committed
resolve of merge conflict dev <- logging.
2 parents 3508291 + c203d82 commit 3e018bf

File tree

6 files changed

+60
-177
lines changed

6 files changed

+60
-177
lines changed

mapswipe_workers/config/example-configuration.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
},
2727
"slack": {
2828
"token": "your_slack_token",
29-
"channel": "your_slack_channel",
30-
"username": "your_slack_username"
29+
"channel": "your_slack_channel"
3130
},
3231
"sentry": {
3332
"dsn": "your_sentry_dsn_value"

mapswipe_workers/mapswipe_workers/definitions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import logging.config
44
import os
55

6+
import sentry_sdk
7+
68
from mapswipe_workers.project_types.build_area.build_area_project import (
79
BuildAreaProject,
810
)
@@ -50,3 +52,6 @@ def load_config(CONFIG_PATH) -> dict:
5052

5153
logging.config.fileConfig(fname=LOGGING_CONFIG_PATH, disable_existing_loggers=True)
5254
logger = logging.getLogger("Mapswipe Workers")
55+
56+
sentry_sdk.init(CONFIG["sentry"]["dsn"])
57+
sentry = sentry_sdk

mapswipe_workers/mapswipe_workers/mapswipe_workers.py

Lines changed: 20 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
import json
55
import time
66

7-
import click
87
import schedule as sched
8+
9+
import click
910
from mapswipe_workers import auth
1011
from mapswipe_workers.definitions import (
1112
PROJECT_TYPE_CLASSES,
12-
PROJECT_TYPE_NAMES,
1313
CustomError,
1414
logger,
15+
sentry,
1516
)
1617
from mapswipe_workers.firebase_to_postgres import (
1718
archive_project,
@@ -20,10 +21,9 @@
2021
)
2122
from mapswipe_workers.generate_stats import generate_stats
2223
from mapswipe_workers.project_types.build_area import build_area_tutorial
23-
from mapswipe_workers.project_types.change_detection import (
24-
change_detection_tutorial,
25-
)
26-
from mapswipe_workers.utils import sentry, slack, user_management
24+
from mapswipe_workers.project_types.change_detection import change_detection_tutorial
25+
from mapswipe_workers.utils import user_management
26+
from mapswipe_workers.utils.slack_helper import send_slack_message
2727

2828

2929
class PythonLiteralOption(click.Option):
@@ -63,6 +63,7 @@ def run_create_projects():
6363
for project_draft_id, project_draft in project_drafts.items():
6464
project_draft["projectDraftId"] = project_draft_id
6565
project_type = project_draft["projectType"]
66+
project_name = project_draft["name"]
6667
try:
6768
# Create a project object using appropriate class (project type).
6869
project = PROJECT_TYPE_CLASSES[project_type](project_draft)
@@ -71,37 +72,15 @@ def run_create_projects():
7172
project.calc_required_results()
7273
# Save project and its groups and tasks to Firebase and Postgres.
7374
project.save_project()
74-
newline = "\n"
75-
message = (
76-
f"### PROJECT CREATION SUCCESSFUL ###{newline}"
77-
f"Project Name: {project.name}{newline}"
78-
f"Project Id: {project.projectId}{newline}"
79-
f"Project Type: {PROJECT_TYPE_NAMES[project_type]}"
80-
f"{newline}"
81-
f"Make sure to activate the project "
82-
f"using the manager dashboard."
83-
f"{newline}"
84-
f"Happy Swiping. :)"
85-
)
86-
slack.send_slack_message(message)
87-
logger.info(message)
8875
except CustomError:
8976
ref = fb_db.reference(f"v2/projectDrafts/{project_draft_id}")
9077
ref.set({})
91-
newline = "\n"
92-
message = (
93-
f"### PROJECT CREATION FAILED ###{newline}"
94-
f'Project Name: {project_draft["name"]}{newline}'
95-
f"Project Id: {project_draft_id}{newline}"
96-
f"{newline}"
97-
f"Project draft is deleted.{newline}"
98-
f"Please check what went wrong."
99-
)
100-
slack.send_slack_message(message)
101-
slack.send_error(CustomError)
102-
logger.exception(f"{project_draft_id} " f"- project creation failed")
78+
send_slack_message("fail", project_name, project.projectId)
79+
logger.exception("Failed: Project Creation ({0}))".format(project_name))
10380
sentry.capture_exception()
104-
continue
81+
send_slack_message("success", project_name, project.projectId)
82+
logger.info("Success: Project Creation ({0})".format(project_name))
83+
continue
10584

10685

10786
@cli.command("firebase-to-postgres")
@@ -154,10 +133,9 @@ def run_user_management(email, manager) -> None:
154133
user_management.set_project_manager_rights(email)
155134
elif not manager:
156135
user_management.remove_project_manager_rights(email)
157-
except Exception as e:
158-
slack.send_error(e)
159-
sentry.capture_exception_sentry(e)
160-
logger.exception(e)
136+
except Exception:
137+
logger.exception()
138+
sentry.capture_exception()
161139

162140

163141
@cli.command("create-tutorial")
@@ -181,20 +159,15 @@ def run_create_tutorial(input_file) -> None:
181159
1: build_area_tutorial.create_tutorial,
182160
3: change_detection_tutorial.create_tutorial,
183161
}
184-
185162
project_types_tutorial[project_type](tutorial)
186-
except Exception as e:
187-
slack.send_error(e)
188-
sentry.capture_exception_sentry(e)
189-
logger.exception(e)
163+
except Exception:
164+
logger.exception()
165+
sentry.capture_exception()
190166

191167

192168
@click.command("archive")
193169
@click.option(
194-
"--project-id",
195-
"-i",
196-
help=("Archive project with giving project id"),
197-
type=str,
170+
"--project-id", "-i", help=("Archive project with giving project id"), type=str,
198171
)
199172
@click.option(
200173
"--project-ids",
@@ -237,4 +210,4 @@ def _run():
237210
sched.run_pending()
238211
time.sleep(1)
239212
else:
240-
_run()
213+
_run()

mapswipe_workers/mapswipe_workers/utils/sentry.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

mapswipe_workers/mapswipe_workers/utils/slack.py

Lines changed: 0 additions & 103 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Initialize slack client with values provided by the config file"""
2+
3+
import json
4+
import slack
5+
6+
from mapswipe_workers.definitions import CONFIG_PATH
7+
8+
9+
def send_slack_message(message_type: str, project_name: str, project_id: str = None):
10+
11+
with open(CONFIG_PATH) as config_file:
12+
config = json.load(config_file)
13+
slack_channel = config["slack"]["channel"]
14+
slack_client = slack.WebClient(token=config["slack"]["token"])
15+
16+
if message_type == "success":
17+
message = (
18+
"### PROJECT CREATION SUCCESSFUL ###\n"
19+
+ "Project Name: {0}\n".format(project_name)
20+
+ "Project Id: {0}\n\n".format(project_id)
21+
+ "Make sure to activate the project using the manager dashboard.\n"
22+
+ "Happy Swiping. :)"
23+
)
24+
elif message_type == "fail":
25+
message = (
26+
"### PROJECT CREATION FAILED ###\n"
27+
+ "Project Name: {0}\n".format(project_name)
28+
+ "Project draft is deleted."
29+
)
30+
else:
31+
# TODO: Raise an Exception
32+
pass
33+
34+
slack_client.chat_postMessage(channel=slack_channel, text=message)

0 commit comments

Comments
 (0)