Coravel Scheduler Invocable: MySQL Connection Error 20004

33 Views Asked by At

I created an Invocable class using Coravel to Schedule a background task in a .Net 6 API project.

I’m encountering a connection error (20004) from the Scheduler class (Coravel Invocable) while connecting to a MySQL database. Surprisingly, API requests within the same application work flawlessly with the same DBContext and the same connection string.

The hosting environment is Google Cloud Run and the database is Google Cloud SQL (MySQL)


public class ProcessScheduledSurvey : IInvocable
{
    private readonly IServiceScopeFactory _serviceScopeFactory;

    public ProcessScheduledSurvey(IServiceScopeFactory serviceScopeFactory)
    {
        _serviceScopeFactory = serviceScopeFactory;
    }

    public async Task Invoke()
    {
        try
        {
            using var scope = _serviceScopeFactory.CreateScope();
            var _dbContext = scope.ServiceProvider.GetRequiredService<IApplicationDbContext>();

            // Get the list await launch
            var projectIds = await _dbContext.SurveySettings.Where(x => x.ScheduleLaunch && !x.LaunchDate.HasValue && x.ScheduleTargetDate.HasValue && x.ScheduleTargetDate.Value <= DateTime.UtcNow).Select(x => x.ProjectId).ToListAsync();

            if (projectIds.Any())
            {
                var _mediatr = scope.ServiceProvider.GetRequiredService<IMediator>();
                int processed = 0;
                int limit = 100;
                while (processed < projectIds.Count())
                {
                    var readyList = projectIds.Skip(processed).Take(limit);

                    if (readyList.Any())
                    {
                        foreach (var projectId in readyList)
                        {
                            try
                            {
                                await _mediatr.Send(new LaunchSurveyCommand()
                                {
                                    ProjectId = projectId,
                                    LaunchAction = SurveyLaunchActionEnum.Launch,
                                    UpdatedBy = "SYSTEM"
                                });
                            }
                            catch (Exception ex)
                            {
                                Log.Error(ex, "ProcessScheduledSurvey {ProjectId}", projectId);
                            }
                        }
                    }

                    processed += readyList.Count();
                }
            }
        }
        catch (Exception ex)
        {
            Log.Error(ex, "ProcessScheduledSurvey");
        }
    }
}

The scheduler is regisered as follows:


        var provider = app.ApplicationServices;
        provider.UseScheduler(scheduler =>
        {
            scheduler.OnWorker("ProcessScheduledSurvey");
            scheduler.Schedule<ProcessScheduledSurvey>().Hourly().PreventOverlapping("ProcessScheduledSurvey");
        })
        .OnError((exception) =>
        {
            Log.Error("Coravel: {@Exception}", exception);
        })
        .LogScheduledTaskProgress(provider.GetService<ILogger<IScheduler>>());

Problem Summary:

  • Coravel Invocable throws a connection error (20004) during initialization.

  • The same connection string works perfectly for API requests in the same application.

Any insights or guidance would be greatly appreciated!

0

There are 0 best solutions below