0

If we use microservice architecture, ideally a single function definition should be at a single place.

So if I have a microservice containing the subscriber function.

And I want to publish an event to that subscriber from any microservice. How would I pass an instance of that function, when it is not even available in any other microservice.

https://python-rq.org/docs/

with Connection(redis.from_url(redis_url)):
    q = Queue(queue_name)
    task = q.enqueue(func, kwargs=params)

My API server is based of flask and my scripts are a separate microservice container, below is the architecture I'm following.

cron_scripts
    category1/
        - script_file.py // Enqueue Redis File
    category2/
flask_server
    - app
        - models
        - routes
        - tests
        - utils
            - queue // Defines my redis enqueue Wrapper
        - tasks
            - All Separate Task functions as per worker
    - init (Flask Init)

Are there any better design pattern resources, for the same? Please let me know.

Update -

This repo does make sense - https://github1s.com/munisisazade/redis-pub-sub/

Any reviews?

zion
  • 391
  • 5
  • 22
  • why does your microservice need to pass the subscriber function? why can you not just have a shared library that defines this function and your microservices import from this shared library? – gold_cy Jan 04 '22 at 12:11
  • And this shared(Copied) library has to be available in all microservices, isn't that against DRY principals?? Any design pattern resources? – zion Jan 04 '22 at 12:13
  • how is that against any principles? that's like using the `csv` module in all your microservices. this is the opposite of DRY since you create a library and declare the function _once_ and then just use it elsewhere. I think you misunderstand what DRY is. – gold_cy Jan 04 '22 at 12:15
  • I understand your point, So My api server is in flask and I'm trying to add a redis-enqueue from my cron script. I have added this in the question. – zion Jan 04 '22 at 12:25
  • And Please share any resources, That handles such a case. – zion Jan 04 '22 at 12:25
  • the structure of your project is not relevant. if you want to share code across microservices, a shared library is the way to go. – gold_cy Jan 04 '22 at 12:26
  • "ideally a single function definition should be at a single place" yes, like in a separate module/library if you want to share it. How exactly does this stop you from reusing it in different projects? – freakish Jan 04 '22 at 12:27
  • My knowledge of converting this into a wrapper in Python is pretty low. Any resources using RQ for creating a shared library will be really helpful. – zion Jan 04 '22 at 12:29

0 Answers0