optimize the background jobs
This commit is contained in:
@@ -94,6 +94,29 @@ namespace BoredCareers.Services.DatabaseService {
|
||||
return applications.ToArray();
|
||||
}
|
||||
|
||||
public async Task<string[]> GetApplicationResponseEmailFromJobListing(int JobListingID) {
|
||||
List<string> emailadds = new List<string>();
|
||||
using (MySqlConnection connection = GetConnection()) {
|
||||
await connection.OpenAsync();
|
||||
string command = @"
|
||||
SELECT ResponseEmail
|
||||
FROM JobApplication
|
||||
WHERE JobListingID = @JobListingID
|
||||
";
|
||||
|
||||
MySqlCommand cmd = new MySqlCommand(command, connection);
|
||||
cmd.Parameters.AddWithValue("@JobListingID", JobListingID);
|
||||
|
||||
using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
|
||||
while (await reader.ReadAsync()) {
|
||||
string _responseemail = reader.GetString("ResponseEmail");
|
||||
emailadds.Add(_responseemail);
|
||||
}
|
||||
}
|
||||
}
|
||||
return emailadds.ToArray();
|
||||
}
|
||||
|
||||
public async Task<Application?> GetApplication(int ApplicationID) {
|
||||
Application? application = null;
|
||||
using (MySqlConnection connection = GetConnection()) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using BoredCareers.Entities;
|
||||
using BoredCareers.Services.TimerService;
|
||||
using MySql.Data.MySqlClient;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
@@ -177,12 +178,12 @@ namespace BoredCareers.Services.DatabaseService {
|
||||
return joblisting;
|
||||
}
|
||||
|
||||
public async Task<JobListing[]> GetJobListingsPastExipre() {
|
||||
List<JobListing> joblistings = new List<JobListing>();
|
||||
public async Task<JobListingDTO[]> GetJobListingsPastExipre() {
|
||||
List<JobListingDTO> joblistings = new List<JobListingDTO>();
|
||||
using (MySqlConnection connection = GetConnection()) {
|
||||
await connection.OpenAsync();
|
||||
string command = @"
|
||||
SELECT *
|
||||
SELECT ID, CompanyID
|
||||
FROM JobListing
|
||||
WHERE IsDeleted = FALSE
|
||||
AND CreatedTime < NOW() - INTERVAL 1 MONTH;
|
||||
@@ -193,38 +194,9 @@ namespace BoredCareers.Services.DatabaseService {
|
||||
while (await reader.ReadAsync()) {
|
||||
int _id = reader.GetInt32("ID");
|
||||
int _companyid = reader.GetInt32("CompanyID");
|
||||
string _title = reader.GetString("Title");
|
||||
string _postalcode = reader.GetString("PostalCode");
|
||||
string _country = reader.GetString("Country");
|
||||
string _state = reader.GetString("StateOrRegion");
|
||||
string _city = reader.GetString("City");
|
||||
int _salarymin = reader.GetInt32("SalaryMin");
|
||||
int _salarymax = reader.GetInt32("SalaryMax");
|
||||
string _jobtype = reader.GetString("JobType");
|
||||
bool _remote = reader.GetBoolean("Remote");
|
||||
string _description = reader.GetString("Description");
|
||||
JobListingSkill[] _skills = await GetJobListingSkills(_id);
|
||||
DateTime _createtime = reader.GetDateTime("CreatedTime");
|
||||
DateTime _modifiedtime = reader.GetDateTime("ModifiedTime");
|
||||
bool _isdeleted = reader.GetBoolean("IsDeleted");
|
||||
|
||||
joblistings.Add(new JobListing() {
|
||||
ID = _id,
|
||||
CompanyID = _companyid,
|
||||
Title = _title,
|
||||
PostalCode = _postalcode,
|
||||
Country = _country,
|
||||
StateOrRegion = _state,
|
||||
City = _city,
|
||||
SalaryMin = _salarymin,
|
||||
SalaryMax = _salarymax,
|
||||
JobType = _jobtype,
|
||||
Remote = _remote,
|
||||
Description = _description,
|
||||
Skills = _skills,
|
||||
CreatedTime = _createtime,
|
||||
ModifiedTime = _modifiedtime,
|
||||
IsDeleted = _isdeleted
|
||||
joblistings.Add(new JobListingDTO() {
|
||||
JobListingID = _id,
|
||||
CompanyID = _companyid
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,26 +14,45 @@ namespace BoredCareers.Services.TimerService {
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
|
||||
while (!stoppingToken.IsCancellationRequested) {
|
||||
try {
|
||||
JobListing[] deletedJobListings = await _db.GetJobListingsPastExipre();
|
||||
await _db.DeleteJobListingsPastExipre();
|
||||
foreach (JobListing listing in deletedJobListings) {
|
||||
Application[] apps = await _db.GetApplicationsFromJobListing(Convert.ToInt32(listing.ID));
|
||||
foreach (Application app in apps) {
|
||||
_em.Send(app.ResponseEmail, EmailService.JobAutoClosedSubject, EmailService.JobAutoClosedEmail);
|
||||
}
|
||||
// Get listing's past expire
|
||||
JobListingDTO[] deletedJobListings = await _db.GetJobListingsPastExipre();
|
||||
|
||||
// Mark the company for the auto closed listing
|
||||
Company? comp = await _db.GetCompany(listing.CompanyID);
|
||||
// Group them by CompanyID, ListingCount
|
||||
Dictionary<int, int> listingsByCompany = deletedJobListings
|
||||
.GroupBy(l => l.CompanyID)
|
||||
.ToDictionary(g => g.Key, g => g.Count());
|
||||
|
||||
// Update each company's rating
|
||||
foreach (KeyValuePair<int, int> kvp in listingsByCompany) {
|
||||
Company? comp = await _db.GetCompany(kvp.Key);
|
||||
if (comp != null) {
|
||||
comp.JobsAutoClosed++;
|
||||
comp.JobsAutoClosed += kvp.Value;
|
||||
await _db.SetCompany(comp);
|
||||
}
|
||||
}
|
||||
|
||||
// Get each listing
|
||||
foreach (JobListingDTO listing in deletedJobListings) {
|
||||
// Get each Person
|
||||
string[] emails = await _db.GetApplicationResponseEmailFromJobListing(listing.JobListingID);
|
||||
foreach (string email in emails) {
|
||||
// Send Notify Email
|
||||
_em.Send(email, EmailService.JobAutoClosedSubject, EmailService.JobAutoClosedEmail);
|
||||
}
|
||||
}
|
||||
|
||||
// Delete Listing's past expire
|
||||
await _db.DeleteJobListingsPastExipre();
|
||||
} catch (Exception e) {
|
||||
Console.WriteLine($"Error: {e.Message}");
|
||||
}
|
||||
await Task.Delay(TimeSpan.FromHours(24), stoppingToken);
|
||||
await Task.Delay(TimeSpan.FromHours(2), stoppingToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class JobListingDTO {
|
||||
public int JobListingID { get; set; }
|
||||
public int CompanyID { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user