Skip to content

Commit

Permalink
refactor: async endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJoin95 committed Jul 24, 2023
1 parent 260d270 commit 36e2eab
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
15 changes: 10 additions & 5 deletions src/ign-api/convert_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@
from flask import jsonify, abort
from flask import request
from flask_cors import CORS, cross_origin
from flask import Blueprint

from ImageGoNord import GoNord, NordPaletteFile

from rq import Queue
from rq.job import Job
from worker import conn
from run import app, API_VERSION, q, cors

@app.route(API_VERSION + "/convert-async", methods=["POST"])
q = Queue(connection=conn)
API_VERSION = '/v1'

convert_async_api = Blueprint('convert_async_api', __name__)

@convert_async_api.route(API_VERSION + "/convert-async", methods=["POST"])
@cross_origin(origin='*')
def convert_queue():
go_nord = setup_instance(request)
Expand All @@ -29,9 +34,9 @@ def convert_queue():
if (request.form.get('output_path') != None):
output_path = request.form.get('output_path')

# convert_image(go_nord, image, output_path, request, response)
job = q.enqueue(f=convert_image, job_timeout='60s', args=(go_nord, image, output_path, request.form.get('b64_output'), response))
conn.incr('conversion_count', 1)
job = q.enqueue(f=convert_image, ttl=900, failure_ttl=900, job_timeout='180s', args=(go_nord, image, output_path, request.form.get('b64_output'), response))

return job.id

def convert_image(go_nord, image, save_path, b64_output, response):
Expand Down
2 changes: 1 addition & 1 deletion src/ign-api/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pillow==7.2.0
flask==1.1.2
flask_cors==3.0.8
image-go-nord==0.0.8
image-go-nord==0.1.6
gunicorn==20.0.4
rq==1.8.0
pytest==6.0.2
Expand Down
12 changes: 7 additions & 5 deletions src/ign-api/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@
from flask import Flask
from flask import jsonify, abort
from flask import request
from flask_cors import CORS
from flask_cors import CORS, cross_origin
from flask_restx import Api, Resource, fields
from werkzeug.datastructures import FileStorage

from rq import Queue
from rq.job import Job
from worker import conn

from convert_image import convert_async_api

q = Queue(connection=conn)
API_VERSION = 'v1'
API_VERSION = '/v1'
API_VERSION_URL = '/' + API_VERSION

app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'

import convert_image
app.register_blueprint(convert_async_api)

@app.route(API_VERSION + "/status", methods=["GET"])
@cross_origin(origin='*')
def get_api_status():
return jsonify({'ok': True})
return jsonify({'ok': True, 'count': conn.get('conversion_count')})

@app.route(API_VERSION + "/quantize", methods=["POST"])
@cross_origin(origin='*')
Expand Down Expand Up @@ -123,4 +125,4 @@ def setup_instance(req):


if __name__ == '__main__':
app.run(port=8000, threaded=True)
app.run(host='127.0.0.1', port=8000, threaded=True)
4 changes: 4 additions & 0 deletions src/ign-api/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from run import app

if __name__ == "__main__":
app.run()
2 changes: 1 addition & 1 deletion src/ign-frontend/src/components/Demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export default Vue.component('Demo', {
const im = new Image();
im.onload = () => {
const conversionCount = CookieManager.getCookie('conversion-counter') || 0;
CookieManager.setCookie('conversion-counter', parseInt(conversionCount) + 1);
CookieManager.setCookie('conversion-counter', parseInt(conversionCount, 10) + 1);
window.gtag('event', 'converted-image', {
event_category: 'converted-image',
event_label: (this.palette.name || 'Nordtheme'),
Expand Down

0 comments on commit 36e2eab

Please sign in to comment.