Skip to content

Commit

Permalink
integrate collective.taskqueue2 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTango committed May 13, 2024
1 parent d8100b2 commit 5bb900e
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 56 deletions.
10 changes: 10 additions & 0 deletions base.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ parts =
i18ndude
omelette
robot
huey
plone-helper-scripts


develop = .
src/collective.taskqueue2


[instance]
Expand All @@ -26,10 +28,13 @@ user = admin:admin
http-address = 8080
environment-vars =
zope_i18n_compile_mo_files true
HUEY_CONSUMER True
HUEY_LOG_LEVEL WARNING
eggs =
Plone
Pillow
pdbpp
huey
Products.EasyNewsletter [test]

[vscode]
Expand Down Expand Up @@ -90,6 +95,10 @@ eggs = zest.releaser
recipe = zc.recipe.egg
eggs = i18ndude

[huey]
recipe = zc.recipe.egg
eggs = huey


[plone-helper-scripts]
recipe = zc.recipe.egg
Expand All @@ -99,6 +108,7 @@ eggs =
interpreter = zopepy
scripts =
zopepy
huey_consumer
plone-compile-resources


Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@
"html2text",
"email-validator>=1.1.2",
"six",
"huey",
"gevent",
"collective.taskqueue2",
# "huey",
# "gevent",
],
extras_require=dict(
test=[
Expand Down
32 changes: 1 addition & 31 deletions src/Products/EasyNewsletter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,10 @@
# -*- coding: utf-8 -*-
# avoid circular import
# from Products.EasyNewsletter import config # noqa
import logging
import threading

from huey.bin.huey_consumer import load_huey
from huey.consumer_options import ConsumerConfig
from zope.i18nmessageid import MessageFactory
import logging

log = logging.getLogger("Products.EasyNewsletter")

EasyNewsletterMessageFactory = MessageFactory("Products.EasyNewsletter")
_ = EasyNewsletterMessageFactory

consumer_options = {
"backoff": 1.15,
"check_worker_health": True,
"extra_locks": None,
"flush_locks": False,
"health_check_interval": 10,
"initial_delay": 0.1,
"max_delay": 10.0,
"periodic": True,
"scheduler_interval": 1,
"worker_type": "thread",
"workers": 1,
"logfile": "huey.log",
"verbose": False,
}

h = load_huey("Products.EasyNewsletter.queue.huey.huey_tasks.huey")

cconfig = ConsumerConfig(**consumer_options)
cconfig.validate()
cconfig.setup_logger()
cconsumer = h.create_consumer(**cconfig.values)

th = threading.Thread(target=cconsumer.run)
th.start()
1 change: 1 addition & 0 deletions src/Products/EasyNewsletter/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<include package=".browser" />
<include package=".content" />
<include package=".global_utilities" />
<include package=".portlets" />
<include package=".queue" />
<include package=".utils" />
Expand Down
3 changes: 2 additions & 1 deletion src/Products/EasyNewsletter/content/newsletter_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ class NewsletterIssue(Container):
context_property("content_aggregation_sources")

def get_newsletter(self):
return self.__parent__
return self.aq_inner.aq_parent
# return self.__parent__

# bbb to support ATCT way, needs to be removed in v5.x:
getNewsletter = get_newsletter
Expand Down
4 changes: 3 additions & 1 deletion src/Products/EasyNewsletter/issuedatafetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ def _render_output_html(self, preview=False):
with header+body+footer (raw html).
"""
output_tmpl_id = self.issue.output_template
issue_tmpl = self.issue.restrictedTraverse(str(output_tmpl_id))
issue_tmpl = self.issue.restrictedTraverse(str(output_tmpl_id), None)
if not issue_tmpl:
import pdb; pdb.set_trace() # NOQA: E702
output_html = issue_tmpl.render()
return output_html

Expand Down
6 changes: 3 additions & 3 deletions src/Products/EasyNewsletter/queue/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
/>
<include
package=".taskqueue"
zcml:condition="installed collective.taskqueue"
zcml:condition="installed collective.taskqueue2"
/>
<include
<!-- <include
package=".huey"
/>
/> -->
</configure>
2 changes: 0 additions & 2 deletions src/Products/EasyNewsletter/queue/huey/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@
class IssueQueue(object):
def start(self, context):
"""Queues issue for sendout through huey-mini task queue"""
print(context.UID())
task_res = send_newsletters(context.UID())
print(task_res)
return task_res
8 changes: 4 additions & 4 deletions src/Products/EasyNewsletter/queue/huey/huey_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from huey import SqliteHuey
from plone import api
from zope.site.hooks import getSite, setSite
from Products.EasyNewsletter import log

QUEUE_NAME = "Products.EasyNewsletter.queue"
Expand All @@ -18,8 +17,9 @@ def send_newsletters(uid):
""" resolve the context object by given uid and triggers
generation and sending of the newsletter issue.
"""
from zope.component import getGlobalSiteManager
gsm = getGlobalSiteManager()
import pdb; pdb.set_trace() # NOQA: E702
# from zope.component import getGlobalSiteManager
# gsm = getGlobalSiteManager()
# import pdb; pdb.set_trace() # NOQA: E702
context = api.content.get(UID=uid)
send_view = api.content.get_view(name="send-issue", context=context)
send_view.send()
1 change: 1 addition & 0 deletions src/Products/EasyNewsletter/queue/huey/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ def __call__(self):
log.info("generating and sending newsletter issue emails")
send_view = api.content.get_view(name="send-issue", context=self.context)
send_view.send()
log.info("sending done ;)")
6 changes: 3 additions & 3 deletions src/Products/EasyNewsletter/queue/taskqueue/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<utility factory=".handler.TCIssueQueue" />
<browser:page
class=".view.ProcessQueue"
for="Products.EasyNewsletter.interfaces.IENLIssue"
layer="collective.taskqueue.interfaces.ITaskQueueLayer"
for="Products.EasyNewsletter.content.newsletter_issue.INewsletterIssue"
name="enl_taskqueue_sendout"
permission="cmf.ReviewPortalContent"
permission="cmf.ModifyPortalContent"
/>
<!-- layer="Products.EasyNewsletter.interfaces.IProductsEasyNewsletterLayer" -->
</configure>
26 changes: 21 additions & 5 deletions src/Products/EasyNewsletter/queue/taskqueue/handler.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
# -*- coding: utf-8 -*-
from collective.taskqueue import taskqueue
from plone import api
from collective.taskqueue2.huey_tasks import schedule_browser_view
from Products.EasyNewsletter.queue.interfaces import IIssueQueue
from zope.interface import implementer


QUEUE_NAME = "Products.EasyNewsletter.queue"
VIEW_NAME = "enl_taskqueue_sendout"
ENL_VIEW_NAME = "enl_taskqueue_sendout"


@implementer(IIssueQueue)
class TCIssueQueue(object):
def start(self, context):
"""Queues issue for sendout through collective.taskqueue"""
jobid = taskqueue.add(
"/".join(context.getPhysicalPath() + (VIEW_NAME,)), queue=QUEUE_NAME
# import pdb; pdb.set_trace() # NOQA: E702
result = schedule_browser_view(
view_name=ENL_VIEW_NAME,
context_path="/".join(context.getPhysicalPath()),
site_path="/".join(api.portal.get().getPhysicalPath()),
username=api.user.get_current().getId(),
params=dict(
base=context.REQUEST.base,
layers=context.REQUEST.__provides__,
cookies=context.REQUEST.cookies,
form=dict(),
_plonebrowserlayer_=context.REQUEST._plonebrowserlayer_,
_plonetheme_=context.REQUEST._plonetheme_,
),
)
return jobid
# jobid = taskqueue.add(
# "/".join(context.getPhysicalPath() + (VIEW_NAME,)), queue=QUEUE_NAME
# )
return result
10 changes: 9 additions & 1 deletion src/Products/EasyNewsletter/queue/taskqueue/view.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
# -*- coding: utf-8 -*-
from plone import api
from plone.protect.interfaces import IDisableCSRFProtection
from Products.Five.browser import BrowserView
from zope.interface import alsoProvides

from Products.EasyNewsletter import log


class ProcessQueue(BrowserView):
def __call__(self):
alsoProvides(self.request, IDisableCSRFProtection)
self.context.send()
log.info("start sending:\n")
import pdb; pdb.set_trace() # NOQA: E702`c`
# send_view = api.content.get_view(name="send-issue", context=self.context)
send_view = self.context.restrictedTraverse("send-issue")
send_view.send()
log.info("sending done ;)\n")
3 changes: 2 additions & 1 deletion src/Products/EasyNewsletter/views/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
name="send-issue"
for="Products.EasyNewsletter.content.newsletter_issue.INewsletterIssue"
class=".newsletter_issue_send.NewsletterIssueSend"
permission="cmf.ModifyPortalContent"
permission="zope2.View"
/>
<!-- permission="cmf.ModifyPortalContent" -->

<browser:page
name="send-issue-form"
Expand Down
8 changes: 6 additions & 2 deletions src/Products/EasyNewsletter/views/newsletter_issue_send.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from Products.MailHost.interfaces import IMailHost
from zope.component import getMultiAdapter, getUtility, queryUtility, subscribers
from zope.component.hooks import getSite
from zope.interface import alsoProvides
from plone.protect.interfaces import IDisableCSRFProtection

from Products.EasyNewsletter import EasyNewsletterMessageFactory as _
from Products.EasyNewsletter.behaviors.plone_user_group_recipients import (
Expand Down Expand Up @@ -63,7 +65,8 @@ class Message(


class LocalLoader(object):
""" """
""" local emails loader for transform Plone image url's
"""

def __getitem__(self, uri):
image_file = None
Expand Down Expand Up @@ -167,7 +170,7 @@ def queue_issue_for_sendout(self):
'Executed queue issue for sendout in wrong review state!'
)
res = queue.start(self.context)
# log.info(f"queue runner results: {res()}")
log.info(f"queue runner results: {res}")

def _send_issue_prepare(self):
self.request["enlwf_guard"] = True
Expand All @@ -183,6 +186,7 @@ def send_issue_immediately(self):
never call this from UI - needs a way to protect
currently manager only
"""
alsoProvides(self.request, IDisableCSRFProtection)
self._send_issue_prepare()
self.send()

Expand Down
6 changes: 6 additions & 0 deletions test_plone60.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,9 @@ greenlet = 3.0.3
# Required by:
# Products.EasyNewsletter==6.0.0b5.dev0
gevent = 24.2.1

# Added by buildout at 2024-05-12 18:22:06.583901

# Required by:
# collective.taskqueue2==1.0a1
z3c.jbot = 2.0

0 comments on commit 5bb900e

Please sign in to comment.