Git Product home page Git Product logo

python-microservices's People

Contributors

ahmedsadman avatar antoniopapa avatar scalablescripts avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-microservices's Issues

Add README

This is an amazing repo! there is also a youtube video about it.

but it would be useful to have a README to explain setup procedure and add some info

Main (flask) backend app does not return likes number in response to GET /api/products

I think there is an issue with the Main (flask) app, regarding what /api/products index returns (see this line of code). The response does not contain likes field.

Consequently, Main.tsx just won't show number of likes for a Product (since it won't be present in the fetch response, see this line).

It could work as shown in your (really awesome! ๐Ÿ‘ ) youtube video only if you changed the destination port there in fetch call to 8000, hence requesting product list from the Admin django app instead. But since this breaks the 'admin vs main' separation logic, I assume it's really the Main flask backend app that should be fixed here (as Main.tsx still should communicate with the Main backend, not Admin backend).

Hence reporting this one here.

Flask database ProductsUser UniqueConstraint configuration not work.

in flask main.py

class ProductsUser(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer)
    product_id = db.Column(db.Integer)
    # TODO: not working actually
    UniqueConstraint('user_id', 'product_id', name='user_product_unique')

where

UniqueConstraint('user_id', 'product_id', name='user_product_unique')

user_id and product_id should be instance instead of string, modified code to :

UniqueConstraint(user_id, product_id, name='user_product_unique')

then migrate database again that fixed this issue.

RuntimeError: Working outside of application context.

I am having an issue with the Flask application context. I am not able to successfully run either one of the methods (however, the logs below are for the update() functionality)

queue_1    | Started Consuming...
queue_1    | Received in app
queue_1    | {'id': 40, 'title': 'new title 1', 'image': 'new image 1', 'likes': 0}
queue_1    | Traceback (most recent call last):
queue_1    |   File "/app/consumer.py", line 42, in <module>
queue_1    |     channel.start_consuming()
queue_1    |   File "/usr/local/lib/python3.10/site-packages/pika/adapters/blocking_connection.py", line 1883, in start_consuming
queue_1    |     self._process_data_events(time_limit=None)
queue_1    |   File "/usr/local/lib/python3.10/site-packages/pika/adapters/blocking_connection.py", line 2044, in _process_data_events
queue_1    |     self.connection.process_data_events(time_limit=time_limit)
queue_1    |   File "/usr/local/lib/python3.10/site-packages/pika/adapters/blocking_connection.py", line 851, in process_data_events
queue_1    |     self._dispatch_channel_events()
queue_1    |   File "/usr/local/lib/python3.10/site-packages/pika/adapters/blocking_connection.py", line 567, in _dispatch_channel_events
queue_1    |     impl_channel._get_cookie()._dispatch_events()
queue_1    |   File "/usr/local/lib/python3.10/site-packages/pika/adapters/blocking_connection.py", line 1510, in _dispatch_events
queue_1    |     consumer_info.on_message_callback(self, evt.method,
queue_1    |   File "/app/consumer.py", line 26, in callback
queue_1    |     product = Product.query.get(data['id'])
queue_1    |   File "/usr/local/lib/python3.10/site-packages/flask_sqlalchemy/model.py", line 31, in __get__
queue_1    |     cls, session=cls.__fsa__.session()  # type: ignore[arg-type]
queue_1    |   File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/scoping.py", line 47, in __call__
queue_1    |     sess = self.registry()
queue_1    |   File "/usr/local/lib/python3.10/site-packages/sqlalchemy/util/_collections.py", line 1006, in __call__
queue_1    |     key = self.scopefunc()
queue_1    |   File "/usr/local/lib/python3.10/site-packages/flask_sqlalchemy/session.py", line 81, in _app_ctx_id
queue_1    |     return id(app_ctx._get_current_object())  # type: ignore[attr-defined]
queue_1    |   File "/usr/local/lib/python3.10/site-packages/werkzeug/local.py", line 513, in _get_current_object
queue_1    |     raise RuntimeError(unbound_message) from None
queue_1    | RuntimeError: Working outside of application context.
queue_1    | 
queue_1    | This typically means that you attempted to use functionality that needed
queue_1    | the current application. To solve this, set up an application context
queue_1    | with app.app_context(). See the documentation for more information.
main_queue_1 exited with code 1

This is my current consumer.py file state:

import json
import pika

from app import Product, db

params = pika.URLParameters('amqps://liqpanjb:[email protected]/liqpanjb')

connection = pika.BlockingConnection(params)

channel = connection.channel()
channel.queue_declare(queue='app')


def callback(ch, method, properties, body):
    print('Received in app')
    data = json.loads(body)
    print(data)

    if properties.content_type == 'product_created':
        product = Product(id=data['id'], title=data['title'], image=data['image'])
        db.session.add(product)
        db.session.commit()

    elif properties.content_type == 'product_updated':
        product = Product.query.get(data['id'])
        product.title = data['title']
        product.image = data['image']
        db.session.commit()

    elif properties.content_type == 'product_deleted':
        product = Product.query.get(data['id'])
        db.session.delete(product)
        db.session.commit()


channel.basic_consume(queue='app', on_message_callback=callback, auto_ack=True)

print('Started Consuming...')

channel.start_consuming()
channel.close()

This is my app.py (main) state:

from dataclasses import dataclass

from flask import Flask, jsonify
from flask_cors import CORS
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import UniqueConstraint


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:root@db/shop_db_main'
CORS(app)

db = SQLAlchemy(app)
migrate = Migrate(app, db)


@dataclass
class Product(db.Model):
    id: int
    title: str
    image: str

    id = db.Column(db.Integer, primary_key=True, autoincrement=False)
    title = db.Column(db.String(200))
    image = db.Column(db.String(200))


@dataclass
class ProductUser(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer)
    product_id = db.Column(db.Integer)

    UniqueConstraint('user_id', 'product_id', name='user_product_unique')


@app.route('/api/products')
def index():
    return jsonify(Product.query.all())


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

These are the packages I have installed (versions included):

alembic==1.8.1
certifi==2022.9.24
charset-normalizer==2.1.1
click==8.1.3
Flask==2.2.2
Flask-Cors==3.0.10
Flask-Migrate==4.0.0
Flask-Script==2.0.6
Flask-SQLAlchemy==3.0.2
greenlet==2.0.1
idna==3.4
itsdangerous==2.1.2
Jinja2==3.1.2
Mako==1.2.4
MarkupSafe==2.1.1
mysqlclient==2.1.1
pika==1.3.1
requests==2.28.1
six==1.16.0
SQLAlchemy==1.4.44
urllib3==1.26.12
Werkzeug==2.2.2

Am I overlooking something because this seems very strange behavior and I am having trouble finding a solution to it. Any help is much appreciated!

Docker-compose up --build

[+] Building 2.3s (6/10)
 => [queue internal] load build definition from Dockerfile
 => => transferring dockerfile: 198B
 => [queue internal] load metadata for docker.io/library/python:3.10
 => [queue auth] library/python:pull token for registry-1.docker.io
 => [queue internal] load .dockerignore
 => => transferring context: 63B
 => [queue 1/5] FROM docker.io/library/python:3.10@sha256:0be08c1c2c060efbcb8cf14d3d2c3e1e8d3f46ca7cb6937e7565b4658a85b790
 => CANCELED [queue internal] load build context
 => => transferring context: 273.44kB
failed to solve: Canceled: context canceled

From what I have gathered this issue was introduced in Docker Desktop 4.23 and newer versions. Thproposed solution on most forum discussions was to downgrade to Docker Desktop 4.22 However I believe there is a better solution out there. This issue for some reason arrises while transfering content from:

=> => transferring context: 63B
 => [queue 1/5] FROM docker.io/library/python:3.10 

perhaps the solution if to include a .dockerignore file and ignore a part of the python library. Or use a different python version like slim.

If anyone has enountered the same issue please LMK what was your solution.
I will try the proposed solution in the meantime.

And big thanks to Scalable Scripts for posting the tutoreal! Real G!

EDIT: after further diagnosing the issue the problem seems to be not with the docker itself, but with the permissions.
Running the docker-compose up from WSL terminal gave me a more detailed error message:

 => ERROR [queue internal] load build context                                                                      0.0s
 => => transferring context: 265B                                                                                  0.0s
[queue internal] load build context: failed to solve: error from sender: open /microservices_app/admin/.dbdata/#innodb_redo: permission denied

So the solution could be to change the permissions to the .dbdata repo using chmod 755, but I feel like that would be violating all the safety protocalls and exposing the db credentials to all users. I will look for a better solution

EDIT: I fixed this error by going into WSL and running the docker compose with sudo inside the project directory
sudo docker-compose up

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.