Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send one heartbeat on scheduling service startup anyway #3014

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions connectors/protocol/connectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,9 +734,10 @@ def sync_cursor(self):
def api_key_secret_id(self):
return self.get("api_key_secret_id")

async def heartbeat(self, interval):
async def heartbeat(self, interval, force=False):
if (
self.last_seen is None
force
or self.last_seen is None
or (datetime.now(timezone.utc) - self.last_seen).total_seconds() > interval
):
self.log_debug("Sending heartbeat")
Expand Down
7 changes: 6 additions & 1 deletion connectors/services/job_scheduling.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ def __init__(self, config):
self.idling = self.service_config["idling"]
self.heartbeat_interval = self.service_config["heartbeat"]
self.source_list = config["sources"]
self.first_run = True
self.last_wake_up_time = datetime.now(timezone.utc)

async def _schedule(self, connector):
# To do some first-time stuff
just_started = self.first_run
self.first_run = False

if self.running is False:
connector.log_debug("Skipping run because service is terminating")
return
Expand All @@ -66,7 +71,7 @@ async def _schedule(self, connector):
raise

# the heartbeat is always triggered
await connector.heartbeat(self.heartbeat_interval)
await connector.heartbeat(self.heartbeat_interval, force=just_started)

connector.log_debug(f"Status is {connector.status}")

Expand Down
22 changes: 22 additions & 0 deletions tests/services/test_job_scheduling.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,25 @@ def _source_klass(config):
data_source_mock.close.assert_awaited_once()

connector.error.assert_awaited_with(error)


@pytest.mark.asyncio
async def test_initial_loop_run_heartbeat_only_once(
connector_index_mock, sync_job_index_mock, set_env
):
connector = mock_connector(next_sync=None)
connector_index_mock.supported_connectors.return_value = AsyncIterator(
[connector, connector, connector, connector]
) # to emulate 4 runs
await create_and_run_service(JobSchedulingService)

connector.prepare.assert_awaited()
connector.heartbeat.assert_awaited()
assert connector.heartbeat.call_count == 4
# Only the first call to heartbeat should pass "True"
assert connector.heartbeat.call_args_list[0].kwargs["force"] is True
assert connector.heartbeat.call_args_list[1].kwargs["force"] is False
assert connector.heartbeat.call_args_list[2].kwargs["force"] is False
assert connector.heartbeat.call_args_list[3].kwargs["force"] is False
connector.update_last_sync_scheduled_at_by_job_type.assert_not_awaited()
sync_job_index_mock.create.assert_not_awaited()
Loading