diff --git a/livesync/indico_livesync/cli.py b/livesync/indico_livesync/cli.py index e701538..9a3fd19 100644 --- a/livesync/indico_livesync/cli.py +++ b/livesync/indico_livesync/cli.py @@ -16,17 +16,22 @@ from __future__ import unicode_literals +import sys + import transaction +from dateutil import rrule from flask_pluginengine import current_plugin from flask_script import Manager from terminaltables import AsciiTable from indico.core.db import db, DBMgr from indico.core.db.sqlalchemy.util.session import update_session_options +from indico.modules.scheduler import Client from indico.util.console import cformat, conferenceHolderIterator from MaKaC.conference import ConferenceHolder from indico_livesync.models.agents import LiveSyncAgent +from indico_livesync.task import LiveSyncTask cli_manager = Manager(usage="Manages LiveSync") @@ -105,7 +110,7 @@ def create_agent(agent_type, name=None): print agent -# TODO: delete_agent, update_agent, create_task +# TODO: delete_agent, update_agent @cli_manager.command @@ -131,3 +136,19 @@ def run(agent_id=None): agent.create_backend().run() finally: transaction.abort() + + +@cli_manager.command +def create_task(interval): + """Creates a livesync task running every N minutes""" + update_session_options(db) + try: + interval = int(interval) + if interval < 1: + raise ValueError + except ValueError: + print 'Invalid interval, must be a number >=1' + sys.exit(1) + with DBMgr.getInstance().global_connection(commit=True): + Client().enqueue(LiveSyncTask(rrule.MINUTELY, interval=interval)) + print 'Task created' diff --git a/livesync/indico_livesync/task.py b/livesync/indico_livesync/task.py new file mode 100644 index 0000000..ddd2421 --- /dev/null +++ b/livesync/indico_livesync/task.py @@ -0,0 +1,49 @@ +# This file is part of Indico. +# Copyright (C) 2002 - 2014 European Organization for Nuclear Research (CERN). +# +# Indico is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 3 of the +# License, or (at your option) any later version. +# +# Indico is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Indico; if not, see . + +from __future__ import unicode_literals + +from indico.core.db import DBMgr +from indico.modules.scheduler.tasks.periodic import PeriodicUniqueTask +from indico.util.date_time import now_utc + +from indico_livesync.models.agents import LiveSyncAgent + + +class LiveSyncTask(PeriodicUniqueTask): + DISABLE_ZODB_HOOK = True + + @property + def logger(self): + return self.getLogger() + + def extend_runtime(self): + # Make the task manager believe the task is running since a much shorter time + self.setOnRunningListSince(now_utc()) + DBMgr.getInstance().commit() + + def run(self): + from indico_livesync.plugin import LiveSyncPlugin + + plugin = LiveSyncPlugin.instance # RuntimeError if not active + with plugin.plugin_context(): + for agent in LiveSyncAgent.find_all(): + if not agent.initial_data_exported: + self.logger.warning('Skipping agent {}; initial export not performed yet'.format(agent.name)) + continue + with DBMgr.getInstance().global_connection(): + self.logger.info('Running agent {}'.format(agent.name)) + agent.create_backend().run(task=self)