locked
Implementing service worker in existing mvc web application RRS feed

  • Question

  • User1554758465 posted

    I'm developing asp.Net Core MVC web application where i have this two tasks that should be running as background services:

    1. Set the user status as "Expired" if EndOfSubscription date is == DateTime.Now
    2. Before 1 month of EndOfSubscription date send a reminder E-mail to this user

    After searching, I found that I can use Service-Worker to implement this. But i'm totally confused how to use this service-worker in existing MVC web application where i need to access my models and database. Should I isolate these tasks in a separate Service Worker project? But in this case should i share the same database for both projects?

    can someone guide me with main steps in this kind of situations?

    Thank you in advance.

    Monday, October 5, 2020 5:16 PM

All replies

  • User475983607 posted

    ShahadAlshuhail

    Set the user status as "Expired" if EndOfSubscription date is == DateTime.Now

    There's no need for a service since the Expiration is just. 

    SELECT EndOfSubscription
    FROM SomeTable
    WHERE EndOfSubscription < DateTime.Now AND UserId = @Id

    Once EndOfSubscription is greater than DateTime.Now then the query will return an empty result and is expired.  There is no need to set a column to "Expired" as you have the date.

    ShahadAlshuhail

    Before 1 month of EndOfSubscription date send a reminder E-mail to this user

    This should be a scheduled database job not a service worker.

    Monday, October 5, 2020 6:04 PM
  • User-474980206 posted

    I agree there is no need for 1 as it can be determined on query.

    while a server worker (asp.net core background service) could work, it has a lot of restrictions. for example, if the asp.net core service is shutdown due to idle (common) it won't be running 

    a job scheduler like sqlagent  or cloud scheduler with web hooks is a better choice.

    Monday, October 5, 2020 6:23 PM
  • User1554758465 posted

    I'm following Code-First approach. 

    so, how we can implement reminder emails, Locking accounts if their subscription is over?

    How this could help me here?

    I'm totally agree that services has a lot of restrictions, but i'm still not finding the right path to follow in my situation.

    Monday, October 5, 2020 7:31 PM
  • User1554758465 posted

    I'm following Code-First approach. 

    how we can implement reminder emails, Locking accounts if their subscription is over?

    can we implement the scheduled database job in the web application code?

    Monday, October 5, 2020 7:33 PM
  • User475983607 posted

    I'm following Code-First approach. 

    It does not matter.

    Locking accounts if their subscription is over?

    This is a basic condition.  Simply check the expiration when the user logs in.  If the expiration has passed then don't authenticate the user.  Very simple.

    can we implement the scheduled database job in the web application code?

    Web applications are not designed for scheduled tasks.  Web apps respond to requests.

    Scheduled tasks were solved many years ago and the tools are well tested.  SQL Agent has a scheduler, Windows has a scheduler, hosting services have schedulers, etc.  Use the right tools for the job.

    There are schedulers that you can install in a web application but I would only recommend this approach if you do not have access to standard scheduling tools.  https://www.google.com/search?q=asp.net+core+schedulers

    You can also create a custom background task in a hosted service.  It is up to you to design, write the code, test, and maintain the source.  Plus the web application needs to be running.  Web sites spin down after several many minutes of nonuse. 

    Monday, October 5, 2020 8:01 PM