Search code examples
asp.netlong-running-processesbackground-task

ASP.NET sync long process w/ Requirements


I am working with an e-commerce platform, and I have a task to synchronize with some remote accounting software. The task requires syncing orders, products, inventory...etc. With large amounts of data being synced,the process can take awhile. So, I don't think asp.net application would be the best place to handle this. So, the requirements are:

  1. To be able to schedule this process to run overnight
  2. To be able to manually fire off this process and pass into it some variables like order numbers to export.
  3. Possibly get back status info when fired off manually.
  4. Has to work on .net 3.5

Issues: Can't use a windows service because the site is hosted remotely on a shared service, and the host won't allow a service.

Ideas: I'm having a really hard time finding the best way to handle this outside asp.net that fits all requirements, but I do have access to their FTP and thought possibly a console app that hosts a web-service may work, and I can put Quartz scheduler in global file to fire off service from the site.

Anyway, please offer some thoughts and experiences if you have them on which methods have worked for you.


Solution

  • Can't use a windows service because the site is hosted remotely on a shared service, and the host won't allow a service.

    That might be a problem. Does this hosting service provide any other kind of scheduling functionality? If not then you may need to consider changing your hosting services.

    You're correct in that ASP.NET is not the tool you'd use for scheduling tasks. A web application is a request/response system (and is very much at the mercy of the hosting process, IIS usually for ASP.NET). So you need some way to schedule the task to execute at regular intervals. Windows Services, Windows Task Scheduler, or some other task scheduling tool.

    As for the requirement to be able to invoke the process manually, that's a simple matter of separating the invocation of the logic from the logic itself. Picture the following components:

    • A module which performs the logic, not bound to any UI or any way of invoking it. Basically a Class Library project (or part of one).
    • A Windows Service or Console Application which references the Class Library and invokes the logic.
    • A Web Application which references the Class Library and invokes the logic.

    Once you've sorted out how to schedule the Console Application, just schedule it and it's all set. If the process returns some information then the Console Application can also perform any notifications necessary to inform people of that information.

    The Web Application can then also have an interface somewhere to invoke the process manually. Since the process "can take a while" then of course you won't want the interface to wait for it to complete. This can result in timeouts and leave the system in an unknown state. Instead you'd want to return the UI to the user indicating that the process has started (or been queued) and that they will be notified with the results when it completes. There are a couple of options for this...

    • You can use a BackgroundWorker to actually invoke the process. When the process completes, send a notification to the user who invoked it.
    • You can write a record to a database table to "queue" the process and have something like a Windows Service or scheduled Console Application (same scenario as above) which regularly polls that table for queued tasks, performs the task, and sends the notification. (Of course updating the status in the table along the way so it doesn't perform it twice.)

    There are pros and cons either way, it's really up to you how you'd like to proceed. Ultimately you're looking at two main things here:

    1. Separate the logic itself from the scheduling/invocation of the logic.
    2. Utilize a scheduling system to schedule tasks. (If your hosting provider doesn't have one, find one that does.)