Add sendTime control for preregister email

This commit is contained in:
sas.fajri
2026-05-08 14:59:50 +07:00
parent 98ad7346fa
commit f9d3e0674b
5 changed files with 151 additions and 14 deletions

View File

@@ -292,6 +292,67 @@ class SendEmailPreregister extends MY_Controller
{
try {
$data_send_patients = [];
$sendTime = '20:00:00';
$sql_notif_config = "SELECT
Email_ConfigNofificationTemplate,
Email_ConfigNofificationSender,
Email_ConfigNofificationSendTime
FROM email_config_nofification
WHERE Email_ConfigNofificationIsActive = 'Y'
ORDER BY Email_ConfigNofificationID DESC
LIMIT 1";
$que_notif_config = $this->db->query($sql_notif_config);
if (!$que_notif_config || $que_notif_config->num_rows() == 0) {
throw new Exception('[Error] failed get notif email template config');
}
$notifConfig = $que_notif_config->row_array();
$templateBody = $notifConfig['Email_ConfigNofificationTemplate'];
if (!empty($notifConfig['Email_ConfigNofificationSendTime'])) {
$sendTime = $notifConfig['Email_ConfigNofificationSendTime'];
}
$nowTime = date('H:i:s');
if ($nowTime < $sendTime) {
$this->sys_ok([
"message" => "skip queue, current time before send time",
"current_time" => $nowTime,
"send_time" => $sendTime,
"total_email_queued" => 0
]);
return;
}
$targetDate = date('Y-m-d', strtotime('+1 day'));
$sql_mcu_sync = "SELECT Mgm_McuID
FROM mgm_mcu
WHERE Mgm_McuIsActive = 'Y'";
$que_mcu_sync = $this->db->query($sql_mcu_sync);
if (!$que_mcu_sync) {
throw new Exception('[Error] failed get mcu list for dashboard sync');
}
$mcuRows = $que_mcu_sync->result_array();
foreach ($mcuRows as $mcuRow) {
$mcuIDSync = isset($mcuRow['Mgm_McuID']) ? intval($mcuRow['Mgm_McuID']) : 0;
if ($mcuIDSync <= 0) {
continue;
}
$que_sync = $this->db->query(
"CALL cpone.sp_refresh_mcu_participant_daily_by_mcu_date(?, ?)",
[$mcuIDSync, $targetDate]
);
if (!$que_sync) {
throw new Exception('[Error] failed sync dashboard participant daily');
}
if (isset($this->db->conn_id) && $this->db->conn_id instanceof mysqli) {
while ($this->db->conn_id->more_results() && $this->db->conn_id->next_result()) {
$dummyResult = $this->db->conn_id->store_result();
if ($dummyResult instanceof mysqli_result) {
$dummyResult->free();
}
}
}
}
$sql_patients = "SELECT
pp.Mcu_PreregisterPatientsID AS preregister_id,
@@ -325,20 +386,6 @@ class SendEmailPreregister extends MY_Controller
}
$data_send_patients = $que_patients->result_array();
$sql_notif_config = "SELECT
Email_ConfigNofificationTemplate,
Email_ConfigNofificationSender
FROM email_config_nofification
WHERE Email_ConfigNofificationIsActive = 'Y'
ORDER BY Email_ConfigNofificationID DESC
LIMIT 1";
$que_notif_config = $this->db->query($sql_notif_config);
if (!$que_notif_config || $que_notif_config->num_rows() == 0) {
throw new Exception('[Error] failed get notif email template config');
}
$notifConfig = $que_notif_config->row_array();
$templateBody = $notifConfig['Email_ConfigNofificationTemplate'];
$this->db->trans_begin();
foreach ($data_send_patients as $patient) {
$sql_config_email = "SELECT * FROM m_emailconfig WHERE M_EmailConfigType = 'R'";

View File

@@ -438,6 +438,28 @@ class Email_gateway extends MY_Controller
$max_per_batch = 20;
$this->log($log_type, "Start Process $log_type Email");
$send_time = '20:00:00';
$sql_cfg = "SELECT Email_ConfigNofificationSendTime
FROM email_config_nofification
WHERE Email_ConfigNofificationIsActive = 'Y'
ORDER BY Email_ConfigNofificationID DESC
LIMIT 1";
$que_cfg = $this->db->query($sql_cfg);
if ($que_cfg && $que_cfg->num_rows() > 0) {
$cfg = $que_cfg->row_array();
if (!empty($cfg['Email_ConfigNofificationSendTime'])) {
$send_time = $cfg['Email_ConfigNofificationSendTime'];
}
}
$send_time_with_delay = date("H:i:s", strtotime($send_time . " +10 minutes"));
$now_time = date("H:i:s");
if ($now_time < $send_time_with_delay) {
$this->log($log_type, "Skip process, current time $now_time < send time+delay $send_time_with_delay");
exit;
}
$tmp_mark_file = "/xtmp/{$log_type}-file.json";
$this->load->library("Gmail");
$is_running = false;

View File

@@ -0,0 +1,62 @@
# Setup Cron 5 Menit - Preregister Email Gateway
Dokumen ini untuk menjalankan worker pengiriman email preregister secara terjadwal setiap 5 menit.
## 1) Tambahkan cron job
Edit crontab user yang menjalankan service web/app:
```bash
crontab -e
```
Tambahkan baris ini:
```cron
*/5 * * * * /usr/bin/php /home/one/project/one/BE_CPONE/index.php tools/Email_gateway/preregister >> /xtmp/cron-preregister-email.log 2>&1
```
Catatan:
- Sesuaikan path PHP jika berbeda (`which php`).
- Sesuaikan path repo jika berbeda dari `/home/one/project/one/BE_CPONE`.
## 2) Cara kerja dengan config jam kirim
Worker `tools/Email_gateway/preregister` akan:
- Baca `Email_ConfigNofificationSendTime` dari tabel `cpone.email_config_nofification` (record aktif terbaru).
- Jika waktu sekarang belum mencapai jam tersebut, worker langsung `exit`.
- Jika sudah masuk jam kirim, worker proses queue `preregister_email_outbox`.
Default `send time` saat ini: `20:00:00`.
## 3) Verifikasi cron aktif
Cek isi crontab:
```bash
crontab -l
```
Pantau log cron:
```bash
tail -f /xtmp/cron-preregister-email.log
```
Pantau log worker:
```bash
tail -f /xtmp/x-email-preregister.log
```
## 4) Ubah jam kirim via DB
Contoh ubah jam kirim jadi 20:00:00:
```sql
UPDATE cpone.email_config_nofification
SET Email_ConfigNofificationSendTime = '20:00:00'
WHERE Email_ConfigNofificationIsActive = 'Y';
```
Disarankan hanya 1 row aktif untuk konfigurasi ini.

View File

@@ -5,6 +5,7 @@ CREATE TABLE IF NOT EXISTS `cpone`.`email_config_nofification` (
`Email_ConfigNofificationUsername` varchar(100) NOT NULL,
`Email_ConfigNofificationPassword` varchar(100) NOT NULL,
`Email_ConfigNofificationServer` varchar(100) NOT NULL,
`Email_ConfigNofificationSendTime` time NOT NULL DEFAULT '20:00:00',
`Email_ConfigNofificationCreated` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Email_ConfigNofificationCreatedUserID` int NOT NULL DEFAULT '0',
`Email_ConfigNofificationLastUpdated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
@@ -20,6 +21,7 @@ INSERT INTO `cpone`.`email_config_nofification` (
`Email_ConfigNofificationUsername`,
`Email_ConfigNofificationPassword`,
`Email_ConfigNofificationServer`,
`Email_ConfigNofificationSendTime`,
`Email_ConfigNofificationCreatedUserID`,
`Email_ConfigNofificationLastUpdatedUserID`,
`Email_ConfigNofificationIsActive`
@@ -30,6 +32,7 @@ SELECT
'',
'',
'',
'20:00:00',
0,
0,
'Y'

View File

@@ -0,0 +1,3 @@
ALTER TABLE cpone.email_config_nofification
ADD COLUMN Email_ConfigNofificationSendTime TIME NOT NULL DEFAULT '20:00:00'
AFTER Email_ConfigNofificationServer;