Hello,
The problem subject is ir.cron and the symptoms are described as below:
When i login as Administrator and run the scheduler manually, it runs as expected, but when the scheduler runs on nextcall time, it runs but it doesn't create mail.message and mail.mail (which are the expected result)
Upon further testing, for example, i set the nextcall value to the next minute from current time (to simulate the process upon nextcall), it runs without error. When i change some fields inside the cron, such as toggling active field, it gives me error which state that the scheduler is currently running. This error is happening until i restart Odoo service and then i can change scheduler's field value.
Upon another testing, i find that lines of code (stated below in bold) is causing the problem. When i comment it out, the scheduler works both manually and automatically and i received expected results (get a new discuss message, and an email is sent)
Below is the function that is being run on the scheduler:
@api.model
def send_notification(self, record_id):
maintenanceEvt = self.env['env.maintenances'].search([('id', '=', record_id)], limit=1)
notify_date = datetime.strptime(self._get_nextcall(maintenanceEvt['notify_on'], maintenanceEvt['planned_date']), "%Y-%m-%d %H:%M:%S")
day_interval = (datetime.now() - datetime.strptime(maintenanceEvt.planned_date, '%Y-%m-%d %H:%M:%S')).days
########Below are the culprits######
# maintenanceEvt.days_late = day_interval
# if day_interval > 0:
# maintenanceEvt.state = 'Late'
# elif day_interval < 0:
# maintenanceEvt.state = 'Scheduled'
# else:
# maintenanceEvt.state = 'Ongoing'
#########End of culprits############
partners = [(4, maintenanceEvt['maintainer_id']['partner_id']['id'])]
partners.append((4, maintenanceEvt['owner_id']['partner_id']['id']))
for user in maintenanceEvt['notified_ids']:
partners.append(((4, user['partner_id']['id'])))
if maintenanceEvt['breakdown']:
if day_interval == 0:
#I create compact variable here, containing strings of HTML codes
#I create html variable here, containing strings of HTML codes
#I create plain variable here, containing strings of non-HTML text
self._create_message(maintenanceEvt, "Breakdown Occurred", compact, partners)
self._send_notification_mail("Breakdown Occurred", html, plain)
elif day_interval > 0:
#I create compact variable here, containing strings of HTML codes
#I create html variable here, containing strings of HTML codes
#I create plain variable here, containing strings of non-HTML text
self._create_message(maintenanceEvt, "Breakdown Still Happening", compact, partners)
self._send_notification_mail("Breakdown Still Happening", html, plain)
else:
event_date = datetime.strptime(maintenanceEvt['planned_date'], "%Y-%m-%d %H:%M:%S")
if datetime.now() >= (event_date - timedelta(days=maintenanceEvt['notify_on'])):
if day_interval < 0:
#I create compact variable here, containing strings of HTML codes
#I create html variable here, containing strings of HTML codes
#I create plain variable here, containing strings of non-HTML text
self._create_message(maintenanceEvt, "Maintenance Event Scheduled", compact, partners)
self._send_notification_mail("Maintenance Event Scheduled", html, plain)
elif day_interval == 0:
#I create compact variable here, containing strings of HTML codes
#I create html variable here, containing strings of HTML codes
#I create plain variable here, containing strings of non-HTML text
self._create_message(maintenanceEvt, "Maintenance Due Today", compact, partners)
self._send_notification_mail("Maintenance Due Today", html, plain)
elif day_interval > 0:
#I create compact variable here, containing strings of HTML codes
#I create html variable here, containing strings of HTML codes
#I create plain variable here, containing strings of non-HTML text
self._create_message(maintenanceEvt, "A Maintenance is Late", compact, partners)
self._send_notification_mail("A Maintenance is Late", html, plain)
Note:
1. _get_nextcall(n_days, datetime):
Get the datetime n_days days before the datetime
2. _create_message(record_id, title, message, partner_id):
Create message (mail.message) for partner_id with subject title and body message and is related to res_id record_id
3. _send_notification_mail(title, bodyHtml, bodyPlain)
Create email (mail.mail) that has title title, body_html bodyHtml and body Plain. Recipient are hardcoded since we don't need dynamic recipient.
Anyone who might have any idea or suggestion for me to try, please help