Skip to content

Instantly share code, notes, and snippets.

@pcn
Last active December 3, 2016 05:48
Show Gist options
  • Select an option

  • Save pcn/71c5b1d3671703dc225746d452654529 to your computer and use it in GitHub Desktop.

Select an option

Save pcn/71c5b1d3671703dc225746d452654529 to your computer and use it in GitHub Desktop.
Using rq and flask-rq2 to create a queue with some variations
#!/usr/bin/env python
from flask import Flask, request, jsonify, redirect, url_for, Response
import sys, json, requests, re
import os
import logging
import logging.handlers
import time
from redis import Redis
from rq import Queue
from flask_rq2 import RQ
import ops_deploy
from ops_deploy.squirrel_deploy import runit
app = Flask(__name__)
rq = RQ(app)
log = logging.getLogger('DefaultLog')
log.setLevel(logging.DEBUG)
# log.addHandler(syslog_handler)
@rq.job('medium')
def call_runit_deploy(command="/home/spacey/bin/test-script.sh",
arglist=[], tag="test"):
return str(runit.queued_runit_deploy(command, arglist, tag))
@rq.job('medium')
def add(a, b):
return a + b
@app.route("/output_lots", methods=["GET", "POST"])
def output_lots():
"""
Given a squirrel runit command, run it and return results.
The command will log its output elsewhere.
Use a streaming context to return data as the command is running,
so the connection doesn't time out.
This will pass a low-bandiwdth stream of status data to the caller to keep
the connection alive as well as to eventually return the status of the
process.
"""
app.logger.info("got output_lots")
# command_data_list = data['command'].split()[1:]
def generate_status_json():
# It seems like when invoking this function via the Response
# call, the app.app_context context manager is needed to provide
# a connection to redis.
with app.app_context():
q = rq.get_queue('medium')
job = call_runit_deploy.queue()
yield '{'
yield '''"jobid": "{}"\n'''.format(job.id)
while True:
app.logger.warn("Waiting")
j = q.fetch_job(job.id)
if j.result is not None:
break
yield '''"waiting": "more",\n'''
time.sleep(1)
yield '''"result": true }'''
return Response(generate_status_json(),
mimetype='application/json')
@app.route("/submit_job", methods=["GET", "POST"])
def submit_job():
def generate_status_json():
job = call_runit_deploy.queue()
yield '{'
yield '''"jobid": "{}"'''.format(job.id)
while not job.result:
app.logger.warn("Waiting")
time.sleep(1)
yield '''"result": true }'''
# return Response(generate_status_json(),
# mimetype='application/json')
job = call_runit_deploy.queue()
return {"jobid": job.id}
@app.route("/get_job", methods=["POST"])
def get_job():
data = request.get_json()
q = rq.get_queue('medium')
j = q.fetch_job(data['jobid'])
try:
return str(j.result)
except AttributeError:
return str(j)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment