Exemplo n.º 1
0
def create_schedules(events):
    schedules = []

    for job in api.get_jobs():
        cmd_sch = croniter(job.interval, job.last_time_run)

        # If we have missed more than one occurrence only run one
        time = cmd_sch.get_next(datetime)
        next_time = cmd_sch.get_next(datetime)
        if next_time <= datetime.now():
            time = next_time
            next_time = cmd_sch.get_next(datetime)

        if (time - datetime.now()) < SCHEDULER_UPDATE_INTERVAL:
            job.last_time_run = time
            api.set_job_time(job)

            next_worker = api.get_next_worker()
            if next_worker is None:
                break

            schedule = api.Schedule(time, job, next_worker)
            schedules.append(schedule)

    api.add_schedules(sort_schedules(schedules))

    delay = SCHEDULER_UPDATE_INTERVAL.total_seconds()
    events.enter(delay, 1, create_schedules, (events,))
Exemplo n.º 2
0
def create_schedules(events):
    schedules = []

    for job in api.get_jobs():
        cmd_sch = croniter(job.interval, job.last_time_run)

        # If we have missed more than one occurrence only run one
        time = cmd_sch.get_next(datetime)
        next_time = cmd_sch.get_next(datetime)
        if next_time <= datetime.now():
            time = next_time
            next_time = cmd_sch.get_next(datetime)

        if (time - datetime.now()) < SCHEDULER_UPDATE_INTERVAL:
            job.last_time_run = time
            api.set_job_time(job)

            next_worker = api.get_next_worker()
            if next_worker is None:
                break

            schedule = api.Schedule(time, job, next_worker)
            schedules.append(schedule)

    api.add_schedules(sort_schedules(schedules))

    delay = SCHEDULER_UPDATE_INTERVAL.total_seconds()
    events.enter(delay, 1, create_schedules, (events, ))
Exemplo n.º 3
0
 def test_get_schedules_many_workers_many_schedules(self):
     num_of_jobs = MANY
     num_of_schedules = num_of_jobs
     num_of_workers = num_of_schedules
     num_of_workers = MANY
     test_jobs = create_test_tab(num_of_jobs, user1)
     api.set_jobs(test_jobs, user1)
     jobs_list = api.get_jobs()
     test_workers = []
     # Create many workers and add them to a list for bookkeeping
     for worker in range(num_of_workers):
         test_workers.append(api.create_worker())
     # Create a schedule belonging to each worker representing the only job
     test_schedules = []
     for worker in test_workers:
         for job in jobs_list:
             test_schedules.append(api.Schedule(datetime.now(),
                                   job, worker))
     api.add_schedules(test_schedules)
     # Aggregate each worker's schedules into a single list for comparison
     # with test schedules
     schedules_for_workers = []
     for worker in test_workers:
         schedules_list = api.get_schedules(worker)
         schedules_for_workers.extend(schedules_list)
     # Verify that the information we get matches what was set
     check_schedule_fields(self, schedules_for_workers,
                           test_schedules)
Exemplo n.º 4
0
 def test_remove_schedule_many_schedules(self):
     # Number of jobs and floor(requests) + ceiling(requests) are equal
     num_of_jobs = MANY
     num_of_schedules = num_of_jobs
     # Automatic flooring care of Python
     num_of_requests = num_of_jobs / 2
     # Create a crontab with many jobs
     test_jobs = create_test_tab(num_of_jobs, user1)
     api.set_jobs(test_jobs, user1)
     jobs_list = api.get_jobs()
     test_workers = []
     # Create one worker and add them to a list for bookkeeping
     test_workers.append(api.create_worker())
     # Create a schedule for many jobs
     test_schedules = []
     for schedule in range(num_of_schedules):
         test_schedules.append(
             api.Schedule(datetime.now(), test_jobs[schedule],
                          test_workers[0]))
     api.add_schedules(test_schedules)
     # Kill the floor of half of the workers in the list
     for request in range(num_of_requests):
         api.remove_schedule(test_schedules.pop())
     schedules_list = api.get_schedules(test_workers[0])
     # Verify that the correct remaining schedules are still in the list
     check_schedule_fields(self, schedules_list, test_schedules)
     # Remove all remaining test schedules
     while test_schedules:
         api.remove_schedule(test_schedules.pop())
     # Try to get schedules from an empty pool
     schedules_list = api.get_schedules(test_workers[0])
     # Verify that the next worker does not exist
     self.assertFalse(len(schedules_list))
Exemplo n.º 5
0
 def test_remove_schedules_many_schedules_random_schedules(self):
     # Number of jobs and floor(requests) + ceiling(requests) are equal
     num_of_jobs = MANY
     num_of_schedules = num_of_jobs
     # Leave one schedule remaining for comparison at the end
     num_of_requests = num_of_jobs - 1
     # Create a crontab with many jobs
     test_jobs = create_test_tab(num_of_jobs, user1)
     api.set_jobs(test_jobs, user1)
     jobs_list = api.get_jobs()
     test_workers = []
     # Create one worker and add them to a list for bookkeeping
     test_workers.append(api.create_worker())
     test_schedules = []
     for schedule in range(num_of_schedules):
         test_schedules.append(
             api.Schedule(datetime.now(), test_jobs[schedule],
                          test_workers[0]))
     api.add_schedules(test_schedules)
     # Kill number of requests random schedules in the list
     for request in range(num_of_requests):
         random_schedule = random.randrange(len(test_schedules))
         api.remove_schedule(test_schedules.pop(random_schedule))
     schedules_list = []
     schedules_list = api.get_schedules(test_workers[0])
     # Verify that the schedules list contains exactly one schedule
     self.assertEqual(len(schedules_list), 1)
     # Verify that the correct remaining schedule is still in the list
     check_schedule_fields(self, schedules_list, test_schedules)
Exemplo n.º 6
0
 def test_get_schedules_many_workers_many_schedules(self):
     num_of_jobs = MANY
     num_of_schedules = num_of_jobs
     num_of_workers = num_of_schedules
     num_of_workers = MANY
     test_jobs = create_test_tab(num_of_jobs, user1)
     api.set_jobs(test_jobs, user1)
     jobs_list = api.get_jobs()
     test_workers = []
     # Create many workers and add them to a list for bookkeeping
     for worker in range(num_of_workers):
         test_workers.append(api.create_worker())
     # Create a schedule belonging to each worker representing the only job
     test_schedules = []
     for worker in test_workers:
         for job in jobs_list:
             test_schedules.append(api.Schedule(datetime.now(), job,
                                                worker))
     api.add_schedules(test_schedules)
     # Aggregate each worker's schedules into a single list for comparison
     # with test schedules
     schedules_for_workers = []
     for worker in test_workers:
         schedules_list = api.get_schedules(worker)
         schedules_for_workers.extend(schedules_list)
     # Verify that the information we get matches what was set
     check_schedule_fields(self, schedules_for_workers, test_schedules)
Exemplo n.º 7
0
 def test_remove_schedule_many_schedules(self):
     # Number of jobs and floor(requests) + ceiling(requests) are equal
     num_of_jobs = MANY
     num_of_schedules = num_of_jobs
     # Automatic flooring care of Python
     num_of_requests = num_of_jobs / 2
     # Create a crontab with many jobs
     test_jobs = create_test_tab(num_of_jobs, user1)
     api.set_jobs(test_jobs, user1)
     jobs_list = api.get_jobs()
     test_workers = []
     # Create one worker and add them to a list for bookkeeping
     test_workers.append(api.create_worker())
     # Create a schedule for many jobs
     test_schedules = []
     for schedule in range(num_of_schedules):
         test_schedules.append(api.Schedule(datetime.now(),
                               test_jobs[schedule], test_workers[0]))
     api.add_schedules(test_schedules)
     # Kill the floor of half of the workers in the list
     for request in range(num_of_requests):
         api.remove_schedule(test_schedules.pop())
     schedules_list = api.get_schedules(test_workers[0])
     # Verify that the correct remaining schedules are still in the list
     check_schedule_fields(self, schedules_list, test_schedules)
     # Remove all remaining test schedules
     while test_schedules:
         api.remove_schedule(test_schedules.pop())
     # Try to get schedules from an empty pool
     schedules_list = api.get_schedules(test_workers[0])
     # Verify that the next worker does not exist
     self.assertFalse(len(schedules_list))
Exemplo n.º 8
0
 def test_remove_schedules_many_schedules_random_schedules(self):
     # Number of jobs and floor(requests) + ceiling(requests) are equal
     num_of_jobs = MANY
     num_of_schedules = num_of_jobs
     # Leave one schedule remaining for comparison at the end
     num_of_requests = num_of_jobs - 1
     # Create a crontab with many jobs
     test_jobs = create_test_tab(num_of_jobs, user1)
     api.set_jobs(test_jobs, user1)
     jobs_list = api.get_jobs()
     test_workers = []
     # Create one worker and add them to a list for bookkeeping
     test_workers.append(api.create_worker())
     test_schedules = []
     for schedule in range(num_of_schedules):
         test_schedules.append(api.Schedule(datetime.now(),
                               test_jobs[schedule], test_workers[0]))
     api.add_schedules(test_schedules)
     # Kill number of requests random schedules in the list
     for request in range(num_of_requests):
         random_schedule = random.randrange(len(test_schedules))
         api.remove_schedule(test_schedules.pop(random_schedule))
     schedules_list = []
     schedules_list = api.get_schedules(test_workers[0])
     # Verify that the schedules list contains exactly one schedule
     self.assertEqual(len(schedules_list), 1)
     # Verify that the correct remaining schedule is still in the list
     check_schedule_fields(self, schedules_list, test_schedules)
Exemplo n.º 9
0
def test_get_jobs():
    jobs_list = api.get_jobs()
    current = 0
    for job in jobs_list:
        if job.interval == test_jobs[current].interval:
            print 'PASS'
        else:
            print 'FAIL'
        current += 1
Exemplo n.º 10
0
 def test_get_jobs_many_jobs(self):
     num_of_jobs = MANY
     # Create a crontab with many jobs
     test_jobs = create_test_tab(num_of_jobs, user1)
     api.set_jobs(test_jobs, user1)
     jobs_list = api.get_jobs()
     self.assertEqual(len(jobs_list), num_of_jobs)
     # Verify that the information we get matches what was set
     check_job_fields(self, jobs_list, test_jobs, user1)
Exemplo n.º 11
0
def get_num_users():
    users = set()
    for job in api.get_jobs():
        users.add(job.user_id)
    L = len(users)
    if L == 1:
        print("1 user has a job")
    else:
        print("%d users have jobs" % L)
Exemplo n.º 12
0
 def test_get_jobs_many_jobs(self):
     num_of_jobs = MANY
     # Create a crontab with many jobs
     test_jobs = create_test_tab(num_of_jobs, user1)
     api.set_jobs(test_jobs, user1)
     jobs_list = api.get_jobs()
     self.assertEqual(len(jobs_list), num_of_jobs)
     # Verify that the information we get matches what was set
     check_job_fields(self, jobs_list, test_jobs, user1)
Exemplo n.º 13
0
def get_num_users():
    users = set()
    for job in api.get_jobs():
        users.add(job.user_id)
    L = len(users)
    if L == 1:
        print("1 user has a job")
    else:
        print("%d users have jobs" % L)
Exemplo n.º 14
0
 def test_get_jobs_one_job(self):
     num_of_jobs = ONE
     # Create a crontab with one job
     test_jobs = create_test_tab(num_of_jobs, user1)
     api.set_jobs(test_jobs, user1)
     jobs_list = api.get_jobs()
     # Verify that the jobs list contains exactly one job
     self.assertEqual(len(jobs_list), num_of_jobs)
     # Verify that the information we get matches what was set
     check_job_fields(self, jobs_list, test_jobs, user1)
Exemplo n.º 15
0
 def test_get_jobs_one_job(self):
     num_of_jobs = ONE
     # Create a crontab with one job
     test_jobs = create_test_tab(num_of_jobs, user1)
     api.set_jobs(test_jobs, user1)
     jobs_list = api.get_jobs()
     # Verify that the jobs list contains exactly one job
     self.assertEqual(len(jobs_list), num_of_jobs)
     # Verify that the information we get matches what was set
     check_job_fields(self, jobs_list, test_jobs, user1)
Exemplo n.º 16
0
 def test_get_jobs_empty_jobs(self):
     num_of_jobs = ZERO
     # Create a crontab with zero jobs
     test_jobs = create_test_tab(num_of_jobs, user1)
     api.set_jobs(test_jobs, user1)
     jobs_list = api.get_jobs()
     # Verify that the jobs list is empty
     self.assertEqual(len(jobs_list), num_of_jobs)
     # Verify that the information we get matches what was set
     check_job_fields(self, jobs_list, test_jobs, user1)
Exemplo n.º 17
0
 def test_get_jobs_empty_jobs(self):
     num_of_jobs = ZERO
     # Create a crontab with zero jobs
     test_jobs = create_test_tab(num_of_jobs, user1)
     api.set_jobs(test_jobs, user1)
     jobs_list = api.get_jobs()
     # Verify that the jobs list is empty
     self.assertEqual(len(jobs_list), num_of_jobs)
     # Verify that the information we get matches what was set
     check_job_fields(self, jobs_list, test_jobs, user1)
Exemplo n.º 18
0
 def test_add_schedules_one_job_one_schedule(self):
     num_of_jobs = ONE
     num_of_schedules = num_of_jobs
     # Create a crontab with one job
     test_jobs = create_test_tab(num_of_jobs, user1)
     api.set_jobs(test_jobs, user1)
     jobs_list = api.get_jobs()
     test_workers = []
     # Create one worker and add them to a list for bookkeeping
     test_workers.append(api.create_worker())
     # Create a schedule for the only job
     test_schedules = []
     test_schedules.append(
         api.Schedule(datetime.now(), test_jobs[0], test_workers[0]))
     api.add_schedules(test_schedules)
     schedules_list = api.get_schedules(test_workers[0])
     # Verify that the schedules list contains exactly one schedule
     self.assertEqual(len(schedules_list), num_of_schedules)
Exemplo n.º 19
0
 def test_get_schedules_one_worker_many_schedules(self):
     num_of_jobs = MANY
     num_of_schedules = num_of_jobs
     test_jobs = create_test_tab(num_of_jobs, user1)
     api.set_jobs(test_jobs, user1)
     jobs_list = api.get_jobs()
     test_workers = []
     # Create one worker and add them to a list for bookkeeping
     test_workers.append(api.create_worker())
     # Create a schedule for the only job
     test_schedules = []
     for job in range(num_of_jobs):
         test_schedules.append(
             api.Schedule(datetime.now(), test_jobs[job], test_workers[0]))
     api.add_schedules(test_schedules)
     schedules_list = api.get_schedules(test_workers[0])
     # Verify that the information we get matches what was set
     check_schedule_fields(self, schedules_list, test_schedules)
Exemplo n.º 20
0
 def test_get_schedules_one_worker_many_schedules(self):
     num_of_jobs = MANY
     num_of_schedules = num_of_jobs
     test_jobs = create_test_tab(num_of_jobs, user1)
     api.set_jobs(test_jobs, user1)
     jobs_list = api.get_jobs()
     test_workers = []
     # Create one worker and add them to a list for bookkeeping
     test_workers.append(api.create_worker())
     # Create a schedule for the only job
     test_schedules = []
     for job in range(num_of_jobs):
         test_schedules.append(api.Schedule(datetime.now(), test_jobs[job],
                               test_workers[0]))
     api.add_schedules(test_schedules)
     schedules_list = api.get_schedules(test_workers[0])
     # Verify that the information we get matches what was set
     check_schedule_fields(self, schedules_list, test_schedules)
Exemplo n.º 21
0
 def test_add_schedules_one_job_one_schedule(self):
     num_of_jobs = ONE
     num_of_schedules = num_of_jobs
     # Create a crontab with one job
     test_jobs = create_test_tab(num_of_jobs, user1)
     api.set_jobs(test_jobs, user1)
     jobs_list = api.get_jobs()
     test_workers = []
     # Create one worker and add them to a list for bookkeeping
     test_workers.append(api.create_worker())
     # Create a schedule for the only job
     test_schedules = []
     test_schedules.append(api.Schedule(datetime.now(), test_jobs[0],
                           test_workers[0]))
     api.add_schedules(test_schedules)
     schedules_list = api.get_schedules(test_workers[0])
     # Verify that the schedules list contains exactly one schedule
     self.assertEqual(len(schedules_list), num_of_schedules)
Exemplo n.º 22
0
 def test_remove_schedule_one_schedule(self):
     num_of_jobs = ONE
     num_of_schedules = num_of_jobs
     # Create a crontab with one job
     test_jobs = create_test_tab(num_of_jobs, user1)
     api.set_jobs(test_jobs, user1)
     jobs_list = api.get_jobs()
     test_workers = []
     # Create one worker and add them to a list for bookkeeping
     test_workers.append(api.create_worker())
     # Create a schedule for the only job
     test_schedules = []
     test_schedules.append(
         api.Schedule(datetime.now(), test_jobs[0], test_workers[0]))
     api.add_schedules(test_schedules)
     # Kill the only schedule in the list
     api.remove_schedule(test_schedules[0])
     schedules_list = api.get_schedules(test_workers[0])
     # Verify that the schedules list is empty
     self.assertFalse(len(schedules_list))
Exemplo n.º 23
0
 def test_remove_schedule_one_schedule(self):
     num_of_jobs = ONE
     num_of_schedules = num_of_jobs
     # Create a crontab with one job
     test_jobs = create_test_tab(num_of_jobs, user1)
     api.set_jobs(test_jobs, user1)
     jobs_list = api.get_jobs()
     test_workers = []
     # Create one worker and add them to a list for bookkeeping
     test_workers.append(api.create_worker())
     # Create a schedule for the only job
     test_schedules = []
     test_schedules.append(api.Schedule(datetime.now(), test_jobs[0],
                           test_workers[0]))
     api.add_schedules(test_schedules)
     # Kill the only schedule in the list
     api.remove_schedule(test_schedules[0])
     schedules_list = api.get_schedules(test_workers[0])
     # Verify that the schedules list is empty
     self.assertFalse(len(schedules_list))
Exemplo n.º 24
0
def get_num_jobs():
    L = len(api.get_jobs())
    if L == 1:
        print("1 job in queue")
    else:
        print("%d jobs in queue" % L)
Exemplo n.º 25
0
def get_num_jobs():
    L = len(api.get_jobs())
    if L == 1:
        print("1 job in queue")
    else:
        print("%d jobs in queue" % L)