(Migrated) try-excepting an imported hot-deployed exception

(This message has been automatically imported from the retired mailing list)

Hello, I’m referring to an older discussion we had about hot-deploying a =
module which is imported by other hot-deployed code.

In my case the hot deployed module contains a base class for services (wh=
ich works OK)
and an error class which subclasses Exception (that’s where problems begi=
n).

class MyError(Exception):
pass

class BaseService(Service):

at the end of the file

mod =3D imp.new_module(‘base_service’)
for name in (‘BaseService’, ‘MyError’):
mod.dict[name] =3D locals()[name]
sys.modules[‘base_service’] =3D mod

Subclasses of BaseService which are deployed in their own module do this:=

from base_service import BaseService, MyError

class SpecificService(BaseService):

def handle(self):
try:
self.some_method_of_base_service() # raises MyError
except MyError:
no_problem()
except Exception:
panic()

it works fine 99 times out of 100, but sometimes MyError slips through th=
e first except block.

I always deploy the base_module as first file, then I wait for some secon=
d and then I deploy the other modules.

Any ideas?

On 04/29/15 16:38, Christopher Arndt wrote:

/Actually, there is a hackish way to do this: you can construct a module=
/>/dynamically in the Python file, which you hot-deploy. If tested this =
/>/once in Zato 1.1 and at least there it worked, but it’s hack. />//>/It=
goes like this: />//>/import imp />/import sys />//>/def testfunc(a, b):=
/>/return a ** b />//>/# Create the module object />/mod =3D imp.new_mod=
ule(‘mydynlib’) />//>/# populate module namespace />/for name in (‘testfu=
nc’, …): />/mod.dict[name] =3D locals()[name] />//>/# make it avail=
able globally for import />/sys.modules[‘mydynlib’] =3D mod />//>//>/Then=
in your service module you can do: />//>/from mydynlib import testfunc /=
//>//>/Chris/

On 18/12/15 14:52, Andrea Peter wrote:

it works fine 99 times out of 100, but sometimes MyError slips through the first except block.

I always deploy the base_module as first file, then I wait for some second and then I deploy the other modules.

Any ideas?

Hi Andrea,

if this is a base module which, if I picture it correctly, won’t change
very often, can you deploy it using service-sources.txt before things
from hot-deploy get picked up?

https://zato.io/docs/admin/guide/installing-services.html

On 22/12/15 13:19, Andrea Peter wrote:

I’m not very familiar with the source code of Zato could you (or
anybody) point me to where the hot-deployment is done?

Check these places out:

  • ParallelServer.maybe_on_first_worker
  • ServiceStore.import_services_from_anywhere
  • WorkerStore.on_broker_msg_HOT_DEPLOY_CREATE

https://github.com/zatosource/zato/blob/master/code/zato-server/src/zato/server/base/parallel.py#L236

https://github.com/zatosource/zato/blob/master/code/zato-server/src/zato/server/service/store.py#L110

https://github.com/zatosource/zato/blob/master/code/zato-server/src/zato/server/base/worker.py#L1203