I’m having trouble with a Django scheduled task that connects to Spotify’s Web API. The task runs every few minutes using django-crontab to manage music playlists, but I keep getting socket errors.
The Issue:
My scheduled task fails with this error message:
ERROR 2024-09-06 01:11:02,768 cron Failed to update playlist: [Errno 98] Address already in use
What I’ve Done So Far:
- Tested the Spotify API calls manually and they work fine
- Added retry logic for common API errors like rate limits
- Checked running processes with lsof command
- Made sure only one cron instance runs at a time
My Code:
def update_playlist_tracks(task, playlist_id, song_uris, spotify_client, account):
max_attempts = 3
wait_time = 5
USER_NAME = account.spotify_credentials.user_id
logger.info(f"Updating playlist {playlist_id} for user {USER_NAME}")
for try_count in range(max_attempts):
try:
logger.debug(f"Adding {len(song_uris)} songs to playlist, try {try_count + 1}")
spotify_client.user_playlist_add_tracks(USER_NAME, playlist_id, song_uris)
logger.info(f"Successfully updated playlist {playlist_id} with {len(song_uris)} tracks")
mark_task_complete(task)
return
except SpotifyException as spotify_error:
logger.error(f"Spotify error: {spotify_error.msg}")
if spotify_error.http_status == 403:
logger.error(f"Permission denied for user {USER_NAME}")
return
if spotify_error.http_status == 429:
wait_seconds = int(spotify_error.headers.get('Retry-After', wait_time))
logger.info(f"Rate limited, waiting {wait_seconds} seconds")
time.sleep(wait_seconds)
except OSError as os_error:
if os_error.errno == 98:
logger.error(f"Failed to update playlist: Address already in use. Waiting {wait_time} seconds.")
time.sleep(wait_time)
else:
logger.error(f"System Error: {os_error}")
time.sleep(wait_time)
except Exception as general_error:
logger.error(f"Failed to update playlist: {general_error}")
time.sleep(wait_time)
logger.error(f"Could not update playlist {playlist_id} after {max_attempts} tries")
Cron Configuration:
CRONJOBS = [
('*/5 * * * *', 'myapp.tasks.cron.run_playlist_update'),
]
CRONTAB_LOCK_JOBS = True
System Info:
- Ubuntu 20.04
- Django (latest version)
- Python 3.12
- Using spotipy library for Spotify API
The error happens right when trying to call the Spotify API function. Everything works fine until that point. I can see from the logs that the authentication and setup work correctly.
Questions:
- Why am I getting this socket error when calling the Spotify API from a cron job?
- How can I fix this network issue in my scheduled task?
- Are there better ways to handle API calls in Django cron jobs?