Beispiel #1
0
 def test_run_no_interval(self):
     instant_function = MagicMock()
     f = RepeatingFunc('test', instant_function, 0)
     f.start()
     time.sleep(0.01)
     f.stop()
     f.join()
     assert instant_function.call_count > 10
Beispiel #2
0
def start_scheduler(environment, domain, regions, lock_interval, loop_interval,
                    provision_interval):
    instance_id = get_instance_id()

    coreos = CoreOsAmiIndex()
    cloudformation = FlotillaCloudFormation(environment, domain, coreos)

    funcs = []
    for region in regions:
        # DynamoDB:
        dynamo = boto.dynamodb2.connect_to_region(region)
        tables = DynamoDbTables(dynamo, environment=environment)
        tables.setup([
            'assignments', 'locks', 'regions', 'services', 'stacks', 'status'
        ])
        db = FlotillaSchedulerDynamo(tables.assignments, tables.regions,
                                     tables.services, tables.stacks,
                                     tables.status)
        locks = DynamoDbLocks(instance_id, tables.locks)

        # Assemble into scheduler:
        schedule = FlotillaScheduler(db, locks, lock_ttl=lock_interval * 3)
        provisioner = FlotillaProvisioner(environment, region, schedule, db,
                                          cloudformation)

        funcs += [
            RepeatingFunc('scheduler-lock-%s' % region, schedule.lock,
                          lock_interval),
            RepeatingFunc('scheduler-%s' % region, schedule.loop,
                          loop_interval),
            RepeatingFunc('provisioner-%s' % region, provisioner.provision,
                          provision_interval)
        ]

        queue_name = 'flotilla-%s-scheduler' % environment
        sqs = boto3.resource('sqs', region)
        message_q = get_queue(sqs, queue_name)
        if message_q:
            elb = boto3.client('elb', region)
            doctor = ServiceDoctor(db, elb)
            messaging = FlotillaSchedulerMessaging(message_q, schedule, doctor)

            funcs.append(
                RepeatingFunc('scheduler-message-%s' % region,
                              messaging.receive, 0))

    # Start loops:
    map(RepeatingFunc.start, funcs)
Beispiel #3
0
def start_agent(environment, service, region, elb_name, health_interval,
                assignment_interval):
    # Identity:
    instance_id = get_instance_id()
    logger.debug('Resolved id: %s', instance_id)

    # Systemd:
    manager = Manager()
    systemd = SystemdUnits(manager)

    # AWS services:
    lb = get_elb(instance_id, elb_name, region)
    dynamo = boto.dynamodb2.connect_to_region(region)
    kms = boto.kms.connect_to_region(region)

    # DynamoDB:
    tables = DynamoDbTables(dynamo, environment=environment)
    tables.setup(['status', 'assignments', 'revisions', 'units', 'locks'])
    db = FlotillaAgentDynamo(instance_id, service, tables.status,
                             tables.assignments, tables.revisions,
                             tables.units, kms)
    locks = DynamoDbLocks(instance_id, tables.locks)

    # SQS:
    sqs = boto3.resource('sqs', region)
    scheduler_q = get_queue(sqs, 'flotilla-%s-scheduler' % environment)
    service_q = get_queue(sqs, 'flotilla-%s-service-%s' %
                          (environment, service))

    messaging = FlotillaAgentMessaging(service, instance_id, scheduler_q,
                                       service_q)

    # Assemble into agent:
    agent = FlotillaAgent(service, db, locks, systemd, messaging, lb)

    # Start loops:
    funcs = [
        RepeatingFunc('health', agent.health, health_interval),
        RepeatingFunc('assignment', agent.assignment, assignment_interval),
    ]

    if service_q:
        funcs.append(RepeatingFunc('message', messaging.receive, 0))

    map(RepeatingFunc.start, funcs)
    logger.info('Startup complete.')
Beispiel #4
0
    def test_run_too_slow(self):
        self.count = 0

        def slow_function():
            self.count += 1
            time.sleep(0.1)

        f = RepeatingFunc('test', slow_function, 0.01)
        f.start()
        time.sleep(0.2)
        f.stop()
        f.join()

        assert self.count > 1
        assert self.count < 3
Beispiel #5
0
 def run_loop(self, instant_function):
     f = RepeatingFunc('test', instant_function, 0.01)
     f.start()
     time.sleep(0.2)
     f.stop()
     f.join()