Added gevent server for tasks
This commit is contained in:
parent
a9762c1a11
commit
0e0131334a
5 changed files with 449 additions and 8 deletions
|
|
@ -18,6 +18,7 @@ from rq import Queue
|
|||
import importlib
|
||||
import traceback
|
||||
from modules.monit.models.monit import LonelyIpField
|
||||
from modules.pastafari2.libraries.configtask import config_task
|
||||
|
||||
class Task(WebModel):
|
||||
|
||||
|
|
@ -49,6 +50,7 @@ class Task(WebModel):
|
|||
self.register(DictField('data', data_field))
|
||||
self.register(corefields.IntegerField('num_servers'))
|
||||
self.register(corefields.BooleanField('is_parent'))
|
||||
self.register(corefields.BooleanField('status'))
|
||||
self.register(ParentField('parent_id', size=11, required=False, field_name='name_task'))
|
||||
self.fields['where_sql_server'].escape=True
|
||||
|
||||
|
|
@ -176,7 +178,7 @@ class Task(WebModel):
|
|||
return False
|
||||
"""
|
||||
|
||||
q = Queue(connection=Redis())
|
||||
#q = Queue(connection=Redis())
|
||||
|
||||
if yes_parent:
|
||||
arr_task=self.select_a_row(task_id)
|
||||
|
|
@ -196,22 +198,91 @@ class Task(WebModel):
|
|||
final_sql=sql_insert+", ".join(sql_insert_values)
|
||||
|
||||
self.query(final_sql)
|
||||
|
||||
"""
|
||||
with self.query('select id from task WHERE parent_id=%s', [task_id]) as cursor:
|
||||
|
||||
for row in cursor:
|
||||
#print(row)
|
||||
result = q.enqueue(task, row['id'], job_timeout=3600)
|
||||
"""
|
||||
|
||||
return send_task_to_server(task_id)
|
||||
|
||||
else:
|
||||
|
||||
#Enqueue task function.
|
||||
|
||||
result = q.enqueue(task, task_id, job_timeout=3600)
|
||||
#result = q.enqueue(task, task_id, job_timeout=3600)
|
||||
|
||||
http = urllib3.PoolManager()
|
||||
|
||||
try:
|
||||
|
||||
#@app.route('/exec/<api_key>/<int:task_id>')
|
||||
|
||||
r = http.request('GET', 'http://127.0.0.1:1337/exec/{}/{}'.format(config_task.api_key, task_id))
|
||||
|
||||
#print('http://127.0.0.1:1337/exec/{}/{}'.format(task_id, pastafari2_api_key))
|
||||
|
||||
if r.status!=200:
|
||||
self.error=True
|
||||
self.txt_error="Cannot access to task server: Error "+str(r.status)
|
||||
return False
|
||||
|
||||
else:
|
||||
resp=json.loads(r.data.decode('utf-8'))
|
||||
|
||||
if resp['error']:
|
||||
self.error=True
|
||||
self.txt_error=resp['message']
|
||||
logtask=LogTask(self.connection)
|
||||
logtask.insert(resp)
|
||||
return False
|
||||
|
||||
except urllib3.exceptions.MaxRetryError:
|
||||
self.error=True
|
||||
self.txt_error='Cannot connect to the task server, check if is up'
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
# Function used in rq worker for exec the ssh task.
|
||||
# Function for send task to task server
|
||||
|
||||
def send_task_to_server(task_id):
|
||||
|
||||
http = urllib3.PoolManager()
|
||||
|
||||
try:
|
||||
|
||||
#@app.route('/exec/<api_key>/<int:task_id>')
|
||||
|
||||
r = http.request('GET', 'http://127.0.0.1:1337/exec/{}/{}'.format(config_task.api_key, task_id))
|
||||
|
||||
#print('http://127.0.0.1:1337/exec/{}/{}'.format(task_id, pastafari2_api_key))
|
||||
|
||||
if r.status!=200:
|
||||
self.error=True
|
||||
self.txt_error="Cannot access to task server: Error "+str(r.status)
|
||||
return False
|
||||
|
||||
else:
|
||||
resp=json.loads(r.data.decode('utf-8'))
|
||||
|
||||
if resp['error']:
|
||||
self.error=True
|
||||
self.txt_error=resp['message']
|
||||
logtask=LogTask(self.connection)
|
||||
logtask.insert(resp)
|
||||
return False
|
||||
|
||||
except urllib3.exceptions.MaxRetryError:
|
||||
self.error=True
|
||||
self.txt_error='Cannot connect to the task server, check if is up'
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
# Function used in rq worker for exec the ssh task in rq.
|
||||
|
||||
def task(task_id):
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue