@shared_task
def send_email_message(message):
if message:
try:
if message.subject:
message.subject = message.subject.strip(' \t\n\r')
message.send(fail_silently=True)
print 'email(s) sent'
except Exception as e:
raise e
@shared_task
def email_test_every_hour():
from django.conf import settings
from mail_templated import EmailMessage
from .models import Bid
bid = Bid.objects.filter(due_date__isnull=False).latest('id')
message = EmailMessage()
send_email_message.delay(message)
To just send, or even return code 1
Error:
[2017-09-21 17:02:16,204: ERROR/MainProcess] Task bpts.tasks.email_test_every_hour[693bbb35-fd97-416b-9b02-82ffee839f0b] raised unexpected: error(10054, 'An existing connection was forcibly closed by the remote host')
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\celery\app\trace.py", line 240, in trace_task
R = retval = fun(*args, **kwargs)
File "C:\Python27\lib\site-packages\celery\app\trace.py", line 438, in __protected_call__
return self.run(*args, **kwargs)
File "C:\inetpub\wwwroot\Django\BPTS\myapp\bpts\tasks.py", line 28, in email_test_every_hour
File "C:\Python27\lib\site-packages\celery\app\task.py", line 453, in delay
return self.apply_async(args, kwargs)
File "C:\Python27\lib\site-packages\celery\app\task.py", line 565, in apply_async
**dict(self._get_exec_options(), **options)
File "C:\Python27\lib\site-packages\celery\app\base.py", line 354, in send_task
reply_to=reply_to or self.oid, **options
File "C:\Python27\lib\site-packages\celery\app\amqp.py", line 310, in publish_task
**kwargs
File "C:\Python27\lib\site-packages\kombu\messaging.py", line 172, in publish
routing_key, mandatory, immediate, exchange, declare)
File "C:\Python27\lib\site-packages\kombu\connection.py", line 470, in _ensured
interval_max)
File "C:\Python27\lib\site-packages\kombu\connection.py", line 382, in ensure_connection
interval_start, interval_step, interval_max, callback)
File "C:\Python27\lib\site-packages\kombu\utils\__init__.py", line 246, in retry_over_time
return fun(*args, **kwargs)
File "C:\Python27\lib\site-packages\kombu\connection.py", line 250, in connect
return self.connection
File "C:\Python27\lib\site-packages\kombu\connection.py", line 756, in connection
self._connection = self._establish_connection()
File "C:\Python27\lib\site-packages\kombu\connection.py", line 711, in _establish_connection
conn = self.transport.establish_connection()
File "C:\Python27\lib\site-packages\kombu\transport\pyamqp.py", line 116, in establish_connection
conn = self.Connection(**opts)
File "C:\Python27\lib\site-packages\amqp\connection.py", line 180, in __init__
(10, 30), # tune
File "C:\Python27\lib\site-packages\amqp\abstract_channel.py", line 67, in wait
self.channel_id, allowed_methods, timeout)
File "C:\Python27\lib\site-packages\amqp\connection.py", line 241, in _wait_method
channel, method_sig, args, content = read_timeout(timeout)
File "C:\Python27\lib\site-packages\amqp\connection.py", line 330, in read_timeout
return self.method_reader.read_method()
File "C:\Python27\lib\site-packages\amqp\method_framing.py", line 189, in read_method
raise m
error: [Errno 10054] An existing connection was forcibly closed by the remote host
[2017-09-21 17:02:25,938: INFO/MainProcess] Received task: bpts.tasks.email_test_every_hour[bbb2fe0e-3a6a-452d-910b-60f76470b716]
When I manually run
message = EmailMessage()
send_email_message.delay(message)
send_email_message.delay(message) also works when called via a post-event in code.
I get the expected result. However, when I run
email_test_every_hour.delay()
manually or wait for it to be triggered via beat it does not work.
We no longer support Celery 3.x.