예제 #1
0
def test_args_set_clear(capsys, tmp_work_dir, freezer):
    freezer.move_to(TEST_DATESTR)
    queries.set_flag("foo", "bar")
    flags.run(["set", "foo="])
    stdout, stderr = capsys.readouterr()
    assert stdout == f"foo=None ({TEST_DATESTR})\n"
    assert stderr == ""
    assert queries.get_flag("foo").value is None
예제 #2
0
def test_maintenance_mode_off(mock_subprocess_run, db):
    ps = add_maintenance_command(mock_subprocess_run, current=None)
    ps.stdout = ""
    assert service.maintenance_mode() is None

    queries.set_flag("mode", "db-maintenance")
    ps = add_maintenance_command(mock_subprocess_run, current="db-maintenance")
    ps.stdout = ""
    assert service.maintenance_mode() is None
예제 #3
0
def test_set_flag(tmp_work_dir):
    assert get_flag_value("foo") is None
    ts1 = set_flag("foo", "bar").timestamp
    assert get_flag_value("foo") == "bar"
    time.sleep(0.01)
    ts2 = set_flag("foo", "bar").timestamp
    # check timestamp has not changed
    assert ts1 == ts2
    set_flag("foo", None)
    assert get_flag_value("foo") is None
예제 #4
0
def test_args_get(capsys, tmp_work_dir):
    flags.run(["get", "foo"])
    stdout, stderr = capsys.readouterr()
    assert stdout == "foo=None (never set)\n"
    assert stderr == ""

    queries.set_flag("foo", "bar", TEST_TIME)
    flags.run(["get", "foo"])
    stdout, stderr = capsys.readouterr()
    assert stdout == f"foo=bar ({TEST_DATESTR})\n"
    assert stderr == ""
예제 #5
0
def test_maintenance_mode_on(mock_subprocess_run, db):
    ps = add_maintenance_command(mock_subprocess_run, current=None)
    ps.stdout = "db-maintenance"
    ps.stderr = "other stuff"
    assert service.maintenance_mode() == "db-maintenance"

    queries.set_flag("mode", "db-maintenance")
    ps = add_maintenance_command(mock_subprocess_run, current="db-maintenance")
    ps.stdout = "db-maintenance"
    ps.stderr = "other stuff"
    assert service.maintenance_mode() == "db-maintenance"
예제 #6
0
def main(action, flags, create=False):
    try:
        get_current_flags()
    except sqlite3.OperationalError as e:
        if "no such table" in str(e):
            if create:
                create_table(get_connection(), Flag)
                get_current_flags()
            else:
                sys.exit(
                    "The flags table does not exists. Run command again with --create to create it."
                )

    flags_to_show = []

    if action == "set":
        for name, value in flags:
            flag = set_flag(name, value)
            flags_to_show.append(flag)

    else:  # action == "get"
        if flags:
            for f in flags:
                try:
                    flag = get_flag(f)
                except ValueError:
                    flag = Flag(f, None, None)
                flags_to_show.append(flag)
        else:
            flags_to_show = get_current_flags()

    for flag in flags_to_show:
        print(flag)
예제 #7
0
def maintenance_mode():
    """Check if the db is currently in maintenance mode, and set flags as appropriate."""
    # This did not seem big enough to warrant splitting into a separate module.
    log.info("checking if db undergoing maintenance...")
    current = get_flag_value("mode")
    ps = docker(
        [
            "run",
            "--rm",
            "-e",
            "DATABASE_URL",
            "ghcr.io/opensafely-core/cohortextractor",
            "maintenance",
            "--current-mode",
            str(current),
        ],
        env={"DATABASE_URL": config.DATABASE_URLS["full"]},
        check=True,
        capture_output=True,
        text=True,
    )
    last_line = ps.stdout.strip().split("\n")[-1]
    if "db-maintenance" in last_line:
        if current != "db-maintenance":
            log.warning("Enabling DB maintenance mode")
        else:
            log.warning("DB maintenance mode is currently enabled")
        set_flag("mode", "db-maintenance")
    else:
        if current == "db-maintenance":
            log.info("DB maintenance mode had ended")
        set_flag("mode", None)

    mode = get_flag_value("mode")
    log.info(f"db mode: {mode}")
    return mode