morleyc
morleyc

Reputation: 2451

Entry point for DDD application service call which should be called on a timer

With my DDD model I have application services which host particular user stories and allow repositories/entities/domain-serices to be organised - as I understand this is a usual part of domain driven design layering as below:

DDD layers

I need to call an application service on a timed schedule, this schedule is defined in the model itself obtained from the jobs repository. A comment on this post kindly mentioned to have repository return GetPendingJobs() with a list of jobs to run.

Each job takes a cron job string, and will call a Application Service (for example SyncAccounts).

My questions are:

  1. I know we could poll this on a thread, but where would this sit, in an entity domain service, or as something external to the domain model (perhaps a layer between application and the presentation)?

  2. As mentioned on the comment with the jobs repository returning GetPendingJobs(), if this is based on the current date/time, is it the responsibility of the repository to know when to run, or of the Job entity (checking the current date time against its cronjob string)?

Code below:

namespace DomainServices
{
    public class JobSchedule
    {
        private readonly JobsRepository repo;

        JobSchedule(JobsRepository repo)
        {
            this.repo = repo;
        }

        public void Poll()
        {
            var jobs = this.repo.GetAllJobs();

            foreach(var job in jobs)
            {
                if(job.IsTimeToRun())
                    job.Run();      
            }
        }
    }
}

Upvotes: 0

Views: 1799

Answers (1)

eulerfx
eulerfx

Reputation: 37739

  1. This logic is an operational concern and thus belongs in the application tier. This however can be implemented in a variety of ways depending on the nature of the jobs. Will the jobs need to be run in their own process? If so, then you may want to go with a windows service for hosting. If the jobs can be run as part of the main application process, the runner can itself be a background application service.

  2. The GetPendingJobs method can return jobs based on the current or a provided time in which case it effectively knows which jobs must run when. However, the canonical information is associated with the job itself - the repository just queries it.

Upvotes: 1

Related Questions