Add task and create_task cli

This commit is contained in:
Adrian Moennich 2014-11-12 17:07:31 +01:00
parent 73f5a05bb7
commit 96d3e0fc56
2 changed files with 71 additions and 1 deletions

View File

@ -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'

View File

@ -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 <http://www.gnu.org/licenses/>.
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)