Пример #1
0
        /// <summary>
        /// Add a job to the database
        /// </summary>
        /// <param name="job">The job to add</param>
        public void AddJob(Job job)
        {
            if (job == null)
                throw new ArgumentNullException("job was null");
            if(job.User == null)
                throw new ArgumentNullException("job.User was null");

            Debug.WriteLine("1 - Inside AddJob(); jobId: " + job.JobId);

            if (job.UserId == 0)
            {
                AddUser(job.User);
            }
            else if (job.JobId == 0)
            {
                lock (_objectLock)
                {
                    _dbContext.Jobs.AddObject(job);

                    Debug.WriteLine("2 - Inside AddJob(); jobId: " + job.JobId);

                    _dbContext.SaveChanges();
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Add a log to the database
        /// </summary>
        /// <param name="job">The job affecting the log</param>
        /// <param name="time">The timestamp for the log</param>
        public void AddLog(Job job, DateTime time)
        {
            if (job == null)
                throw new ArgumentNullException("job was null");
            if (job.User == null)
                throw new ArgumentNullException("job.User was null");
            if (job.JobId == 0)
                throw new ArgumentNullException("job.JobId was 0");
            if (time == null)
                throw new ArgumentNullException("time was null");

            lock (_objectLock)
            {
                var dbContext = new TaskManagerModelContainer();
                Debug.WriteLine("1 - Inside Log.AddJob(); Event: " + job.StateString + "; jobId: " + job.JobId );
                Log log = new Log();
                log.Event = job.StateString;
                log.Time = time;
                log.JobId = job.JobId;

                dbContext.Logs.AddObject(log);
                Debug.WriteLine("2 - Inside Log.AddJob(); State: " + job.StateString + "; jobId: " + job.JobId + "; logId: " + log.LogId);
                dbContext.SaveChanges();
            }
        }
Пример #3
0
        public void Test_AddJob()
        {
            User user = new User("Test User", "Test Password");
            _tmDAO.AddUser(user);
            Job job = new Job(user, 100, 5, s => 50);

            _tmDAO.AddJob(job);
        }
Пример #4
0
        private void AddJobs(uint amount, User user)
        {
            for (uint index = 0; index < amount; index++)
            {
                uint time = (uint)_random.Next(100, 5000);
                int cpus = _random.Next(1, 6);
                Job job = new Job(user, time, cpus, s => 0);

                _jobs.Enqueue(job);
            }

            _jobsAdded += amount;
        }
Пример #5
0
 public void EventHandlerRunningTest()
 {
     Job job = new Job(new User("test", ""), 1000, 1, s => 0);
     _b.JobCancelled += delegate { throw new Exception(); };
     _b.SubmitJob(job);
     try
     {
         _b.ExecuteAll();
         Assert.Fail();
     }
     catch (Exception)
     {
     }
 }
Пример #6
0
        /// <summary>
        /// The main method...
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            TM2SQL_DAO tm2sqlDAO = new TM2SQL_DAO();
            //NB Modified TaskManagerModelContainer.Designer.cs linie 40!!

            //var obs = new ObjectContext(@"metadata=res://*/TaskManagerModel.csdl|res://*/TaskManagerModel.ssdl|res://*/TaskManagerModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source='YNDAL-LAPTOP\SQLEXPRESS';initial catalog=TaskManagerDB;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot");

            //Console.WriteLine("First step done");

            //IEnumerable<User> users = TM2SQL_DAO.GetAllUsers();

            //Console.Out.WriteLine("Size: " + users.Count<User>());

            //foreach (User u in users)
            //{
            //    Console.Out.WriteLine(u.UserName);
            //}

            //TM2SQL_DAO.GetJobsFromUser(4);

            //TM2SQL_DAO.GetJobsFromPeriodGrouped(new DateTime(2012, 1, 1), new DateTime(2012, 12, 31));
                //.GetJobsFromUser(5);

            User user = new User("Endnu en.WIHhhU..!", "...");
            tm2sqlDAO.AddUser(user);

            //IEnumerable<User> users = TM2SQL_DAO.GetAllUsers();

            Job job = new Job(user, 50, 5, s => 50);
            //job.TimeSubmitted = DateTime.Now;

            tm2sqlDAO.AddJob(job);
            tm2sqlDAO.AddLog(job, DateTime.Now);

            IEnumerable<User> users = tm2sqlDAO.GetAllUsers();
            IEnumerable<Job> jobs = tm2sqlDAO.GetJobsFromUser(user.UserId);

            foreach (User u in users)
            {
                Console.WriteLine("User: "******"Jobs: " + j);
            }

            //ConsoleUI console = new ConsoleUI();
        }
Пример #7
0
        public void CancelJobTest()
        {
            User user = new User("test", "");
            Job job = new Job(user, 100, 1, s => 0);
            _b.SubmitJob(job);

            //If job does anything else than runs or cancels...
            job.JobDone += delegate { Assert.Fail();};
            job.JobFailed += delegate { Assert.Fail(); };
            job.JobTerminated += delegate { Assert.Fail(); };

            _b.CancelJob(job);

            Assert.AreEqual(job.State, JobState.Cancelled);
        }
Пример #8
0
        public void Test_PopJobSequence()
        {
            Scheduler s = new Scheduler();
            Func<string[], int> funk = st => 10;

            // Check the sequence og jobs returned
            Job shortJob = new Job(new User("test", ""), 100, 5, funk);
            Job longJob = new Job(new User("test", ""), 1000, 5, funk);
            Job veryLongJob = new Job(new User("test", ""), 2500, 5, funk);

            // short, long, verylong
            AssertSequence(s, shortJob, longJob, veryLongJob);
            // long, short, verylong
            AssertSequence(s, longJob, shortJob, veryLongJob);
            // long, verylong, short
            AssertSequence(s, longJob, veryLongJob, shortJob);
            // short, verylong, long
            AssertSequence(s, shortJob, veryLongJob, longJob);
            // verylong, short, long
            AssertSequence(s, veryLongJob, shortJob, longJob);
            // verylong, long, short
            AssertSequence(s, veryLongJob, longJob, shortJob);
        }
Пример #9
0
        public void Test_AddJob()
        {
            Scheduler s = new Scheduler();

            // Add legal jobs with the three types: Short, Long, VeryLong
            Func<string[], int> funk = st => 10;
            Job shortJob = new Job(new User("test", ""), 100, 5, funk);
            Job longJob = new Job(new User("test", ""), 1000, 5, funk);
            Job veryLongJob = new Job(new User("test", ""), 2500, 5, funk);

            s.AddJob(shortJob);
            s.AddJob(longJob);
            s.AddJob(veryLongJob);

            Assert.AreEqual(3, s.JobsInSequence.Count);

            // Add 4000 jobs of varying type
            for (uint i = 0; i < 4000; i++)
            {
                s.AddJob(new Job(new User("test", ""), i + 100, 5, funk));
            }

            Assert.AreEqual(4003, s.JobsInSequence.Count);
        }
Пример #10
0
 /// <summary>
 /// Terminate a running job
 /// </summary>
 /// <param name="job">The job to terminate</param>
 public void TerminateJob(Job job)
 {
     Contract.Requires(job != null);
     Contract.Requires(job.State == JobState.Running);
     Contract.Ensures(job.State == JobState.Terminated);
     lock (_activityLock)
     {
         job.Terminate();
     }
 }
Пример #11
0
        /// <summary>
        /// Add job to queue
        /// </summary>
        /// <param name="job">The job to be added</param>
        public void SubmitJob(Job job)
        {
            Contract.Requires(job != null);
            Contract.Requires(job.User != null);            

            StoreJob(job);
            lock (_schedulerLock)
                _scheduler.AddJob(job);

            EventHandler handler = JobSubmitted;
            if (handler != null)
                handler(job, EventArgs.Empty);
            StartJobFromEvent();
        }
 /// <summary>
 /// Create a new Job object.
 /// </summary>
 /// <param name="jobId">Initial value of the JobId property.</param>
 /// <param name="timeExpected">Initial value of the TimeExpected property.</param>
 /// <param name="description">Initial value of the Description property.</param>
 /// <param name="stateString">Initial value of the StateString property.</param>
 /// <param name="userId">Initial value of the UserId property.</param>
 public static Job CreateJob(global::System.Int32 jobId, global::System.Int32 timeExpected, global::System.String description, global::System.String stateString, global::System.Int32 userId)
 {
     Job job = new Job();
     job.JobId = jobId;
     job.TimeExpected = timeExpected;
     job.Description = description;
     job.StateString = stateString;
     job.UserId = userId;
     return job;
 }
Пример #13
0
        public void Test_GetJobsFromUserWithinDays()
        {
            User user = new User("Test User", "Test Password");
            _tmDAO.AddUser(user);

            // Create 10 jobs on different days
            Job job = null;
            List<TimeSpan> timespans = new List<TimeSpan>();
            for (int i = 0; i < 10; i++)
            {
                timespans.Add(new TimeSpan(i, 0, 0, 0));
                job = new Job(user, 100, 5, s => 50);
                job.TimeSubmitted = DateTime.Now.Subtract(timespans[i]);

                _tmDAO.AddJob(job);
                _tmDAO.AddLog(job, DateTime.Now.Subtract(timespans[i]));
            }

            foreach (TimeSpan timespan in timespans)
            {
                IEnumerable<Job> jobs = _tmDAO.GetJobsFromUserInPeriod(user.UserId, DateTime.Now.Subtract(timespan), DateTime.Now);
                Assert.AreEqual(timespan.TotalDays, jobs.Count());
            }
        }
Пример #14
0
 public void Test_RemoveJobNoTInScheduler()
 {
     Scheduler s = new Scheduler();
     Job phonyJob = new Job(new User("test", ""), 2500, 5, st => 10);
     try
     {
         s.RemoveJob(phonyJob);
         Assert.Fail("It was possible to remove a job not in the scheduler");
     }
     catch (ArgumentException)
     {
         // This is good
     }
 }
Пример #15
0
        private void StoreJob(Job job)
#endif
        {
            Contract.Requires(job != null);
            Contract.Requires(job.User != null);
            Contract.Assume(!_jobs.ContainsKey(job.User) || _jobs[job.User] != null);

            lock (_tmDAOLock)
                _tmDAO.AddJob(job);

            lock (_activityLock)
            {
                if (!_jobs.ContainsKey(job.User)) _jobs.Add(job.User, new List<Job>());
                _jobs[job.User].Add(job);
            }
        }
Пример #16
0
 public void SubmitJobTest()
 {
     Job job = new Job(new User("test", ""), 1000, 1, s => 0);
     _b.SubmitJob(job);
     List<Job> list = _b.GetJobs(job.User);
     Assert.IsTrue(list.Contains(job));
 }
Пример #17
0
        public void StoreJobTest()
        {
            int expectedTime = 100; //Milliseconds
            User owner = new User("test", "");
            Job job = new Job(owner, (uint)expectedTime, 1, s => 0);
            _b.SubmitJob(job);

            List<Job> jobs = _b.GetJobs(owner);

            if(!jobs.Contains(job)) Assert.Fail("The job was not stored!");
        }
Пример #18
0
 public void GetJobsTest()
 {
     User owner = new User("test", "");
     Job job = new Job(owner, 1000, 1, s => 0);
     _b.SubmitJob(job);
     _b.ExecuteAll();
     List<Job> jobs = _b.GetJobs(owner);
     if (!jobs.Contains(job)) Assert.Fail();
 }
Пример #19
0
        public void FinishCurrentJobsTest()
        {
            uint numberOfJobs = 5;
            List<Job> jobs = new List<Job>((int)numberOfJobs);

            uint counter = 0;
            Job job;
            for (int index = 0; index < numberOfJobs; index++)
            {
                job = new Job(new User("test", ""), 100, 1, s => 0) { Description = "Job " + index };
                job.JobDone += delegate { counter++; };
                jobs.Add(job);
                _b.SubmitJob(job);

            }

            _b.JobDone += delegate
            {
                if (counter == numberOfJobs)
                    Assert.AreEqual(BSStatus.Stopped, _b.Status);
            };
            _b.ExecuteAll();
            _b.FinishCurrentJobs();
        }
Пример #20
0
        public void ExecuteAllTest()
        {
            //Also tests StartNextJob()
            uint numbOfJobs = 100;

            //Using the event from this job to see when all jobs are handled
            Job lastJob = null;

            User owner = new User("test", "");
            for (uint index = 0; index < numbOfJobs; index++)
            {
                Job job = new Job(owner, 100, 1, s => 0);
                _b.SubmitJob(job);
                lastJob = job;
            }

            ICollection jobs = _b.GetJobs(owner);

            uint counterDone = 0;
            uint counterUndone = 0;

            foreach (Job j in jobs)
                if (JobState.Done == j.State)
                    counterDone++;
                else
                    counterUndone++;

            //No jobs should be done
            Assert.AreEqual((uint)0, counterDone);
            Assert.AreEqual(numbOfJobs, counterUndone);

            _b.ExecuteAll();

            //Will let the test try to finish for 5 second, which should be more than enough!
            int counter = 0;
            while (_b.Status != BSStatus.Ready)
            {
                if (counter++ > 50) Assert.Fail("Benchmark was unable to reach the state: Ready");
                Thread.Sleep(100);
            }

            counterDone = 0;
            counterUndone = 0;

            foreach (Job j in jobs)
                if (j.State == JobState.Done)
                    counterDone++;
                else
                    counterUndone++;

            Assert.AreEqual(numbOfJobs, counterDone);
            Assert.AreEqual((uint)0, counterUndone);
        }
Пример #21
0
        public void Test_GetJobsFromUser()
        {
            User user = new User("Test User", "Test Password");
            _tmDAO.AddUser(user);
            Job job = new Job(user, 100, 5, s => 50);
            _tmDAO.AddJob(job);

            IEnumerable<Job> jobs = _tmDAO.GetJobsFromUser(user.UserId);
            Assert.AreEqual(1, jobs.Count());
        }
Пример #22
0
        /// <summary>
        /// Cancel a waiting job
        /// </summary>
        /// <param name="job">The job to remove from the queue</param>
        public void CancelJob(Job job)
        {
            Contract.Requires(job != null);
            Contract.Requires(job.State == JobState.Waiting);

            lock (_stateLock)
            {
                job.CancelJob();

                EventHandler handler = JobCancelled;
                if (handler != null)
                    handler(job, EventArgs.Empty);
            }
        }
Пример #23
0
        /// <summary>
        /// Store the job, to let the owner access it later on
        /// </summary>
        /// <param name="job">The job to store</param>
#if DEBUG
        internal void StoreJob(Job job)
Пример #24
0
 /// <summary>
 /// Unsubscribes from all event handlers related to parameter job
 /// </summary>
 /// <param name="job">The job to unsubscribe to</param>
 private void Unsubscribe(Job job)
 {
     job.JobRunning -= OnJobRunning;
     job.JobCancelled -= OnJobCancelled;
     job.JobTerminated -= OnJobTerminated;
     job.JobFailed -= OnJobFailed;
     job.JobDone -= OnJobDone;
 }
Пример #25
0
        public void Test_RemoveJobInScheduler()
        {
            Scheduler s = new Scheduler();
            Job job = new Job(new User("test", ""), 2000, 5, st => 10);
            s.AddJob(job);

            // Remove a job in the scheduler
            s.RemoveJob(job);
            Assert.AreEqual(0, s.JobsInSequence.Count);
        }
Пример #26
0
 /******************************************************************************************
  * The following methods relate to events
  * ****************************************************************************************/
 /// <summary>
 /// Subscribes from all event handlers related to parameter job
 /// </summary>
 /// <param name="job">The job to subscribe to</param>
 private void Subscribe(Job job)
 {
     job.JobRunning += OnJobRunning;
     job.JobCancelled += OnJobCancelled;
     job.JobTerminated += OnJobTerminated;
     job.JobFailed += OnJobFailed;
     job.JobDone += OnJobDone;
 }
Пример #27
0
 /// <summary>
 /// Helper method. Checks that the sequence of jobs returned is in the same order as they were added
 /// </summary>
 /// <param name="s">The scheduler</param>
 /// <param name="first">Job to add first</param>
 /// <param name="second">Job to add second</param>
 /// <param name="third">Job to add last</param>
 private void AssertSequence(Scheduler s, Job first, Job second, Job third)
 {
     s.AddJob(first);
     s.AddJob(second);
     s.AddJob(third);
     Assert.AreEqual(first, s.PopJob());
     Assert.AreEqual(second, s.PopJob());
     Assert.AreEqual(third, s.PopJob());
 }
Пример #28
0
 /// <summary>
 /// Checks if a job has reached its delay limit
 /// </summary>
 /// <param name="job">The job.</param>
 /// <returns>True if delaylimit for job is reached, false otherwise</returns>
 private bool ReachedDelaylimit(Job job)
 {
     lock (_delayLock)
     {
         return _delayForJob[job] == _delayLimit;
     }
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the Jobs EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToJobs(Job job)
 {
     base.AddObject("Jobs", job);
 }
Пример #30
0
        public void Test_GetJobsFromUserInPeriodOrdered()
        {
            BenchmarkSystem b = new BenchmarkSystem();
            DateTime startTime = new DateTime(2020, 1, 1);
            DateTime endTime = new DateTime(2020, 12, 31);
            User user = new User("Test User", "A secret password");

            _tmDAO.AddUser(user);
            int jobQuantity = 30;
            Job job = null;
            for (int index = 1; index <= jobQuantity; index++)
            {
                DateTime t1 = new DateTime(2020, 3, index);

                job = new Job(user, 100, 5, s => 38);
                _tmDAO.AddJob(job);
                _tmDAO.AddLog(job, t1);
                if (index % 2 == 0)
                {
                    job.Process(new String[] { "test" });
                    _tmDAO.AddLog(job, t1.AddHours(1));
                }
            }
            List<Job> jobsRequested = new List<Job>(_tmDAO.GetJobsFromUserInPeriodOrdered(user.UserId, startTime, endTime));

            for (int i = 0; i < jobsRequested.Count - 1; i++)
            {
                if (jobsRequested[i].CompareTo(jobsRequested[i + 1]) == 1)
                {
                    Assert.Fail("The order is no correct. JobId: " + jobsRequested[i] + " was larger than JobId: " + jobsRequested[i + 1]);
                }
            }

            Assert.AreEqual(jobQuantity, jobsRequested.Count);
        }