<p>So I have solution for above problem:</p>
<p>Since I was using http request with r_script to call Api's and docker was running for longer time to perform certain database calculations, I ended up with the time out after 240 second due to limitations of http request.</p>
<p>Solution: So my simple solution was to make r_script request API asynchronous so that my API's will respond immediately with response and docker will keep running in background to perform its action.</p>
<p>Status: There are 2 ways to check status of the request. First is to use database logs as we are performing calculation logic in database and second is to create another api to check the status of the API.</p>
<p>Note: We have similar things available in durable functions which responds immediately, however it also returns url to check the status of the call</p>

CC BY-SA 4.0
2024-10-08T07:10:56.717Z
2024-10-08T07:10:56.717Z
Techiemanu
<p>Summary ToDo : I am trying to run r-script in docker which is having some calculation logic interacting to database. This script run for around one hour.</p>
<p>What I have tried : I have tried to use azure durable function which triggers r-script in docker and here is the example to do so:</p>
<pre><code>[FunctionName(nameof(RunRScript))]
    public static async Task&lt;string&gt; RunRScript([ActivityTrigger] string name, ILogger log)
    {
        log.LogInformation($&quot;Starting R script execution...&quot;);

        // Command to run the R script in the Docker container
        var process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = &quot;docker&quot;, 
                Arguments = &quot;run -p 80:80 testing.azurecr.io/test_docker:latest&quot;,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true
            }
            
        };

        process.Start();

        string output = await process.StandardOutput.ReadToEndAsync();
        string error = await process.StandardError.ReadToEndAsync();

        process.WaitForExit();

        if (process.ExitCode != 0)
        {
            log.LogError($&quot;R script execution failed: {error}&quot;);
            throw new Exception($&quot;R script execution failed: {error}&quot;);
        }

        log.LogInformation($&quot;R script execution completed successfully: {output}&quot;);
        return output;
    }

    [FunctionName(&quot;Function1_HttpStart&quot;)]
    public static async Task&lt;HttpResponseMessage&gt; HttpStart(
        [HttpTrigger(AuthorizationLevel.Anonymous, &quot;get&quot;, &quot;post&quot;)] HttpRequestMessage req,
        [DurableClient] IDurableOrchestrationClient starter,
        ILogger log)
    {
        // Function input comes from the request content.
        string instanceId = await starter.StartNewAsync(&quot;Function1&quot;, null);

        log.LogInformation(&quot;Started orchestration with ID = '{instanceId}'.&quot;, instanceId);

        return starter.CreateCheckStatusResponse(req, instanceId);
    }
</code></pre>
<p>Problem: So problem here is that I am able to run FunctionApp in my development machine however it failing to run once deployed to azure function app.</p>
<p>Limitations: I am not allowed to use VMs due to cost issues</p>

<azure>
Running Rscript using Azure durable functions in Azure enviornment - No Virtual machine
Techiemanu
# Running Rscript using Azure durable functions in Azure enviornment - No Virtual machine

(Asked by: username_0 on 2024-09-11)

Summary ToDo : I am trying to run r-script in docker which is having some calculation logic interacting to database. This script run for around one hour.
What I have tried : I have tried to use azure durable function which triggers r-script in docker and here is the example to do so:
[FunctionName(nameof(RunRScript))]
    public static async Task RunRScript([ActivityTrigger] string name, ILogger log)
    {
        log.LogInformation($"Starting R script execution...");

        // Command to run the R script in the Docker container
        var process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "docker", 
                Arguments = "run -p 80:80 testing.azurecr.io/test_docker:latest",
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true
            }
            
        };

        process.Start();

        string output = await process.StandardOutput.ReadToEndAsync();
        string error = await process.StandardError.ReadToEndAsync();

        process.WaitForExit();

        if (process.ExitCode != 0)
        {
            log.LogError($"R script execution failed: {error}");
            throw new Exception($"R script execution failed: {error}");
        }

        log.LogInformation($"R script execution completed successfully: {output}");
        return output;
    }

    [FunctionName("Function1_HttpStart")]
    public static async Task HttpStart(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestMessage req,
        [DurableClient] IDurableOrchestrationClient starter,
        ILogger log)
    {
        // Function input comes from the request content.
        string instanceId = await starter.StartNewAsync("Function1", null);

        log.LogInformation("Started orchestration with ID = '{instanceId}'.", instanceId);

        return starter.CreateCheckStatusResponse(req, instanceId);
    }

Problem: So problem here is that I am able to run FunctionApp in my development machine however it failing to run once deployed to azure function app.
Limitations: I am not allowed to use VMs due to cost issues

**Tags:** azure


---

## 1 answers

### Answer by username_0 on 2024-10-08. Score: 0

So I have solution for above problem:

Since I was using http request with r\_script to call Api's and docker was running for longer time to perform certain database calculations, I ended up with the time out after 240 second due to limitations of http request.

Solution: So my simple solution was to make r\_script request API asynchronous so that my API's will respond immediately with response and docker will keep running in background to perform its action.

Status: There are 2 ways to check status of the request. First is to use database logs as we are performing calculation logic in database and second is to create another api to check the status of the API.

Note: We have similar things available in durable functions which responds immediately, however it also returns url to check the status of the call


---

