def start_job(job_key):
    job_class = next((job
                      for job in BackgroundJobManager.available_job_classes()
                      if job.key() == job_key), None)
    if job_class:
        app.logger.info(
            f'Current user ({current_user.uid}) started job {job_class.key()}')
        job_class(app.app_context).run(force_run=True)
        return tolerant_jsonify(_job_class_to_json(job_class))
    else:
        raise ResourceNotFoundError(f'Invalid job_key: {job_key}')
def job_schedule():
    api_json = {
        'autoStart': app.config['JOBS_AUTO_START'],
        'jobs': [],
        'secondsBetweenJobsCheck':
        app.config['JOBS_SECONDS_BETWEEN_PENDING_CHECK'],
        'startedAt': to_isoformat(background_job_manager.get_started_at()),
    }
    for job in Job.get_all(include_disabled=True):
        job_class = next(
            (j for j in BackgroundJobManager.available_job_classes()
             if j.key() == job.key), None)
        if job_class:
            api_json['jobs'].append({
                **job.to_api_json(),
                **_job_class_to_json(job_class),
            })
    return tolerant_jsonify(api_json)
    def test_start_mock_jobs(self):
        """Runs jobs as scheduled and skips disabled jobs."""
        with override_config(app, 'JOB_MANAGER', _job_manager_config()):
            job_manager = BackgroundJobManager()
            try:
                job_manager.start(app)
                # The JOB_MANAGER config has four (4) jobs but one is disabled. So, only three are loaded.
                assert len(job_manager.job_instances) == 3
                time.sleep(2)

                for job_instance in job_manager.job_instances:
                    if job_instance.key() == 'volume':
                        assert job_instance.level == 11
                    elif job_instance.key() == 'volume_sub_class':
                        # Property not yet set
                        assert job_instance.level is None
                    elif job_instance.key() == 'light_switch':
                        assert job_instance.is_light_on is True
                    else:
                        assert False

            finally:
                job_manager.stop()
Exemple #4
0
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED
"AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
ENHANCEMENTS, OR MODIFICATIONS.
"""
import os

from diablo import cache, db
from diablo.configs import load_configs
from diablo.jobs.background_job_manager import BackgroundJobManager
from diablo.logger import initialize_logger
from diablo.routes import register_routes
from flask import Flask

background_job_manager = BackgroundJobManager()


def create_app(standalone=False):
    """Initialize app with configs."""
    app = Flask(__name__.split('.')[0])
    load_configs(app)
    initialize_logger(app)
    cache.init_app(app)
    cache.clear()
    db.init_app(app)

    if not standalone:
        with app.app_context():
            register_routes(app)
            _register_jobs(app)