Beispiel #1
0
def test_factory_pattern(app, config):
    rq = RQ(default_timeout=123)
    rq.init_app(app)
    rq.default_timeout = 456  # override default timeout
    rq.job(add)
    add.helper.timeout == 456
    add.helper.timeout = 789
    add.helper.timeout == 789
Beispiel #2
0
def test_init_app(app, config):
    rq = RQ()
    assert 'rq2' not in getattr(app, 'extensions', {})
    assert getattr(rq, 'module', None) is None

    rq.init_app(app)
    assert rq.redis_url == config.RQ_REDIS_URL
    assert isinstance(rq.connection, StrictRedis)
    assert 'rq2' in getattr(app, 'extensions', {})
Beispiel #3
0
def init_rq(app):
    """
    :param app: flask app
    :return: redis queue handle
    """
    # redis host url
    app.config['RQ_REDIS_URL'] = REDIS_URL
    # redis task queue
    rq = RQ(app)
    return rq
Beispiel #4
0
def test_init_app(app, config):
    rq = RQ()
    assert 'rq2' not in getattr(app, 'extensions', {})
    assert getattr(rq, 'module', None) is None

    rq.init_app(app)
    assert rq.url == config.RQ_REDIS_URL
    assert isinstance(rq.connection, Redis)
    assert 'rq2' in getattr(app, 'extensions', {})
    assert rq.module_path == 'flask_rq2.backend_%s' % app.name
Beispiel #5
0
def test_job_override(app, config):
    rq = RQ(app, async_=True)

    rq.job(add, timeout=123, result_ttl=456, ttl=789)
    assert add.helper.timeout == 123
    assert add.helper.result_ttl == 456
    assert add.helper.ttl == 789

    job1 = add.queue(timeout=111, result_ttl=222, ttl=333)
    assert job1.timeout == 111
    assert job1.result_ttl == 222
    assert job1.ttl == 333
Beispiel #6
0
def test_app_context(app):
    rq = RQ(app)

    class ContextCommand(RQCommand):
        def run(self):
            assert current_app == app
            return current_app.name

    command = ContextCommand(rq)
    assert command.rq == rq

    result = command(app)
    assert result == app.name
Beispiel #7
0
def test_factory_pattern(app, config):
    rq = RQ(default_timeout=111)
    rq.init_app(app)

    # override some rq defaults
    rq.default_timeout = 222
    rq.default_result_ttl = 333
    rq.default_queue = 'non-default'
    rq.job(add)

    # then check if those default have been passed to the helper
    assert add.helper.timeout == 222
    assert add.helper.result_ttl == 333
    assert add.helper.queue_name == 'non-default'

    # then queue if the values have been passed to the job as well
    job = add.queue(1, 2)
    assert job.timeout == 222
    assert job.result_ttl == 333
    assert job.ttl is None
    assert job.origin == 'non-default'

    # change the values in the helpr and see if that works
    add.helper.timeout = 444
    assert add.helper.timeout == 444
    add.helper.result_ttl = 555
    assert add.helper.result_ttl == 555
    add.helper.queue_name = 'totally-different'
    assert add.helper.queue_name == 'totally-different'

    # assert the helper's values
    job = add.queue(1, 2)
    assert job.timeout == 444
    assert job.result_ttl == 555
    assert job.ttl is None
    assert job.origin == 'totally-different'

    # now finally override the values while queueing
    job = add.queue(1,
                    2,
                    queue='yet-another',
                    timeout=666,
                    result_ttl=777,
                    ttl=888)
    assert job.timeout == 666
    assert job.result_ttl == 777
    assert job.ttl == 888
    assert job.origin == 'yet-another'
Beispiel #8
0
def test_scheduler_command_pid(config, app, monkeypatch, tmpdir):
    monkeypatch.setattr(flask_rq2_app.Scheduler, 'run',
                        lambda *args, **kwargs: None)
    rq = RQ(app)
    manager = RQManager(app=app, rq=rq)
    pid = tmpdir.join('rq2_scheduler.pid')
    assert not pid.exists()
    monkeypatch.setattr(sys, 'argv', ['rq', 'scheduler', '--pid', pid.strpath])
    try:
        manager.run()
    except SystemExit as e:
        exit_code = e.code
    else:
        exit_code = None
    assert exit_code == 0
    assert pid.read() != ''
Beispiel #9
0
def test_queue_job(app):
    rq = RQ(app, async_=True)
    rq.connection.flushdb()
    rq.job(add)

    job1 = add.queue(1, 2)
    assert isinstance(job1, import_attribute(rq.job_class))
    assert job1.args == (1, 2)
    assert job1.kwargs == {}
    assert job1.timeout == add.helper.timeout == rq.default_timeout

    job2 = add.queue(3, 4, description='job 2')
    assert job2.description == 'job 2'

    job3_id = uuid.uuid4().hex
    job3 = add.queue(5, 6, job_id=job3_id)
    assert job3.id == job3_id

    job4 = add.queue(7, 8, depends_on=job3)
    assert job4.dependency.id == job3.id

    other_queue = 'other_queue'
    job5 = add.queue(9, 10, queue=other_queue)
    # job will be scheduled in the other queue eventually
    assert job5.origin == other_queue

    job6 = add.queue(11, 12)
    result = job6.perform()
    assert result == 23

    queue = rq.get_queue()
    assert job1 in queue.jobs
    assert job2 in queue.jobs
    assert job3 in queue.jobs
    # job 4 is a dependency on job 3, so not queued yet
    assert job4 not in queue.jobs

    assert job3.result is None
    assert job4.result is None
    response = rq.get_worker('default').work(True)
    assert response
    assert job4.dependency.result == 11
    assert job4.result == 15

    assert len(queue.jobs) == 0
Beispiel #10
0
def test_scheduler_command_verbose(config, app, monkeypatch):
    monkeypatch.setattr(flask_rq2_app.Scheduler, 'run',
                        lambda *args, **kwargs: None)
    rq = RQ(app)
    manager = RQManager(app=app, rq=rq)

    def setup_loghandlers(level):
        assert level == 'DEBUG'

    monkeypatch.setattr(flask_rq2_script, 'setup_loghandlers',
                        setup_loghandlers)

    monkeypatch.setattr(sys, 'argv', ['rq', 'scheduler', '--verbose'])
    try:
        manager.run()
    except SystemExit as e:
        exit_code = e.code
    else:
        exit_code = None
    assert exit_code == 0
Beispiel #11
0
def test_schedule_job(app):
    rq = RQ(app, is_async=True)
    scheduler = rq.get_scheduler()
    purge(scheduler)
    assert scheduler.count() == 0
    rq.job(add)

    job1 = add.schedule(timedelta(seconds=1), 1, 2)
    assert scheduler.count() == 1
    assert job1 in scheduler.get_jobs()
    purge(scheduler)

    job2 = add.schedule(datetime.utcnow() + timedelta(seconds=1), 3, 4)
    assert scheduler.count() == 1
    assert job2 in scheduler.get_jobs()
    purge(scheduler)

    job3_id = uuid.uuid4().hex
    job3_description = 'custom description'
    job3 = add.schedule(timedelta(seconds=1),
                        5,
                        6,
                        repeat=10,
                        interval=2,
                        description=job3_description,
                        job_id=job3_id)
    assert job3 in scheduler.get_jobs()
    assert job3.meta.get('repeat') == 10
    assert job3.meta.get('interval') == 2
    assert job3.id == job3_id
    assert job3.description == job3_description
    purge(scheduler)

    other_queue = 'other-queue'
    job4 = add.schedule(timedelta(seconds=1), 5, 6, queue=other_queue)
    # job will be scheduled in the other queue eventually
    assert job4.origin == other_queue
    # one more. the scheduler will have all jobs, no matter what
    # queue the job will eventually be queued in.
    assert job4 in scheduler.get_jobs()
    purge(scheduler)
Beispiel #12
0
def test_commands(command, output, uses_logging, app, caplog, capsys,
                  monkeypatch, request):
    rq = RQ(app)
    manager = RQManager(app=app, rq=rq)
    monkeypatch.setattr(sys, 'argv', ['manage.py'] + command.split())
    try:
        manager.run()
    except SystemExit as e:
        exit_code = e.code
    else:
        exit_code = None
    assert exit_code == 0
    if uses_logging:
        caplog.setLevel(logging.INFO, logger='rq.worker')
        out = caplog.text()
    else:
        out, err = capsys.readouterr()
    assert output in out

    def flush():
        rq.connection.flushdb()

    request.addfinalizer(flush)
Beispiel #13
0
def test_cron_job(app):
    rq = RQ(app, async_=True)
    scheduler = rq.get_scheduler()
    purge(scheduler)
    assert scheduler.count() == 0
    rq.job(add)

    cron_string = '* * * * *'
    cron_name = 'add-it-for-real'
    job1 = add.cron(cron_string, cron_name, 1, 2)
    assert scheduler.count() == 1
    assert job1 in scheduler.get_jobs()
    assert job1.meta['cron_string'] == cron_string
    assert job1.id == 'cron-' + cron_name
    purge(scheduler)

    job2 = add.cron(cron_string, cron_name, 3, 4)
    assert scheduler.count() == 1  # no duplicate here
    assert job2 in scheduler.get_jobs()
    assert job2.meta['cron_string'] == cron_string
    assert job2.id == 'cron-' + cron_name

    job3 = add.cron(cron_string, cron_name + '-pro', 3, 4)
    assert scheduler.count() == 2  # second cron
    assert job3 in scheduler.get_jobs()
    assert job3.id == 'cron-' + cron_name + '-pro'

    other_queue = 'other-queue'
    job4 = add.cron(cron_string, cron_name + '-other', 3, 4, queue=other_queue)
    # job will be scheduled in the other queue eventually
    assert job4.origin == other_queue
    # one more. the scheduler will have all jobs, no matter what
    # queue the job will eventually be queued in.
    assert job4 in scheduler.get_jobs()
    assert scheduler.count() == 3

    purge(scheduler)
Beispiel #14
0
def rq(app):
    return RQ(app)
Beispiel #15
0
    from ddtrace import patch_all

    patch_all()


def load_corpus():
    mombler = Momblish(corpus=Corpus.load("support/corpus.json"))
    return mombler


db = SQLAlchemy()
migrate = Migrate()
jwt = JWTManager()
momblish = load_corpus()
json_schema_manager = JSONSchemaManager("../support/schemas")
Q = RQ()
redis_client = FlaskRedis()
Q.queues = ["email", "nomad"]
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


def create_app(env: str = "development"):
    app = Flask(__name__)
    app.config.from_object(settings.app_config[env])

    db.init_app(app)
    migrate.init_app(app, db)
    jwt.init_app(app)
    json_schema_manager.init_app(app)
    Q.init_app(app)
app = Flask(__name__)
app.config.from_object("config")

cache = Cache(app)
cors = CORS(app)
db = SQLAlchemy(app)
jwt = JWTManager(app)
limiter = Limiter(
    app,
    key_func=get_remote_address,
    default_limits=["100 per minute", "5 per second"],
)
mail = Mail(app)
swagger = Swagger(app)
storage = Minio(app)
rq = RQ(app)

# -- Handler
from app.handlers import (http_handler, jwt_handler, log_handler)

# Comment above code when change database systems
if not os.path.exists("db.sqlite"):
    db.create_all()

# -- Controllers
from app.controllers import (auth_controller, file_controller,
                             homepage_controller, mail_controller,
                             user_controller)

app.register_blueprint(auth_controller.mod)
app.register_blueprint(file_controller.mod)
Beispiel #17
0
app = Flask(__name__)
with app.app_context():
    from helper import *
"""
__author__      : Bijin Benny
__email__       : [email protected]
__license__     : MIT
__version__     : 1.0
Modification    : The native Redis library used in the original reference is 
                  outdated and is modified to use the new redis library specific
                  to Flask apps
                  
Configure and intialize Redis task queue    
"""
app.config['RQ_REDIS_URL'] = 'redis://localhost:6379/0'
redis_conn = RQ(app)
"""
__author__      : Bijin Benny
__email__       : [email protected]
__license__     : MIT
__version__     : 1.0
Modification    : The deprecated elasticsearch library elasticsearch_dsl is 
                  removed and replaced with the new elasticsearch library for 
                  ES clients

Load environment variables and create elastic search DB client 
"""
host = os.getenv("HOST")
user = os.getenv("USERNAME")
pwd = os.getenv("PASSWORD")
port = os.getenv("PORT")
Beispiel #18
0
def rq_cli_app(app):
    app.cli.name = app.name
    RQ(app)
    return app
 def __init__(self):
     super(WorkerCommand, self).__init__()
     self.rq = RQ()
class BaseRestApiException(Exception):
    pass


def create_app():
    app = Flask(__name__)
    app.config.from_object(settings)
    app.name = app.config["APP_NAME"]
    return app


app = create_app()
app.url_map.strict_slashes = False
app.register_blueprint(rq_dashboard.blueprint, url_prefix="/math-ocr/rq")
api = Api(app)
redis_q = RQ(app)

# Call factory function to create our blueprint
swaggerui_blueprint = get_swaggerui_blueprint(
    settings.
    SWAGGER_URL,  # Swagger UI static files will be mapped to '{SWAGGER_URL}/dist/'
    settings.API_URL,
    config={  # Swagger UI config overrides
        'app_name': "Test application"
    },
    # oauth_config={  # OAuth config. See https://github.com/swagger-api/swagger-ui#oauth2-configuration .
    #    'clientId': "your-client-id",
    #    'clientSecret': "your-client-secret-if-required",
    #    'realm': "your-realms",
    #    'appName': "your-app-name",
    #    'scopeSeparator': " ",
Beispiel #21
0
def test_rq_outside_flask():
    rq = RQ()
    # the redis connection is none since the Flask app context isn't there
    assert rq.connection is None
Beispiel #22
0
def rq_cli_app(app):
    FlaskCLI(app)
    app.cli.name = app.name
    RQ(app)
    return app
Beispiel #23
0
from services.stripe import Stripe

# Setup flask cache
cache = Cache()

# init flask assets
assets_env = Environment()

debug_toolbar = DebugToolbarExtension()

login_manager = LoginManager()
login_manager.login_view = "auth.login"
login_manager.login_message_category = "warning"
# login_manager.user_loader is registered in controllers/auth.

# TODO:
login_manager.refresh_view = "auth.reauth"
login_manager.needs_refresh_message = (
    u"To protect your account, please reauthenticate to access this page.")
login_manager.needs_refresh_message_category = "info"

token = Token()

socketio = SocketIO()
rq2 = RQ()
admin = AdminDashboard()
mail = Mail()
limiter = Limiter(key_func=get_remote_address)
stripe = Stripe()
sentry = Sentry()
Beispiel #24
0
def test_rq_outside_flask():
    rq = RQ()
    assert pytest.raises(RuntimeError, lambda: rq.connection)
Beispiel #25
0
from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_migrate import Migrate
from flask_rq2 import RQ
import config
import os

logger = None

bcrypt = Bcrypt()
db = SQLAlchemy()
rq = RQ(default_timeout=180 * 2)
migrate = Migrate()


def create_app(config_name=None):
    global logger

    # Define the WSGI application object
    app = Flask(__name__)

    # Configurations
    app.config.from_object(_get_config_class(config_name))

    logger = app.logger

    # Define Database object which is imported by some modules
    bcrypt.init_app(app)
    rq.init_app(app)
    db.init_app(app)
Beispiel #26
0
def test_config_default_timeout(app, config):
    rq3 = RQ(app, default_timeout=911)
    assert rq3.default_timeout != Queue.DEFAULT_TIMEOUT
    assert rq3.default_timeout == 911
Beispiel #27
0
def test_config_async_override(app, config, rq):
    rq2 = RQ(app, async_=not config.RQ_ASYNC)
    assert rq2._async != config.RQ_ASYNC
Beispiel #28
0
def job_fetch(self, id):
    job = False, None
    try:
        job = True, Job.fetch(id, connection=rq_instance.connection)
    except NoSuchJobError as e:
        job = False, str(e)
    except Exception as e:
        job = False, str(e)
    return job


# class method injection
RQ.job_fetch = job_fetch

rq_instance = RQ()

RESUTL_TTL = '7d'  # -1 for never expire, clean up result key manually
RUN_MINTCAST_JOB_TIMEOUT = '20d'
NORMAL_JOB_TIMEOUT = '30m'
DOWNLOAD_JOB_TIMEOUT = '4h'


def queue_job_with_connection(job,
                              connection,
                              *args,
                              _queue_name=None,
                              **kwargs):
    if not connection:
        return job
Beispiel #29
0
from flask import Flask
from flask_rq2 import RQ
from dobby_hardware.wheels import Wheels  # DON'T REMOVE
from dobby_hardware.camservo import Camservo  # DON'T REMOVE
from dobby_hardware.ultrasound import Ultrasonic  # DON'T REMOVE

rq = RQ()


def create_app():

    app = Flask(__name__)
    app.config.from_object('config')

    rq.init_app(app)

    from .api.wheels.views import wheels as wheelsapi
    from .api.cam.views import cam as camapi
    from .api.ultrasonic.views import path as pathapi

    app.register_blueprint(wheelsapi)
    app.register_blueprint(camapi)
    app.register_blueprint(pathapi)

    import web.jobs  # DON'T REMOVE

    return app
Beispiel #30
0
from __future__ import absolute_import, print_function

from flask import Flask
from flask_rq2 import RQ


class Config(object):
    RQ_REDIS_URL = 'redis://localhost:6379/15'
    RQ_QUEUES = ['test-queue']
    RQ_ASYNC = False
    RQ_SCHEDULER_QUEUE = 'scheduler-queue'
    RQ_SCHEDULER_INTERVAL = 42


testapp = Flask('testapp')
testapp.config.from_object(Config())
testrq = RQ(testapp)