This repository has been archived by the owner on Jun 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Add Reschedule to FivetranSensor #69
Open
PubChimps
wants to merge
5
commits into
fivetran:main
Choose a base branch
from
PubChimps:reschedule
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -356,7 +356,7 @@ def get_last_sync(self, connector_id, xcom=""): | |
last_sync = succeeded_at if succeeded_at > failed_at else failed_at | ||
return last_sync | ||
|
||
def get_sync_status(self, connector_id, previous_completed_at): | ||
def get_sync_status(self, connector_id, previous_completed_at, reschedule_time=0): | ||
""" | ||
For sensor, return True if connector's 'succeeded_at' field has updated. | ||
:param connector_id: Fivetran connector_id, found in connector settings | ||
|
@@ -365,9 +365,10 @@ def get_sync_status(self, connector_id, previous_completed_at): | |
:param previous_completed_at: The last time the connector ran, collected on Sensor | ||
initialization. | ||
:type previous_completed_at: pendulum.datetime.DateTime | ||
:param reschedule_time: Optional, if connector is in reset state | ||
number of seconds to wait before restarting, else Fivetran suggestion used | ||
:type reschedule_time: int | ||
""" | ||
# @todo Need logic here to tell if the sync is not running at all and not | ||
# likely to run in the near future. | ||
connector_details = self.get_connector(connector_id) | ||
succeeded_at = self._parse_timestamp(connector_details["succeeded_at"]) | ||
failed_at = self._parse_timestamp(connector_details["failed_at"]) | ||
|
@@ -387,6 +388,19 @@ def get_sync_status(self, connector_id, previous_completed_at): | |
sync_state = connector_details["status"]["sync_state"] | ||
self.log.info(f'Connector "{connector_id}": sync_state = {sync_state}') | ||
|
||
# if sync in resheduled start, wait for time recommended by Fivetran | ||
# or manually specified, then restart sync | ||
if ( | ||
sync_state == "rescheduled" | ||
and connector_details["schedule_type"] == "manual" | ||
): | ||
self.log.info( | ||
f'Connector is in "rescheduled" state and needs to be manually restarted' | ||
) | ||
self.pause_and_restart( | ||
connector_details["status"]["rescheduled_for"], reschedule_time | ||
) | ||
return False | ||
# Check if sync started by FivetranOperator has finished | ||
# indicated by new 'succeeded_at' timestamp | ||
if current_completed_at > previous_completed_at: | ||
|
@@ -399,6 +413,34 @@ def get_sync_status(self, connector_id, previous_completed_at): | |
else: | ||
return False | ||
|
||
def pause_and_restart(connector_id, reschedule_for, reschedule_time): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an instance method, so the first argument should be |
||
""" | ||
While a connector is syncing, if it falls into a reschedule state, | ||
wait for a time either specified by the user of recommended by Fivetran, | ||
Then restart a sync | ||
:param connector_id: Fivetran connector_id, found in connector settings | ||
page in the Fivetran user interface. | ||
:type connector_id: str | ||
:param reschedule_for: From connector details, if schedule_type is manual, | ||
then the connector expects triggering the event at the designated UTC time | ||
:type reschedule_for: str | ||
:param reschedule_time: Optional, if connector is in reset state | ||
number of seconds to wait before restarting, else Fivetran suggestion used | ||
:type reschedule_time: int | ||
""" | ||
if reschedule_time: | ||
self.log.info(f'Starting connector again in "{reschedule_time}" seconds') | ||
time.sleep(reschedule_time) | ||
else: | ||
wait_time = ( | ||
_parse_timestamp(reschedule_for).add(minutes=1) - pendulum.now(tz="UTC") | ||
).seconds | ||
self.log.info(f'Starting connector again in "{wait_time}" seconds') | ||
time.sleep(wait_time) | ||
|
||
self.log.info("Restarting connector now") | ||
return self.start_fivetran_sync(connector_id) | ||
|
||
def _parse_timestamp(self, api_time): | ||
""" | ||
Returns either the pendulum-parsed actual timestamp or | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're missing the first argument to
pause_and_restart
(connector_id
)