Petter Holt Juliussen • Mail | Mastodon | GitHub | Letterboxd

for later reference.

Redis

2019-04-04
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Imports
import json
from croniter import croniter
from os import path
from requests import get
from datetime import timedelta
from flask import Flask, request, abort, render_template, jsonify, Response
# from flask_rq2 import RQ


from tasks import rq, sync
from podcast import parse_feed
from database import Podcast, Episode, EpisodeIndex, IntegrityError
# from logger import logger


# App
app = Flask(__name__)
rq.init_app(app)

#redis-server --daemonize yes
#rqscheduler
#rq worker --url redis://localhost:6379

#export FLASK_APP=hello.py
#flask run

scheduler = rq.get_scheduler()

job = sync.cron('*/1 * * * *', 'sync-podcasts')

# q = rq.get_queue()
# j = q.fetch_job('cron-test')
# j.delete()

# @app.route('/')
# def test():
#     # q = rq.get_queue()
#     # job = q.enqueue('rqtask.example', 10)
#     job = example.queue(3)

#     return jsonify({
#         'task_id': job.get_id()
#     })

@app.route('/status/')
def status():
    scheduled_jobs = {job.get_id(): {
        'scheduled_for': time,
        'status': job.get_status(),
        'enqueued_at': job.enqueued_at,
        'started_at': job.started_at,
        'ended_at': job.ended_at
    } for job, time in scheduler.get_jobs(with_times=True)}

    return jsonify({
        'scheduler': scheduled_jobs
    })

@app.route('/status/<task_id>')
def taskstatus(task_id):
    q = rq.get_queue()
    j = q.fetch_job(task_id)

    if not j:
        return jsonify({
            'id': task_id,
            'status': 'expired or unknown'
        })

    response = {
        'id': j.get_id(),
        'status': j.get_status(),
        'meta': j.meta,
        'result': j.result,
        'enqueued_at': j.enqueued_at,
        'started_at': j.started_at,
        'ended_at': j.ended_at
    }
    return jsonify(response)
# Imports
import time
from flask_rq2 import RQ
from rq import get_current_job


# RQ
rq = RQ()


# Job: Sync
@rq.job
def sync():
    ''
    job = get_current_job()

    seconds = 10

    for i in range(seconds):
        print i

        job.meta['progress'] = round((i/float(seconds)) * 100)
        job.save_meta()

        time.sleep(1)

    job.meta['progress'] = 100
    job.save_meta()

    return {
        'foo': 'bar'
    }