Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Services.Hive/3.3/Scheduler/TaskScheduler.cs

Last change on this file was 17586, checked in by mkommend, 5 years ago

#2521: Merged trunk changes into problem refactoring branch.

File size: 1.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.Services.Hive.DataAccess;
4using HeuristicLab.Services.Hive.DataAccess.Interfaces;
5
6
7namespace HeuristicLab.Services.Hive {
8  public abstract class TaskScheduler : ITaskScheduler {
9    protected IPersistenceManager PersistenceManager => ServiceLocator.Instance.PersistenceManager;
10
11    public IEnumerable<Guid> Schedule(Slave slave, int count = 1) {
12      return PersistenceManager.UseTransactionAndSubmit(() => {
13        var ids = ScheduleInternal(slave, count);
14        foreach (var id in ids) AssignTask(slave, id);
15        return ids;
16      });
17    }
18
19    protected abstract IReadOnlyList<Guid> ScheduleInternal(Slave slave, int count);
20
21    private void AssignTask(Slave slave, Guid taskId) {
22      const TaskState transferring = TaskState.Transferring;
23      DateTime now = DateTime.Now;
24
25      var pm = PersistenceManager;
26      var taskDao = pm.TaskDao;
27      var stateLogDao = pm.StateLogDao;
28
29      var task = taskDao.GetById(taskId);
30      task.State = transferring;
31      task.LastHeartbeat = now;
32
33      stateLogDao.Save(new StateLog {
34        State = transferring,
35        DateTime = now,
36        TaskId = taskId,
37        SlaveId = slave.ResourceId,
38        UserId = null,
39        Exception = null
40      });
41    }
42  }
43}
Note: See TracBrowser for help on using the repository browser.