Repository navigation
Expand file tree
/
Copy pathworker.py
More file actions
117 lines (102 loc) · 4.91 KB
/
Copy pathworker.py
File metadata and controls
117 lines (102 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import os, logging, tempfile
from crawler.aggregator import aggregate_jobs
from matcher.engine import rank_jobs
from db.master import init_db, is_new_job, save_job
from emailer.sendgrid_client import send_digest, send_no_matches_email
from config import Config
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
def run():
try:
logger.info("Starting enhanced job alert process (Canada, US & China)...")
# Initialize database
init_db()
logger.info("Database initialized")
# Aggregate jobs from all sources
jobs = aggregate_jobs()
logger.info(f"Found {len(jobs)} total jobs from all sources")
if not jobs:
logger.warning("No jobs found from any source")
# Send no matches email with log
send_no_matches_email(Config.TARGET_EMAIL, "No jobs found from any source", None)
return
# Rank and filter jobs
matches = rank_jobs(jobs)
logger.info(f"Ranked {len(matches)} jobs")
# Process new matches with enhanced filtering
new_matches = []
for job in matches:
try:
sig = f"{job['company']}|{job['title']}|{job.get('location','')}|{job['url']}"
meets_score_threshold = job['match_score'] >= Config.MIN_MATCH_SCORE
has_relocation = job['relocation_detected']
is_new = is_new_job(sig)
if is_new and has_relocation and meets_score_threshold:
save_job(sig, job)
new_matches.append(job)
logger.info(
f"New match: {job['title']} @ {job['company']} "
f"(Score: {job['match_score']}, Confidence: {job['relocation_confidence']})"
)
elif is_new and has_relocation:
logger.info(
f"Job below threshold: {job['title']} @ {job['company']} "
f"(Score: {job['match_score']}, Threshold: {Config.MIN_MATCH_SCORE})"
)
elif is_new and meets_score_threshold:
logger.info(
f"Job without relocation: {job['title']} @ {job['company']} "
f"(Score: {job['match_score']})"
)
except Exception as e:
logger.error(f"Error processing job {job.get('title', 'Unknown')}: {e}")
continue
# Persist logs to a temporary file for emailing
log_file_path = None
try:
tmp_dir = tempfile.gettempdir()
log_file_path = os.path.join(tmp_dir, "job_alert_run.log")
with open(log_file_path, "w", encoding="utf-8") as lf:
lf.write("Job Alert Run Log (Canada, US & China)\n")
lf.write("="*60 + "\n\n")
lf.write(f"Total jobs aggregated: {len(jobs)}\n")
lf.write(f"Candidates after ranking: {len(matches)}\n")
lf.write(f"New matches to send: {len(new_matches)}\n\n")
lf.write("Top 10 ranked jobs:\n")
for j in matches[:10]:
lf.write(
f"- {j.get('title')} @ {j.get('company')} | "
f"{j.get('location','')} | Score: {j.get('match_score')} | "
f"Relocation: {j.get('relocation_confidence','unknown')} | {j.get('url')}\n"
)
except Exception as e:
logger.warning(f"Failed to write log file: {e}")
# Send email if new matches found
if new_matches:
logger.info(f"Sending email with {len(new_matches)} new matches")
try:
send_digest(Config.TARGET_EMAIL, new_matches, log_file_path)
logger.info("Email sent successfully")
except Exception as e:
logger.error(f"Critical error sending digest, will send report instead: {e}")
reason = f"Digest send failed: {e}. See attached logs."
try:
send_no_matches_email(Config.TARGET_EMAIL, reason, log_file_path)
except Exception as inner:
logger.error(f"Also failed to send report email: {inner}")
else:
logger.info("No new matches to send")
# Send no matches email with detailed log
reason = f"Found {len(jobs)} jobs but none met criteria (score >= {Config.MIN_MATCH_SCORE} + relocation)"
try:
send_no_matches_email(Config.TARGET_EMAIL, reason, log_file_path)
except Exception as e:
logger.error(f"Failed to send no-matches report: {e}")
except Exception as e:
logger.error(f"Critical error in job alert process: {e}")
# Do not re-raise to allow workflow to complete
if __name__ == "__main__":
run()