def get_task(): if not MUST_WORK: return '{"sleep":1}' worker = request.values['name'] ip = get_ip() db = DBVk() res = db.select_task_debt(worker) if res is not None: expire = db.update_task(res['id'], worker, ip, res['down'], res['up'], days=EXPIRE_DAYS) res['expire'] = expire res['comment'] = 'Your debt' else: res = db.select_task_free() if res is not None: expire = db.update_task(res['id'], worker, ip, res['down'], res['up'], days=EXPIRE_DAYS) res['expire'] = expire res['comment'] = 'Someone`s debt' else: ind, down = db.select_task_new() up = down - 1 + TASK_SIZE expire = db.insert_task(ind, worker, ip, down, up, days=EXPIRE_DAYS) res = { 'id': ind, 'worker': worker, 'ip': ip, 'down': down, 'up': up, 'expire': expire, } res['comment'] = 'New task' log_inf('GetTask: {}@{} got task ID {} as {}'.format( worker, ip, res['id'], res['comment'])) db.commit_db() return json.dumps(res)
def submit_task(): res = {} comment = '' if not MUST_WORK: code = 0 else: code = 2 # code = 3 if len(request.files) == 0: comment = 'file not found' elif len(request.files) > 1: comment = 'too many files' else: file = None for key in request.files: file = request.files[key] break data = None try: data = load_pickle(file) except: pass try: if data is not None and isinstance( data, dict) and 'vk_task_id' in data: ip = get_ip() worker = request.form['name'] ind = data['vk_task_id'] down = int(data['vk_from']) up = int(data['vk_to']) label = 'Task {} from {} to {}'.format(ind, down, up) label += ' Rows: {}'.format(len(data['data'])) res['file'] = label db = DBVk() can_load = False meta = db.select_task_id(ind) if meta is None: comment = 'task was not found' else: if meta['up'] == meta['down']: comment = 'task is already completed' code = 3 elif meta['worker'] != worker: comment = 'different name' log_er( 'SubmitTask: {}@{} tries to submit task of {}, ID is {}' .format(worker, ip, meta['worker'], ind)) elif down - meta['down'] < 3: can_load = True else: comment = 'need user_id since {}, not {}'.format( meta['down'], down) if can_load: for row in data['data']: # print('\n- Row:') # print(row) db.insert_features(row[0], row[1], row[2]) new_down = min(up, meta['up']) db.update_task(ind, worker, ip, new_down, meta['up'], days=EXPIRE_DAYS) db.commit_db() code = 1 comment = 'very well ' + str(len(data['data'])) log_inf( 'SubmitTask: {}@{} loaded task ID {} from {} to {}' .format(worker, ip, ind, down, up)) now = int(time.time()) if worker in activities_: time_log(worker, now - activities_[worker], up - down, data.get('notes')) activities_[worker] = now else: log_inf( 'SubmitTask: {}@{} cannot load task ID {} from {} to {} because "{}"' .format(worker, ip, ind, down, up, comment)) except Exception as ex: log_ex(ex, label, show=1) comment = 'very bad file' res['code'] = code res['descr'] = SUBMIT_CODES.get(code, '?') res['comment'] = comment # print(res) return json.dumps(res)