-
Notifications
You must be signed in to change notification settings - Fork 0
added new task to dag that gets remaining api calls to github api #30
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,14 +1,21 @@ | ||||||||||||||
| import os | ||||||||||||||
| from airflow.sdk import dag, task | ||||||||||||||
| from pendulum import datetime | ||||||||||||||
| from celery import Celery | ||||||||||||||
| import os | ||||||||||||||
| from github import Auth, Github, GithubException | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| api_token, api_token_two = os.getenv("GITHUB_API_TOKEN"), os.getenv("GITHUB_API_TOKEN_SECOND_ACCOUNT") | ||||||||||||||
| auth, auth_two = Auth.Token(api_token), Auth.Token(api_token_two) | ||||||||||||||
| gh, gh_two = Github(auth=auth), Github(auth=auth_two) | ||||||||||||||
|
Comment on lines
+8
to
+10
Contributor
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. The variables
Suggested change
Comment on lines
+8
to
+10
Contributor
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. Initializing the GitHub client at the top level of the DAG file is an Airflow anti-pattern. This code is executed on every DAG parsing cycle, which is inefficient and can lead to performance issues. This logic should be moved into the |
||||||||||||||
|
|
||||||||||||||
| app = Celery( | ||||||||||||||
| 'airflow_client', | ||||||||||||||
| broker = os.getenv('CELERY_BROKER_URL'), | ||||||||||||||
| backend = os.getenv('CELERY_BACKEND_URL') | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| @dag( | ||||||||||||||
| schedule="@hourly", | ||||||||||||||
| start_date=datetime(2026, 2, 3), | ||||||||||||||
|
|
@@ -17,14 +24,28 @@ | |||||||||||||
| tags=["celery_queue"], | ||||||||||||||
| max_consecutive_failed_dag_runs=3, | ||||||||||||||
| ) | ||||||||||||||
| def run_queue(): | ||||||||||||||
| def run_github_data_queue(): | ||||||||||||||
|
|
||||||||||||||
| @task | ||||||||||||||
| def check_rate_limit(): | ||||||||||||||
| rate_limit = gh.rate_limiting | ||||||||||||||
|
Comment on lines
+30
to
+31
Contributor
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. As mentioned in my other comment, the GitHub client should be initialized within the task to follow Airflow best practices. This avoids running authentication logic during DAG parsing. For better security, it's also recommended to use Airflow Connections to manage secrets like API tokens. def check_rate_limit():
api_token = os.getenv("GITHUB_API_TOKEN")
auth = Auth.Token(api_token)
gh = Github(auth=auth)
rate_limit = gh.rate_limiting |
||||||||||||||
| print(f"Rate limit: {rate_limit[0]} remaining / {rate_limit[1]} total") | ||||||||||||||
|
Contributor
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. Using |
||||||||||||||
|
|
||||||||||||||
| return { | ||||||||||||||
| "remaining": rate_limit[0], | ||||||||||||||
| "total": rate_limit[1] | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| @task | ||||||||||||||
| def run_the_queue(): | ||||||||||||||
| app.send_task("worker.get_data_from_queue", args=[100, 500]) | ||||||||||||||
| def run_the_queue(rate_limit: str): | ||||||||||||||
|
Contributor
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. The type hint for
Suggested change
Contributor
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. |
||||||||||||||
| print(f'rate limit: {rate_limit["total"]}, remaining {rate_limit["remaining"]}') | ||||||||||||||
|
Contributor
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. |
||||||||||||||
|
|
||||||||||||||
| if rate_limit["remaining"] > 4900: | ||||||||||||||
|
Contributor
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.
Contributor
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. |
||||||||||||||
| app.send_task("worker.get_data_from_queue", args=[100, 500]) | ||||||||||||||
|
|
||||||||||||||
| run_the_queue() | ||||||||||||||
| val = check_rate_limit() | ||||||||||||||
| run_the_queue(rate_limit=val) | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| run_queue() | ||||||||||||||
| run_github_data_queue() | ||||||||||||||
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.
The
GithubExceptionimport is not used in this file. It's good practice to remove unused imports to keep the code clean and reduce unnecessary dependencies.