I have a Dot Net Core 3.1 Worker Service Project which is hosted on Azure Kubernetes using Linux Docker as a CronJob.
The worker service needs to make external API calls as per the schedule. But when the pod instantiates for execution, I get the Connection refused error.
What is more frustrating is that I have other projects running as Linux Dockers but Dot Net Core 2 API projects and they are able to make external API calls using the same code as shown below.
Below are the codes:
WorkerService:
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
try
{
await _jobService.SyncProducts();
}
catch (Exception ex)
{
_logger.LogInformation($"{ex.Message}, {ex.InnerException}, {ex.StackTrace}");
}
Environment.Exit(1);
}
}
Startup:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
services.AddHttpClient(
"JobService",
client =>
{
client.BaseAddress = new Uri($"{config.Endpoint}"); // BaseAddress
})
.AddPolicyHandler(timeoutPolicy)
.AddPolicyHandler(request => request.Method == HttpMethod.Get ? retryPolicy : noOpPolicy)
.AddPolicyHandler(throttlerPolicy)
.SetHandlerLifetime(TimeSpan.FromMinutes(config.HandlerLifeSpanInMinutes));
services.AddSingleton(typeof(IJobService), typeof(JobService));
});
Error:
Connection refused, System.Net.Sockets.SocketException (111): Connection refused
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken), at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Polly.Bulkhead.AsyncBulkheadEngine.ImplementationAsync[TResult](Func`3 action, Context context, Func`2 onBulkheadRejectedAsync, SemaphoreSlim maxParallelizationSemaphore, SemaphoreSlim maxQueuedActionsSemaphore, CancellationToken cancellationToken, Boolean continueOnCapturedContext)
at Polly.AsyncPolicy`1.ExecuteAsync(Func`3 action, Context context, CancellationToken cancellationToken, Boolean continueOnCapturedContext)
at Microsoft.Extensions.Http.PolicyHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Polly.Retry.AsyncRetryEngine.ImplementationAsync[TResult](Func`3 action, Context context, CancellationToken cancellationToken, ExceptionPredicates shouldRetryExceptionPredicates, ResultPredicates`1 shouldRetryResultPredicates, Func`5 onRetryAsync, Int32 permittedRetryCount, IEnumerable`1 sleepDurationsEnumerable, Func`4 sleepDurationProvider, Boolean continueOnCapturedContext)
at Polly.AsyncPolicy`1.ExecuteAsync(Func`3 action, Context context, CancellationToken cancellationToken, Boolean continueOnCapturedContext)
at Microsoft.Extensions.Http.PolicyHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Polly.Timeout.AsyncTimeoutEngine.ImplementationAsync[TResult](Func`3 action, Context context, CancellationToken cancellationToken, Func`2 timeoutProvider, TimeoutStrategy timeoutStrategy, Func`5 onTimeoutAsync, Boolean continueOnCapturedContext)
at Polly.Timeout.AsyncTimeoutEngine.ImplementationAsync[TResult](Func`3 action, Context context, CancellationToken cancellationToken, Func`2 timeoutProvider, TimeoutStrategy timeoutStrategy, Func`5 onTimeoutAsync, Boolean continueOnCapturedContext)
at Polly.AsyncPolicy`1.ExecuteAsync(Func`3 action, Context context, CancellationToken cancellationToken, Boolean continueOnCapturedContext)
at Microsoft.Extensions.Http.PolicyHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at SE.CAF.ETA.Workers.Agena.PIM.Sync.Services.EtaJobService.SyncAgenaProducts(Int32 page) in /src/SE.CAF.ETA.Workers.Agena.PIM.Sync/Services/EtaJobService.cs:line 34
at SE.CAF.ETA.Workers.Agena.PIM.Sync.Worker.ExecuteAsync(CancellationToken stoppingToken) in /src/SE.CAF.ETA.Workers.Agena.PIM.Sync/Worker.cs:line 38
CronJob Yaml File:
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: eta-worker-agena-pim-sync-cron-job
namespace: eta-core
spec:
schedule: "*/2 * * * *"
concurrencyPolicy: Forbid
jobTemplate:
spec:
activeDeadlineSeconds: 600
template:
metadata:
labels:
app: eta-agena-pim-sync
spec:
restartPolicy: OnFailure
containers:
- name: eta-agena-pim-sync
image: etadev.azurecr.io/worker_agena_pim_sync:latest
imagePullPolicy: Always
env:
- name: DOTNET_ENVIRONMENT
value: dev