Commit 2ca29cce authored by Łukasz Nowak's avatar Łukasz Nowak

Return only previously calculated computer information.

Respond immediately with no data in case of no cached information and register
cache filling.

In case if data are available, but older then 5 minutes, request its
recalculation.
parent 94921f8d
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Cache Factory" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_count</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
<item>
<key> <string>_mt_index</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAM=</string> </persistent>
</value>
</item>
<item>
<key> <string>_tree</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAQ=</string> </persistent>
</value>
</item>
<item>
<key> <string>cache_duration</string> </key>
<value> <int>86400</int> </value>
</item>
<item>
<key> <string>description</string> </key>
<value> <string>Storage of _getComputerInformation XML marshalled data.\n
\n
Shall be flushed while accessing from time to time.</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>computer_information_cache_factory</string> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Cache Factory</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>Computer Information Cache</string> </value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="Length" module="BTrees.Length"/>
</pickle>
<pickle> <int>0</int> </pickle>
</record>
<record id="3" aka="AAAAAAAAAAM=">
<pickle>
<global name="OOBTree" module="BTrees.OOBTree"/>
</pickle>
<pickle>
<none/>
</pickle>
</record>
<record id="4" aka="AAAAAAAAAAQ=">
<pickle>
<global name="OOBTree" module="BTrees.OOBTree"/>
</pickle>
<pickle>
<none/>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Distributed Ram Cache" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>categories</string> </key>
<value>
<tuple>
<string>specialise/portal_memcached/persistent_memcached_plugin</string>
</tuple>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>persistent_cache_plugin</string> </value>
</item>
<item>
<key> <string>int_index</string> </key>
<value> <int>0</int> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Distributed Ram Cache</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>Persistent Distributed Ram Cache</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
466 467
\ No newline at end of file \ No newline at end of file
...@@ -28,6 +28,8 @@ person_module/test_vifib_user_admin ...@@ -28,6 +28,8 @@ person_module/test_vifib_user_admin
person_module/test_vifib_user_admin/** person_module/test_vifib_user_admin/**
person_module/test_vifib_user_developer person_module/test_vifib_user_developer
person_module/test_vifib_user_developer/** person_module/test_vifib_user_developer/**
portal_caches/computer_information_cache_factory
portal_caches/computer_information_cache_factory/persistent_cache_plugin
portal_caches/slap_cache_factory portal_caches/slap_cache_factory
portal_caches/slap_cache_factory/persistent_cache_plugin portal_caches/slap_cache_factory/persistent_cache_plugin
service_module/computer_registration service_module/computer_registration
......
...@@ -39,7 +39,9 @@ from Products.ERP5Type.Tool.BaseTool import BaseTool ...@@ -39,7 +39,9 @@ from Products.ERP5Type.Tool.BaseTool import BaseTool
from Products.ZSQLCatalog.SQLCatalog import Query, ComplexQuery from Products.ZSQLCatalog.SQLCatalog import Query, ComplexQuery
from Products.ERP5Type import Permissions from Products.ERP5Type import Permissions
from Products.ERP5Type.Cache import CachingMethod from Products.ERP5Type.Cache import CachingMethod
from Products.ERP5Type.Cache import DEFAULT_CACHE_SCOPE
from lxml import etree from lxml import etree
import time
try: try:
from slapos.slap.slap import Computer from slapos.slap.slap import Computer
from slapos.slap.slap import ComputerPartition as SlapComputerPartition from slapos.slap.slap import ComputerPartition as SlapComputerPartition
...@@ -135,6 +137,44 @@ class SlapTool(BaseTool): ...@@ -135,6 +137,44 @@ class SlapTool(BaseTool):
# Public GET methods # Public GET methods
#################################################### ####################################################
def _getCachePlugin(self):
return self.getPortalObject().portal_caches\
.getRamCacheRoot().get('computer_information_cache_factory')\
.getCachePluginList()[0]
def _fillComputerInformationCache(self, computer_id, user, full):
user_document = self.getPortalObject().portal_catalog.getResultValue(
reference=user, portal_type=['Person', 'Computer', 'Software Instance'])
user_type = user_document.getPortalType()
self.REQUEST.response.setHeader('Content-Type', 'text/xml')
slap_computer = Computer(computer_id)
parent_uid = self._getComputerUidByReference(computer_id)
slap_computer._computer_partition_list = []
slap_computer._software_release_list = \
self._getSoftwareReleaseValueListForComputer(computer_id, full=full)
for computer_partition in self.getPortalObject().portal_catalog(
parent_uid=parent_uid,
validation_state="validated",
portal_type="Computer Partition"):
slap_computer._computer_partition_list.append(
self._getSlapPartitionByPackingList(computer_partition.getObject()))
self._getCachePlugin().set(user, DEFAULT_CACHE_SCOPE,
dict (
time=time.time(),
data=xml_marshaller.xml_marshaller.dumps(slap_computer),
),
cache_duration=self.getPortalObject().portal_caches\
.getRamCacheRoot().get('computer_information_cache_factory'\
).cache_duration
)
def _activateFillComputerInformationCache(self, computer_id, user, full):
tag = 'computer_information_cache_fill_%s' % user
if self.getPortalObject().portal_activities.countMessageWithTag(tag) == 0:
self.activate(activity='SQLQueue', tag=tag)._fillComputerInformationCache(
computer_id, user, full)
def _getComputerInformation(self, computer_id, user, full): def _getComputerInformation(self, computer_id, user, full):
user_document = self.getPortalObject().portal_catalog.getResultValue( user_document = self.getPortalObject().portal_catalog.getResultValue(
reference=user, portal_type=['Person', 'Computer', 'Software Instance']) reference=user, portal_type=['Person', 'Computer', 'Software Instance'])
...@@ -145,6 +185,21 @@ class SlapTool(BaseTool): ...@@ -145,6 +185,21 @@ class SlapTool(BaseTool):
slap_computer._computer_partition_list = [] slap_computer._computer_partition_list = []
if user_type in ('Computer', 'Person'): if user_type in ('Computer', 'Person'):
cache_plugin = self._getCachePlugin()
try:
entry = cache_plugin.get(user, DEFAULT_CACHE_SCOPE)
except KeyError:
entry = None
if entry is not None and type(entry.getValue()) == type({}):
result = entry.getValue()['data']
if time.time() - entry.getValue()['time'] > 60 * 5:
# entry was stored 5 minutes ago, ask for recalculation
self._activateFillComputerInformationCache(computer_id, user, full)
return result
else:
self._activateFillComputerInformationCache(computer_id, user, full)
self.REQUEST.response.setStatus(503)
return self.REQUEST.response
slap_computer._software_release_list = \ slap_computer._software_release_list = \
self._getSoftwareReleaseValueListForComputer(computer_id, full=full) self._getSoftwareReleaseValueListForComputer(computer_id, full=full)
else: else:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment