question

SaudKhan-7420 avatar image
0 Votes"
SaudKhan-7420 asked AgaveJoe answered

How to register same event for different times ASP.MVC C#

I'm building an Auction Web App in Asp.Net MVC and I have an auction expiration time for each auction item. It means whenever that time will reach the auction will be ended for that particular item and a flag of Auction Ended will be set in the database for the particular auction item. Now I'm struggling with how I can initialize a timer or background job or something like that for each auction item so whenever the expiration time will reach it will change the item status to Auction Ended immediately in the database. I have worked with Schedulers and Timers in the Winforms but I think it's not a better idea to have a separate scheduler for each auction item.
Looking forward to any suggestion and idea.
Thanks

dotnet-csharpdotnet-aspnet-mvc
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @SaudKhan-7420 ,
At the same time, are there multiple auction items start or end? Single-threaded process or multi-threaded process?And I think,you could set the start time and the end time.
Best regards,
Yijing Sun

0 Votes 0 ·

Yes, there're multiple auction items and each item will have different start and end date/time

0 Votes 0 ·
AgaveJoe avatar image
0 Votes"
AgaveJoe answered AgaveJoe commented

The database design should include the auction end date and time. On the UI side you can write a count down timer for which there are many examples online.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You are right, I already have an end date in the database also a counter on the front-end for the end-user. But the issue is that if there will no user activity on the application then the auction should end on its time.
On the front-end, I have initialized count down for each item and when the time is finished for any item it triggers a backend request to make that item ENDED. But I want the same thing on the backend as well.

0 Votes 0 ·

On the front-end, I have initialized count down for each item and when the time is finished for any item it triggers a backend request to make that item ENDED.

This design does not make sense if you have an end date and time in the database. The auction is over if the current date and time, GETDATE(), is greater than the value stored in the database. There's no logical reason for the countdown timer running in a browser to end the auction. The countdown timer is only for the user's experience.

0 Votes 0 ·
JoseZero-8614 avatar image
0 Votes"
JoseZero-8614 answered JoseZero-8614 edited

Did you considered Background Jobs using Quartz or HangFire?
www.quartz-scheduler.net
www.hangfire.io


Reviewed my answer to include FrontEnd alternative.
Since it is an webapp in ASP.NET MVC looks you can have Background Jobs running on server, that controls when Auction Start/End, and SignalR at client FrontEnd, so Background Job can notify client about Start/End auction.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Why add the extra complexity of a timer to set a flag when you can simply set the auction end date and time in a table? The timer must check the end date and time to flip a flag. Any query needs to check the flag. It is much simpler to compare the DateTime column to the current date and time. This removes the need for a timer and flag column.

0 Votes 0 ·

I know about the Crone job but here's the issue is that every auction item has its own Start and EndDaate which needs to be monitored separately so that if any of them ends a trigger/function call will happen immediately so that it can notify the user's also set the flag in the database along select the top bidder of it.

0 Votes 0 ·

Croned Jobs doesn´t fit your needs.
Quartz and Hangfire are just an option to load many Background Jobs and Schedule in different ways, or just start/stop on demand.

But I have to tell you, doesn´t matter you choose Timer, Background Job, Task, Quartz or Hangfire, you have to consider Application Pool Recycle, of course you can set it to run at specific time.
why-is-the-iis-default-app-pool-recycle-set-to-1740-minutes


0 Votes 0 ·
AgaveJoe avatar image
0 Votes"
AgaveJoe answered

It seems you made up your mind before making this post even though you initially asked for suggestions and ideas.

In my opinion, you are over complicating the design. Replace the timer and the flag column with a simple WHERE clause. The same WHERE logic that the timer must use to set the flag. Take a look at this basic SQL example.

 IF OBJECT_ID(N'tempdb..#Auction') IS NOT NULL
     DROP TABLE #Auction
    
 IF OBJECT_ID(N'tempdb..#AuctionBids') IS NOT NULL
     DROP TABLE #AuctionBids
    
 CREATE TABLE #Auction(
     AuctionId    INT IDENTITY(1,1),
     Item        VARCHAR(10),
     StartDate    DATETIME,
     EndDate        DATETIME
 )
    
 CREATE TABLE #AuctionBids(
     AuctionBidId    INT IDENTITY(1,1),
     AuctionId        INT,
     BidValue        DECIMAL
 )
    
 INSERT INTO #Auction(Item, StartDate, EndDate)
 VALUES ('Widget', DATEADD(second, -1, GETDATE()), DATEADD(second, 10, GETDATE()))
    
 INSERT INTO #AuctionBids(AuctionId, BidValue)
 VALUES(1, 10.00)
    
    
    
 INSERT INTO #AuctionBids(AuctionId, BidValue)
 SELECT a.AuctionId, 11.00
 FROM #Auction AS a
 WHERE a.AuctionId = 1 AND GETDATE() BETWEEN a.StartDate AND a.EndDate

A bid cannot be inserted if the auction end date has passed. Your design uses the same concept but sets a flag by DateTime with a timer. Why add all the extra logic when a simple SQL query using GETDATE() or DateTime.Now determines if the auction has ended?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.