[Fixture] 添加 rerun function

pull/417/head
ibuler 8 years ago
parent 915873135e
commit 240c7db416

@ -40,7 +40,7 @@ class TaskRecord(models.Model):
from assets.models import Asset
return [Asset.objects.get(hostname=hostname)._to_secret_json()
for hostname in self.total_assets
if Asset.objects.exists(hostname=hostname)]
if Asset.objects.filter(hostname=hostname)]
@property
def module_args(self):

@ -70,8 +70,20 @@ def run_AdHoc(self, task_tuple, assets,
return summary
@shared_task(bind=True)
def push_users(self, assets, users):
def rerun_AdHoc(uuid):
from .models import TaskRecord
record = TaskRecord.objects.get(uuid=uuid)
assets = record.assets_json
task_tuple = record.module_args
pattern = record.pattern
task_name = record.name
task = run_AdHoc.apply_async((task_tuple, assets),
{'pattern': pattern, 'task_name': task_name},
task_id=uuid)
return task
def push_users(assets, users):
"""
user: {
name: 'web',
@ -104,25 +116,7 @@ def push_users(self, assets, users):
))
])
task_name = 'Push user {}'.format(','.join([user['name'] for user in users]))
record = TaskRecord(name=task_name,
uuid=self.request.id,
date_start=timezone.now(),
assets=','.join(asset['hostname'] for asset in assets))
record.save()
logger.info('Runner {0} start {1}'.format(task_name, timezone.now()))
hoc = AdHocRunner(assets)
ts_start = time.time()
_ = hoc.run(task_tuple)
logger.info('Runner {0} complete {1}'.format(task_name, timezone.now()))
result_clean = hoc.clean_result()
record.time = int(time.time() - ts_start)
record.date_finished = timezone.now()
record.is_finished = True
if len(result_clean['failed']) == 0:
record.is_success = True
else:
record.is_success = False
record.result = json.dumps(result_clean)
record.save()
return result_clean
task = run_AdHoc.delay(task_tuple, assets, pattern='all', task_name=task_name)
return task

@ -231,7 +231,7 @@ def push_system_user(assets, system_user):
assets = [asset._to_secret_json() for asset in assets]
system_user = system_user._to_secret_json()
task = push_users.delay(assets, system_user)
task = push_users(assets, system_user)
return task.id

@ -49,7 +49,7 @@
<i class="fa fa-coffee"></i> <span class="nav-label">{% trans 'Job Center' %}</span><span class="fa arrow"></span>
</a>
<ul class="nav nav-second-level">
<li id="task"><a href="{% url 'ops:task-record-list' %}">{% trans 'Task Record' %}</a></li>
<li id="task-record"><a href="{% url 'ops:task-record-list' %}">{% trans 'Task Record' %}</a></li>
</ul>
</li>

@ -29,7 +29,7 @@ def start_django():
def start_celery():
os.chdir(apps_dir)
os.environ.setdefault('C_FORCE_ROOT', '1')
os.environ.setdefault('PYTHONOPTIMIZE', 1)
os.environ.setdefault('PYTHONOPTIMIZE', '1')
print('start celery')
subprocess.call('celery -A common worker -s /tmp/celerybeat-schedule -l debug', shell=True)

Loading…
Cancel
Save