Skip to content

Commit

Permalink
Add --only_enabled_servers option to spread_schedules (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-pie authored Jan 6, 2023
1 parent 8d21ff1 commit bb75d03
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 15 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Cicada scheduler

[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

*Centralized Distributed Scheduler*

- [Overview](#overview)
Expand Down
6 changes: 6 additions & 0 deletions cicada/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ def spread_schedules():
required=True,
help="List of target server_ids to spread schedules to",
)
parser.add_argument(
"--exclude_disabled_servers",
default=False,
action="store_true",
help="Exclude disabled servers from target server_ids",
)
parser.add_argument(
"--commit",
default=False,
Expand Down
8 changes: 4 additions & 4 deletions cicada/commands/spread_schedules.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ def get_last_week_schedules_by_load(db_cur, server_ids: [int] = None):
return last_week_schedules_by_load


def get_enabled_servers(db_cur, enabled_only: bool = True, server_ids: [int] = None):
def get_target_servers(db_cur, exclude_disabled, server_ids: [int] = None):
"""Get valid servers"""
sql_enabled_filter = " and is_enabled = 1" if enabled_only else ""
sql_enabled_filter = " AND is_enabled = 1" if exclude_disabled else ""
sql_server_id_filter = ""
if server_ids:
sql_server_ids = ",".join(str(server_id) for server_id in server_ids)
sql_server_id_filter = f" and server_id in ({sql_server_ids})"
sql_server_id_filter = f" AND server_id in ({sql_server_ids})"

sqlquery = f"""
SELECT server_id FROM servers
Expand Down Expand Up @@ -81,7 +81,7 @@ def main(spread_details, dbname=None):
from_server_ids = csv_to_list(spread_details["from_server_ids"])
to_server_ids = csv_to_list(spread_details["to_server_ids"])

valid_target_servers = get_enabled_servers(db_cur, server_ids=to_server_ids)
valid_target_servers = get_target_servers(db_cur, spread_details["exclude_disabled_servers"], to_server_ids)
valid_server_count = len(valid_target_servers)

if valid_server_count == 0:
Expand Down
2 changes: 1 addition & 1 deletion local-dev/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ check the [local-dev/.env](../local-dev/.env) file for the credentials.
### Running tests

``` sh
$ docker exec -it cicada_dev make pytest
$ docker exec -it cicada_dev make dev pytest
```

### Running linters
Expand Down
12 changes: 6 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name="cicada",
version="0.4.0",
version="0.4.1",
description="Lightweight, agent-based, distributed scheduler",
long_description=long_description,
long_description_content_type="text/markdown",
Expand All @@ -20,17 +20,17 @@
install_requires=[
"psycopg2-binary==2.9.5",
"pyyaml==6.0",
"croniter==1.3.7",
"croniter==1.3.8",
"tabulate==0.9.0",
"slack-sdk==3.19.1",
"slack-sdk==3.19.5",
"backoff==2.2.1",
],
extras_require={
"dev": [
"pytest==7.1.3",
"pytest==7.2.0",
"pytest-cov==4.0.0",
"black==22.10.0",
"flake8==5.0.4",
"black==22.12.0",
"flake8==6.0.0",
"freezegun==1.2.2",
]
},
Expand Down
8 changes: 6 additions & 2 deletions tests/test_functional_cli_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ def test_spread_schedules():
actual = subprocess.run(["cicada", "spread_schedules"], check=False, stderr=subprocess.PIPE).stderr.decode("utf-8")

expected = """usage: spread_schedules [-h] --from_server_ids FROM_SERVER_IDS --to_server_ids
TO_SERVER_IDS [--commit] [--force]
TO_SERVER_IDS [--exclude_disabled_servers] [--commit]
[--force]
spread_schedules: error: the following arguments are required: --from_server_ids, --to_server_ids
"""

Expand All @@ -208,7 +209,8 @@ def test_spread_schedules_help():
)

expected = """usage: spread_schedules [-h] --from_server_ids FROM_SERVER_IDS --to_server_ids
TO_SERVER_IDS [--commit] [--force]
TO_SERVER_IDS [--exclude_disabled_servers] [--commit]
[--force]
Spread schedules accross servers
Expand All @@ -218,6 +220,8 @@ def test_spread_schedules_help():
List of source server_ids to collect schedules from
--to_server_ids TO_SERVER_IDS
List of target server_ids to spread schedules to
--exclude_disabled_servers
Exclude disabled servers from target server_ids
--commit Commits changes to backend DB, otherwise only print
output
--force If schedule is moving servers and also currently
Expand Down
42 changes: 40 additions & 2 deletions tests/test_functional_spread_schedules.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ def test_create_dummy_servers():
VALUES
(1, '1', '1', '192.168.0.1', 1),
(2, '2', '2', '192.168.0.2', 1),
(3, '3', '3', '192.168.0.3', 1),
(3, '3', '3', '192.168.0.3', 0),
(4, '4', '4', '192.168.0.4', 1)
;
"""
)

results = query_test_db(f"SELECT count(*) FROM servers WHERE is_enabled=1")[0][0]
results = query_test_db(f"SELECT count(*) FROM servers")[0][0]

assert results == 4

Expand Down Expand Up @@ -172,6 +172,7 @@ def test_spread_schedules():
spread_details["to_server_ids"] = "1,2,3,4"
spread_details["commit"] = False
spread_details["force"] = False
spread_details["exclude_disabled_servers"] = False

spread_schedules.main(spread_details, pytest.db_test)

Expand Down Expand Up @@ -204,6 +205,7 @@ def test_spread_schedules_commit():
spread_details["to_server_ids"] = "1,2,3,4"
spread_details["commit"] = True
spread_details["force"] = False
spread_details["exclude_disabled_servers"] = False

spread_schedules.main(spread_details, pytest.db_test)

Expand Down Expand Up @@ -236,6 +238,7 @@ def test_spread_schedules_force():
spread_details["to_server_ids"] = "1,2,3,4"
spread_details["commit"] = True
spread_details["force"] = True
spread_details["exclude_disabled_servers"] = False

spread_schedules.main(spread_details, pytest.db_test)

Expand All @@ -259,6 +262,41 @@ def test_spread_schedules_force():
]


@freeze_time("2022-01-02 02:00:00")
def test_spread_schedules_exclude_disabled_servers():
"""test_spread_schedules_force"""
spread_details = {}

spread_details["from_server_ids"] = "1,2,3,4"
spread_details["to_server_ids"] = "1,2,3,4"
spread_details["commit"] = True
spread_details["force"] = True
spread_details["exclude_disabled_servers"] = True

spread_schedules.main(spread_details, pytest.db_test)

result = query_test_db(
f"SELECT server_id, schedule_id, is_enabled, abort_running, adhoc_execute FROM schedules ORDER BY schedule_id"
)

print(result)

assert result == [
(4, "1-1", 1, 0, 0),
(1, "1-2", 1, 1, 1),
(2, "1-3", 1, 0, 0),
(4, "1-4", 1, 0, 0),
(2, "2-1", 1, 0, 0),
(4, "2-2", 1, 0, 0),
(1, "2-3", 1, 0, 0),
(2, "2-4", 1, 0, 0),
(1, "3-1", 1, 0, 0),
(2, "3-2", 1, 0, 0),
(4, "3-3", 1, 0, 0),
(1, "3-4", 1, 0, 0),
]


def test_db_teardown():
"""test_db_teardown"""
pg_conn = psycopg2.connect(
Expand Down

0 comments on commit bb75d03

Please sign in to comment.