How to run a scheduled task from web application

Cenk 1,036 Reputation points
2022-11-09T05:50:12.523+00:00

Hi there,

I wonder if running a scheduled task from a web application is possible. This scheduled task will take some parameters and the user can start/stop it from this web application. Can it be controlled from a web interface? I hope you can help me.

I really appreciate any help you can provide.

Developer technologies ASP.NET Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2022-11-09T09:05:16.96+00:00

    Hi @enk-1028,

    I wonder if running a scheduled task from a web application is possible.

    Have you considered or used some ready-made frameworks, for example Hangfire or QuartzNET?

    This scheduled task will take some parameters and the user can start/stop it from this web application

    Taking Quartz.NET as an example, you can manually trigger tasks in a controller. Use addQuartz to add a scheduler factory and use this factory to get scheduled tasks. You can refer to this thread, which also contains an answer about passing parameters in scheduled tasks.

    Can it be controlled from a web interface?

    I'm not sure if you're referring to a custom page to control the start and stop/pause of these tasks. If this is the case, then the problem is similar to the second part, for example you want to pause the task scheduling, you can use Scheduler.PauseJob to achieve it when using QuartzNET.

    Best regards,
    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Cenk 1,036 Reputation points
    2022-11-16T08:31:23.367+00:00

    Hi @Anonymous ,

    I have this ASP.NET Core 2.1 web application. The admin gets online game codes from the page below. Selecting the game (Razer Gold Pin), quantity, and email to send those codes.

    A Rest API call is made for the purchase. There is a limit for purchasing, a maximum of 200 purchases can be made at one time.

    At first, there wasn't much to buy so there was no problem. But the demand has increased, when there are 10 thousand, 20 thousand purchases, it is necessary to purchase from this screen for hours. I wonder if can we make a large number of purchases without waiting in front of the screen with scheduling?

    260863-yql9f.png

    Here is the Javascript in the cshtml:

       $("#btn_add").html(  
                                    '<span class="spinner-border spinner-border-sm" role="status" id="spinner" aria-hidden="true"></span> Loading...'  
                                );  
                                var selText = $("#validationCustom04").val();  
                                var gameName = $("#validationCustom04 option:selected").text();  
                                var quantity = $("#quantity").val();  
                                var email = $("#email").val();  
                                var price = $("#total").val();  
                                var company = $("#validationCustom05").val();  
          
                                if ($("#total").val() !== '') {  
          
                                    price = $("#total").val();  
          
                                }  
          
                                var serviceUrl = '/GameBanks/A101PinAsync?quantity=' + quantity + '&game=' + selText + '&email=' + email + '&prc=' + price + '&gameName=' + gameName + '&company=' + company;  
                                $.ajax({  
                                    type: "GET",  
                                    url: serviceUrl,  
                                    dataType: 'json',  
                                    success: function (data) {  
                                        //alert(JSON.stringify(data));  
                                        ShowResult(data);  
                                        $("#spinner").remove();  
                                        $("#btn_add").html('Add');  
          
                                    }, error: function (xhr, status, error) {  
                                        $("#spinner").remove();  
                                        $("#btn_add").html('Add');  
          
                                        var errorMessage = xhr.responseText;  
          
                                        alert('Error - ' + errorMessage);  
                                    }  
          
                                });  
    

    Here is the controller method:

        [HttpGet]  
        public async Task<IActionResult> A101PinAsync(int quantity, string game, string email, int prc, string gameName, string company)  
        {  
            var price = 0;  
          
            try  
            {  
                string result = null;  
                          
                var dList = new DemoList();  
          
                if (prc > 0)  
                {  
                    price = prc;  
                }  
          
                var games = new Game { Product = game, Quantity = quantity };  
          
                var content = await games.CallGameAsync("Test", "12345", game, quantity, company);  
          
                var deserializedResult = JObject.Parse(content);  
          
                var root = JsonConvert.DeserializeObject<Root>(content);  
          
                if ((string)deserializedResult["coupons"]?[0]?["Serial"] == null)  
                {  
                    result = result + gameName + ":" + (string)deserializedResult["Message"] + "\n";  
                }  
                else  
                {  
                    foreach (var coupon in root.coupons)  
                    {  
                        result = result + gameName + "  Serial:" + coupon.Serial + "  Pin:" + coupon.Pin + "\n";  
                    }  
                }  
                         
                dList.gameList = result;  
                          
                // NLOG  
                NLogPin(logger, User.Identity.Name, DateTime.Now, result, email);  
                          
                return new JsonResult(dList);  
            }  
            catch (Exception e)  
            {  
                // NLOG  
                NLog(logger2, "OyunPalas " + e.Message, DateTime.UtcNow,0);  
                          
                return StatusCode(500,e.Message);  
            }  
        }  
      
      
    Here is the Web API calling method:  
        public class Game  
        {  
            public string Username { get; set; }  
            public string Password { get; set; }  
            public string Product { get; set; }  
            public int Quantity { get; set; }  
            public string ShopNo { get; set; }  
          
            private static readonly Logger logger2 = LogManager.GetLogger("exceptionFile");  
          
            public async Task<string> CallGameAsync(string username, string password, string product, int quantity, string company)  
            {  
                try  
                {  
                    HttpWebRequest request = null;  
                      
                    request = (HttpWebRequest)WebRequest.Create("http://111.111.111.111:1907//api/v2/web/purchase");  
          
                    var svcCredentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));  
                      
                    request.Headers.Add("Authorization", "Basic " + svcCredentials);  
                    request.Method = "POST";  
                    request.KeepAlive = false;  
                    request.ContentType = "application/x-www-form-urlencoded";  
          
                    var content = new FormUrlEncodedContent(new[]  
                    {  
                        new KeyValuePair<string, string>("productCode", product),  
                        new KeyValuePair<string, string>("quantity", quantity.ToString()),  
                        new KeyValuePair<string, string>("shopNo", company),  
                        new KeyValuePair<string, string>("safeNo", company),  
                        new KeyValuePair<string, string>("cashierNo", company)  
                    });  
          
                    var urlEncodedString = await content.ReadAsStringAsync();  
          
                    using (var streamWriter = new StreamWriter(await request.GetRequestStreamAsync()))  
                    {  
                        await streamWriter.WriteAsync(urlEncodedString);  
                    }  
          
                    var httpResponse = (HttpWebResponse) (await request.GetResponseAsync());  
          
                    var response = new HttpResponseMessage  
                    {  
                        StatusCode = httpResponse.StatusCode,  
                        Content = new StreamContent(httpResponse.GetResponseStream()),  
                    };  
          
                    //Read response  
                    var htmlResponse = await response.Content.ReadAsStringAsync();  
          
                    return htmlResponse;  
                }  
                catch (Exception e)  
                {  
                    //NLOG  
                    NLog(logger2, "Test" + e.Message);  
                    throw;  
                }  
            }  
          
            public void NLog(Logger logger, string user)  
            {  
                var sb = new StringBuilder();  
                sb.AppendLine("Test: " + user);  
                logger.Info(sb.ToString());  
            }  
        }  
    

    I have to pass product (game type) to the job. I read the https://www.quartz-scheduler.net/documentation/quartz-3.x/tutorial/more-about-jobs.html#jobdatamap documentation but not fully understand how to use in my case. And I wonder if I can stop/cancel the job from this page before its end time?

    I really appreciate any help you can provide. I'm sorry if the question is too silly as I have no experience with this scheduling.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.