コード例 #1
0
ファイル: worker.py プロジェクト: stefanmk/fishtest
def worker(worker_info, password, remote):
  global ALIVE

  payload = {
    'worker_info': worker_info,
    'password': password,
  }

  try:
    req = requests.post(remote + '/api/request_version', data=json.dumps(payload), headers={'Content-type': 'application/json'}, timeout=HTTP_TIMEOUT)
    req = json.loads(req.text)

    if 'version' not in req:
      print 'Incorrect username/password'
      time.sleep(5)
      sys.exit(1)

    if req['version'] > WORKER_VERSION:
      print 'Updating worker version to %d' % (req['version'])
      #update()

    req = requests.post(remote + '/api/request_task', data=json.dumps(payload), headers={'Content-type': 'application/json'}, timeout=HTTP_TIMEOUT)
    req = json.loads(req.text)
  except:
    sys.stderr.write('Exception accessing host:\n')
    traceback.print_exc()
    time.sleep(10)
    return

  if 'error' in req:
    raise Exception('Error from remote: %s' % (req['error']))

  # No tasks ready for us yet, just wait...
  if 'task_waiting' in req:
    print 'No tasks available at this time, waiting...'
    time.sleep(10)
    return

  success = True
  run, task_id = req['run'], req['task_id']
  try:
    run_games(worker_info, password, remote, run, task_id)
  except:
    sys.stderr.write('\nException running games:\n')
    traceback.print_exc()
    success = False
  finally:
    payload = {
      'username': worker_info['username'],
      'password': password,
      'run_id': str(run['_id']),
      'task_id': task_id
    }
    try:
      requests.post(remote + '/api/failed_task', data=json.dumps(payload), headers={'Content-type': 'application/json'}, timeout=HTTP_TIMEOUT)
    except:
      pass
    sys.stderr.write('Task exited\n')

  return success
コード例 #2
0
def worker(worker_info, password, remote):
  global ALIVE

  payload = {
    'worker_info': worker_info,
    'password': password,
  }

  try:
    req = requests.post(remote + '/api/request_version', data=json.dumps(payload), headers={'Content-type': 'application/json'})
    req = json.loads(req.text)

    if 'version' not in req:
      print 'Incorrect username/password'
      time.sleep(5)
      sys.exit(1)

    if req['version'] > WORKER_VERSION:
      print 'Updating worker version to %d' % (req['version'])
      update()

    req = requests.post(remote + '/api/request_task', data=json.dumps(payload), headers={'Content-type': 'application/json'})
    req = json.loads(req.text)
  except:
    sys.stderr.write('Exception accessing host:\n')
    traceback.print_exc()
    time.sleep(10)
    return

  if 'error' in req:
    raise Exception('Error from remote: %s' % (req['error']))

  # No tasks ready for us yet, just wait...
  if 'task_waiting' in req:
    print 'No tasks available at this time, waiting...'
    time.sleep(10)
    return

  success = True
  run, task_id = req['run'], req['task_id']
  try:
    run_games(worker_info, password, remote, run, task_id)
  except:
    sys.stderr.write('\nException running games:\n')
    traceback.print_exc()
    success = False
  finally:
    payload = {
      'username': worker_info['username'],
      'password': password,
      'run_id': str(run['_id']),
      'task_id': task_id
    }
    try:
      requests.post(remote + '/api/failed_task', data=json.dumps(payload), headers={'Content-type': 'application/json'})
    except:
      pass
    sys.stderr.write('Task exited\n')

  return success
コード例 #3
0
ファイル: worker.py プロジェクト: phenri/fishtest
def worker(worker_info, password, remote):
    global ALIVE

    payload = {"worker_info": worker_info, "password": password}

    try:
        req = requests.post(remote + "/api/request_version", data=json.dumps(payload))
        req = json.loads(req.text)

        if "version" not in req:
            print "Incorrect username/password"
            time.sleep(5)
            sys.exit(1)

        if req["version"] > WORKER_VERSION:
            print "Updating worker version to %d" % (req["version"])
            update()

        req = requests.post(remote + "/api/request_task", data=json.dumps(payload))
        req = json.loads(req.text)
    except:
        sys.stderr.write("Exception accessing host:\n")
        traceback.print_exc()
        time.sleep(10)
        return

    if "error" in req:
        raise Exception("Error from remote: %s" % (req["error"]))

    # No tasks ready for us yet, just wait...
    if "task_waiting" in req:
        time.sleep(10)
        return

    success = True
    run, task_id = req["run"], req["task_id"]
    try:
        run_games(worker_info, password, remote, run, task_id)
    except:
        sys.stderr.write("\nException running games:\n")
        traceback.print_exc()
        success = False
    finally:
        payload = {
            "username": worker_info["username"],
            "password": password,
            "run_id": str(run["_id"]),
            "task_id": task_id,
        }
        try:
            requests.post(remote + "/api/failed_task", data=json.dumps(payload))
        except:
            pass
        sys.stderr.write("Task exited\n")

    return success
コード例 #4
0
ファイル: worker.py プロジェクト: G-Lorenz/fishtest
def worker(worker_info, password, remote):
    global ALIVE, FLEET

    payload = {"worker_info": worker_info, "password": password}

    try:
        print("Fetch task...")
        if not get_rate():
            raise Exception("Near API limit")

        t0 = datetime.utcnow()
        req = requests.post(
            remote + "/api/request_version",
            data=json.dumps(payload),
            headers={"Content-type": "application/json"},
            timeout=HTTP_TIMEOUT,
        )
        req = json.loads(req.text)

        if "version" not in req:
            print("Incorrect username/password")
            time.sleep(5)
            worker_exit()

        if req["version"] > WORKER_VERSION:
            print("Updating worker version to {}".format(req["version"]))
            update()
        print("Worker version checked successfully in {}s".format(
            (datetime.utcnow() - t0).total_seconds()))

        t0 = datetime.utcnow()
        worker_info["rate"] = rate
        req = requests.post(
            remote + "/api/request_task",
            data=json.dumps(payload),
            headers={"Content-type": "application/json"},
            timeout=HTTP_TIMEOUT,
        )
        req = json.loads(req.text)
    except Exception as e:
        sys.stderr.write("Exception accessing host:\n")
        print(e, file=sys.stderr)
        #    traceback.print_exc()
        if FLEET:
            worker_exit()
        time.sleep(random.randint(10, 60))
        return

    print("Task requested in {}s".format(
        (datetime.utcnow() - t0).total_seconds()))
    if "error" in req:
        raise Exception("Error from remote: {}".format(req["error"]))

    # No tasks ready for us yet, just wait...
    if "task_waiting" in req:
        print("No tasks available at this time, waiting...\n")
        if FLEET:
            worker_exit()
        # Note that after this sleep we have another ALIVE HTTP_TIMEOUT...
        time.sleep(random.randint(1, 10))
        return

    success = True
    global RUN, TASK_ID
    RUN, TASK_ID = req["run"], req["task_id"]
    try:
        pgn_file = run_games(worker_info, password, remote, RUN, TASK_ID)
    except:
        sys.stderr.write("\nException running games:\n")
        traceback.print_exc()
        success = False
    finally:
        payload = {
            "username": worker_info["username"],
            "password": password,
            "run_id": str(RUN["_id"]),
            "task_id": TASK_ID,
            "unique_key": worker_info["unique_key"],
        }
        try:
            requests.post(
                remote + "/api/failed_task",
                data=json.dumps(payload),
                headers={"Content-type": "application/json"},
                timeout=HTTP_TIMEOUT,
            )
        except:
            pass

        TASK_ID = None
        if success and ALIVE:
            sleep = random.randint(1, 10)
            print("Wait {} seconds before upload of PGN...".format(sleep))
            time.sleep(sleep)
            if "spsa" not in RUN["args"]:
                try:
                    with open(pgn_file, "r") as f:
                        data = f.read()
                    # Ignore non utf-8 characters in PGN file
                    if sys.version_info[0] == 2:
                        data = data.decode("utf-8", "ignore").encode(
                            "utf-8")  # Python 2
                    else:
                        data = bytes(data,
                                     "utf-8").decode("utf-8",
                                                     "ignore")  # Python 3
                    payload["pgn"] = base64.b64encode(
                        zlib.compress(data.encode("utf-8"))).decode()
                    print("Uploading compressed PGN of {} bytes".format(
                        len(payload["pgn"])))
                    requests.post(
                        remote + "/api/upload_pgn",
                        data=json.dumps(payload),
                        headers={"Content-type": "application/json"},
                        timeout=HTTP_TIMEOUT,
                    )
                except Exception as e:
                    sys.stderr.write("\nException PGN upload:\n")
                    print(e, file=sys.stderr)
        #          traceback.print_exc()
        try:
            os.remove(pgn_file)
        except:
            pass
        sys.stderr.write("Task exited\n")

    return success
コード例 #5
0
ファイル: worker.py プロジェクト: cwsaunders/fishtest
def worker(worker_info, password, remote):
    global ALIVE

    payload = {
        'worker_info': worker_info,
        'password': password,
    }

    try:
        print('Fetch task...')
        if not get_rate():
            raise Exception('Near API limit')

        t0 = datetime.utcnow()
        req = requests.post(remote + '/api/request_version',
                            data=json.dumps(payload),
                            headers={'Content-type': 'application/json'},
                            timeout=HTTP_TIMEOUT)
        req = json.loads(req.text)

        if 'version' not in req:
            print('Incorrect username/password')
            time.sleep(5)
            sys.exit(1)

        if req['version'] > WORKER_VERSION:
            print('Updating worker version to %s' % (req['version']))
            update()
        print("Worker version checked successfully in %ss" %
              ((datetime.utcnow() - t0).total_seconds()))

        t0 = datetime.utcnow()
        worker_info['rate'] = rate
        req = requests.post(remote + '/api/request_task',
                            data=json.dumps(payload),
                            headers={'Content-type': 'application/json'},
                            timeout=HTTP_TIMEOUT)
        req = json.loads(req.text)
    except Exception as e:
        sys.stderr.write('Exception accessing host:\n')
        print(e)
        #    traceback.print_exc()
        time.sleep(random.randint(10, 60))
        return

    print("Task requested in %ss" % ((datetime.utcnow() - t0).total_seconds()))
    if 'error' in req:
        raise Exception('Error from remote: %s' % (req['error']))

    # No tasks ready for us yet, just wait...
    if 'task_waiting' in req:
        print('No tasks available at this time, waiting...\n')
        # Note that after this sleep we have another ALIVE HTTP_TIMEOUT...
        time.sleep(random.randint(1, 10))
        return

    success = True
    run, task_id = req['run'], req['task_id']
    try:
        pgn_file = run_games(worker_info, password, remote, run, task_id)
    except:
        sys.stderr.write('\nException running games:\n')
        traceback.print_exc()
        success = False
    finally:
        payload = {
            'username': worker_info['username'],
            'password': password,
            'run_id': str(run['_id']),
            'task_id': task_id
        }
        try:
            requests.post(remote + '/api/failed_task',
                          data=json.dumps(payload),
                          headers={'Content-type': 'application/json'},
                          timeout=HTTP_TIMEOUT)
        except:
            pass

        if success and ALIVE:
            sleep = random.randint(1, 10)
            print('Wait %d seconds before upload of PGN...' % (sleep))
            time.sleep(sleep)
            if not 'spsa' in run['args']:
                try:
                    with open(pgn_file, 'r') as f:
                        data = f.read()
                    # Ignore non utf-8 characters in PGN file
                    if sys.version_info[0] == 2:
                        data = data.decode('utf-8', 'ignore').encode(
                            'utf-8')  # Python 2
                    else:
                        data = bytes(data,
                                     'utf-8').decode('utf-8',
                                                     'ignore')  # Python 3
                    payload['pgn'] = base64.b64encode(
                        zlib.compress(data.encode('utf-8'))).decode()
                    print('Uploading compressed PGN of %d bytes' %
                          (len(payload['pgn'])))
                    requests.post(remote + '/api/upload_pgn',
                                  data=json.dumps(payload),
                                  headers={'Content-type': 'application/json'},
                                  timeout=HTTP_TIMEOUT)
                except Exception as e:
                    sys.stderr.write('\nException PGN upload:\n')
                    print(e)


#          traceback.print_exc()
        try:
            os.remove(pgn_file)
        except:
            pass
        sys.stderr.write('Task exited\n')

    return success
コード例 #6
0
def fetch_and_handle_task(worker_info, password, remote, lock_file,
                          current_state):
    # This function should normally not raise exceptions.
    # Unusual conditions are handled by returning False.
    # If an immediate exit is necessary then one can set
    # current_state["alive"] to False.

    # The following check can be triggered theoretically
    # but probably not in practice.
    if locked_by_others(lock_file):
        current_state["alive"] = False
        return False

    payload = {"worker_info": worker_info, "password": password}

    try:
        rate, near_api_limit = get_rate()
        if near_api_limit:
            print("Near API limit")
            return False

        worker_info["rate"] = rate

        print("Verify worker version...")
        req = send_api_post_request(remote + "/api/request_version", payload)

        if "error" in req:
            current_state["alive"] = False
            return False

        if req["version"] > WORKER_VERSION:
            print("Updating worker version to {}".format(req["version"]))
            backup_log()
            update()
        print("Current time is {} UTC (offset: {}) ".format(
            datetime.datetime.utcnow(), utcoffset()))
        print("Fetching task...")
        req = send_api_post_request(remote + "/api/request_task", payload)
    except Exception as e:
        print("Exception accessing host:\n", e, sep="", file=sys.stderr)
        return False

    if "error" in req:
        return False

    # No tasks ready for us yet, just wait...
    if "task_waiting" in req:
        print("No tasks available at this time, waiting...")
        return False

    run, task_id = req["run"], req["task_id"]
    current_state["run"] = run
    current_state["task_id"] = task_id

    print("Working on task {} from {}/tests/view/{}".format(
        task_id, remote, run["_id"]))
    if "sprt" in run["args"]:
        type = "sprt"
    elif "spsa" in run["args"]:
        type = "spsa"
    else:
        type = "num_games"
    log("run: {} task: {} size: {} tc: {} concurrency: {} threads: {} [ {} : {} ]"
        .format(
            run["_id"],
            task_id,
            run["my_task"]["num_games"],
            run["args"]["tc"],
            worker_info["concurrency"],
            run["args"]["threads"],
            type,
            run["args"]["num_games"],
        ))
    print("Running {} vs {}".format(run["args"]["new_tag"],
                                    run["args"]["base_tag"]))

    success = False
    message = ""
    server_message = ""
    api = remote + "/api/failed_task"
    pgn_file = [None]
    try:
        run_games(worker_info, password, remote, run, task_id, pgn_file)
        success = True
    except FatalException as e:
        message = str(e)
        server_message = message
        current_state["alive"] = False
    except RunException as e:
        message = str(e)
        server_message = message
        api = remote + "/api/stop_run"
    except WorkerException as e:
        message = str(e)
        server_message = message
    except Exception as e:
        server_message = get_exception(["worker.py", "games.py"])
        message = "{} ({})".format(server_message, str(e))
        current_state["alive"] = False

    current_state["task_id"] = None
    current_state["run"] = None

    payload = {
        "password": password,
        "run_id": str(run["_id"]),
        "task_id": task_id,
        "message": server_message,
        "worker_info": worker_info,
    }

    if not success:
        print("\nException running games:\n", message, sep="", file=sys.stderr)
        print("Informing the server")
        try:
            req = send_api_post_request(api, payload)
        except Exception as e:
            print("Exception posting failed_task:\n",
                  e,
                  sep="",
                  file=sys.stderr)
    # Upload PGN file.
    pgn_file = pgn_file[0]
    if pgn_file is not None and os.path.exists(
            pgn_file) and "spsa" not in run["args"]:
        try:
            with open(pgn_file, "r") as f:
                data = f.read()
            # Ignore non utf-8 characters in PGN file.
            data = bytes(data, "utf-8").decode("utf-8", "ignore")
            payload["pgn"] = base64.b64encode(
                zlib.compress(data.encode("utf-8"))).decode()
            print("Uploading compressed PGN of {} bytes".format(
                len(payload["pgn"])))
            req = send_api_post_request(remote + "/api/upload_pgn", payload)
        except Exception as e:
            print("\nException uploading PGN file:\n",
                  e,
                  sep="",
                  file=sys.stderr)

    if pgn_file is not None and os.path.exists(pgn_file):
        try:
            os.remove(pgn_file)
        except Exception as e:
            print("Exception deleting PGN file:\n", e, sep="", file=sys.stderr)

    print("Task exited")

    return success
コード例 #7
0
ファイル: worker.py プロジェクト: tomtor/fishtest
def fetch_and_handle_task(worker_info, password, remote, current_state):
    # This function should normally not raise exceptions.
    # Unusual conditions are handled by returning False.
    # If an immediate exit is necessary then one can set
    # current_state["alive"] to False.

    payload = {"worker_info": worker_info, "password": password}

    try:
        rate, near_api_limit = get_rate()
        if near_api_limit:
            print("Near API limit")
            return False

        t0 = datetime.utcnow()

        print("Verify worker version...")
        req = requests.post(
            remote + "/api/request_version",
            data=json.dumps(payload),
            headers={
                "Content-type": "application/json"
            },
            timeout=HTTP_TIMEOUT,
        ).json()

        if "version" not in req:
            print("Incorrect username/password")
            current_state["alive"] = False
            return False

        if req["version"] > WORKER_VERSION:
            print("Updating worker version to {}".format(req["version"]))
            update()
        print("Worker version checked successfully in {}s".format(
            (datetime.utcnow() - t0).total_seconds()))

        t0 = datetime.utcnow()
        print("Fetch task...")
        worker_info["rate"] = rate
        req = requests.post(
            remote + "/api/request_task",
            data=json.dumps(payload),
            headers={
                "Content-type": "application/json"
            },
            timeout=HTTP_TIMEOUT,
        ).json()
    except Exception as e:
        print("Exception accessing host:\n", e, sep="", file=sys.stderr)
        return False

    print("Task requested in {}s".format(
        (datetime.utcnow() - t0).total_seconds()))
    if "error" in req:
        print("Error from remote: {}".format(req["error"]))
        return False

    # No tasks ready for us yet, just wait...
    if "task_waiting" in req:
        print("No tasks available at this time, waiting...")
        return False

    run, task_id = req["run"], req["task_id"]
    current_state["run"] = run
    current_state["task_id"] = task_id

    success = False
    try:
        pgn_file = run_games(worker_info, password, remote, run, task_id)
        success = True
    except FatalException as e:
        print("\nException running games:\n", e, sep="", file=sys.stderr)
        current_state["alive"] = False
    except Exception as e:
        print("\nException running games:\n", e, sep="", file=sys.stderr)
    finally:
        current_state["task_id"] = None
        current_state["run"] = None

    if not success:
        payload = {
            "username": worker_info["username"],
            "password": password,
            "run_id": str(run["_id"]),
            "task_id": task_id,
            "unique_key": worker_info["unique_key"],
        }
        try:
            requests.post(
                remote + "/api/failed_task",
                data=json.dumps(payload),
                headers={"Content-type": "application/json"},
                timeout=HTTP_TIMEOUT,
            )
        except Exception as e:
            print("Exception posting failed_task:\n",
                  e,
                  sep="",
                  file=sys.stderr)

    # Upload pgn file
    if success and current_state["alive"] and "spsa" not in run["args"]:
        sleep = random.randint(1, 10)
        print("Wait {} seconds before upload of PGN...".format(sleep))
        time.sleep(sleep)

        try:
            with open(pgn_file, "r") as f:
                data = f.read()
            # Ignore non utf-8 characters in PGN file
            if sys.version_info[0] == 2:
                data = data.decode("utf-8",
                                   "ignore").encode("utf-8")  # Python 2
            else:
                data = bytes(data, "utf-8").decode("utf-8",
                                                   "ignore")  # Python 3
            payload["pgn"] = base64.b64encode(
                zlib.compress(data.encode("utf-8"))).decode()
            print("Uploading compressed PGN of {} bytes".format(
                len(payload["pgn"])))
            requests.post(
                remote + "/api/upload_pgn",
                data=json.dumps(payload),
                headers={"Content-type": "application/json"},
                timeout=HTTP_TIMEOUT,
            )
        except Exception as e:
            print("\nException uploading PGN file:\n",
                  e,
                  sep="",
                  file=sys.stderr)
        try:
            os.remove(pgn_file)
        except Exception as e:
            print("Exception deleting PGN file:\n", e, sep="", file=sys.stderr)

    print("Task exited")

    return success
コード例 #8
0
ファイル: worker.py プロジェクト: glinscott/fishtest
def worker(worker_info, password, remote):
  global ALIVE

  payload = {
    'worker_info': worker_info,
    'password': password,
  }

  try:
    print('Fetch task...')
    t0 = datetime.utcnow()
    req = requests.post(remote + '/api/request_version', data=json.dumps(payload), headers={'Content-type': 'application/json'}, timeout=HTTP_TIMEOUT)
    req = json.loads(req.text)

    if 'version' not in req:
      print('Incorrect username/password')
      time.sleep(5)
      sys.exit(1)

    if req['version'] > WORKER_VERSION:
      print('Updating worker version to %s' % (req['version']))
      update()
    print("Worker version checked successfully in %ss" % ((datetime.utcnow() - t0).total_seconds()))

    t0 = datetime.utcnow()
    req = requests.post(remote + '/api/request_task', data=json.dumps(payload), headers={'Content-type': 'application/json'}, timeout=HTTP_TIMEOUT)
    req = json.loads(req.text)
  except:
    sys.stderr.write('Exception accessing host:\n')
    traceback.print_exc()
    time.sleep(random.randint(10,60))
    return

  print("Task requested in %ss" % ((datetime.utcnow() - t0).total_seconds()))
  if 'error' in req:
    raise Exception('Error from remote: %s' % (req['error']))

  # No tasks ready for us yet, just wait...
  if 'task_waiting' in req:
    print('No tasks available at this time, waiting...\n')
    # Note that after this sleep we have another ALIVE HTTP_TIMEOUT...
    time.sleep(random.randint(1,10))
    return

  success = True
  run, task_id = req['run'], req['task_id']
  try:
    pgn_file = run_games(worker_info, password, remote, run, task_id)
  except:
    sys.stderr.write('\nException running games:\n')
    traceback.print_exc()
    success = False
  finally:
    payload = {
      'username': worker_info['username'],
      'password': password,
      'run_id': str(run['_id']),
      'task_id': task_id
    }
    try:
      requests.post(remote + '/api/failed_task', data=json.dumps(payload), headers={'Content-type': 'application/json'}, timeout=HTTP_TIMEOUT)
    except:
      pass

    if success and ALIVE:
      sleep = random.randint(1,10)
      print('Wait %d seconds before upload of PGN...' % (sleep))
      time.sleep(sleep)
      if not 'spsa' in run['args']:
        try:
          with open(pgn_file, 'r') as f:
            data = f.read()
          payload['pgn'] = base64.b64encode(zlib.compress(data.encode('utf-8'))).decode()
          print('Uploading compressed PGN of %d bytes' % (len(payload['pgn'])))
          requests.post(remote + '/api/upload_pgn', data=json.dumps(payload), headers={'Content-type': 'application/json'}, timeout=HTTP_TIMEOUT)
        except:
          sys.stderr.write('\nException PGN upload:\n')
          traceback.print_exc()
    try:
      os.remove(pgn_file)
    except:
      pass
    sys.stderr.write('Task exited\n')

  return success