Esempio n. 1
0
 def put(self):
     payload = json.loads(self.request.body)
     id = int(self.request.url.rsplit('/', 3)[1])
     org = Organization.get_by_id(id)
     key = self.request.url.rsplit('/', 1)[1]
     heart = Heart.get_by_key_name(key, parent=org)
     heart.title = str(payload['title'])
     heart.threshold = int(payload['threshold'])
     heart.cron = str(payload['cron'])
     heart.time_zone = str(payload['time_zone'])
     croniter(heart.cron)
     heart.put()
Esempio n. 2
0
    def get_next_local_pulse_dates(self):
        if self.cron == "":
            return (None, None)

        local_time_zone = self.tz()

        next_date = croniter(self.cron, self.localized_last_pulse()).get_next(datetime)
        next_date = local_time_zone.localize(next_date)

        next_next_date = croniter(self.cron, next_date).get_next(datetime)
        next_next_date = local_time_zone.localize(next_next_date)

        return next_date, next_next_date
Esempio n. 3
0
    def get_next_local_pulse_dates(self):
        if self.cron == '':
            return (None, None)

        local_time_zone = self.tz()

        next_date = croniter(self.cron,
                             self.localized_last_pulse()).get_next(datetime)
        next_date = local_time_zone.localize(next_date)

        next_next_date = croniter(self.cron, next_date).get_next(datetime)
        next_next_date = local_time_zone.localize(next_next_date)

        return next_date, next_next_date
Esempio n. 4
0
 def put(self):
     payload = json.loads(self.request.body)
     id = int(self.request.url.rsplit('/', 3)[1])
     org = Organization.get_by_id(id)
     key = self.request.url.rsplit('/', 1)[1]
     heart = Heart.get_by_key_name(key, parent=org)
     heart.title = str(payload['title'])
     heart.check_deactivation(int(payload['threshold']))
     heart.threshold = int(payload['threshold'])
     heart.maintenance_day = datetime.strptime(payload['maintenance_day'], '%Y-%m-%d').date() if len( payload['maintenance_day'] ) > 0 else None
     heart.cron = str(payload['cron'])
     heart.time_zone = str(payload['time_zone'])
     croniter(heart.cron)
     heart.check_maintenance()
     heart.put()
Esempio n. 5
0
 def findNextRun(self,now):
     #find the cron expression and get the next run time
     cron_exp = self.parseSchedule()
     cron_ob = croniter(cron_exp,datetime.datetime.fromtimestamp(now))
     new_run_time = cron_ob.get_next(float)
     # utils.log('new run time' +  str(new_run_time))
     # utils.log('next run time' + str(self.next_run))
     if(new_run_time != self.next_run):
         self.next_run = new_run_time
         self.updateEpg()
         # utils.showNotification('EPG Updater', 'Next Update: ' + datetime.datetime.fromtimestamp(self.next_run).strftime('%m-%d-%Y %I:%M %p'))
         utils.log("scheduler will run again on " + datetime.datetime.fromtimestamp(self.next_run).strftime('%m-%d-%Y %I:%M %p'))
Esempio n. 6
0
    def is_flatlined(self):
        if self.cron == '':
            return self.last_pulse + timedelta(seconds=self.threshold*2) < datetime.utcnow()

        if self.time_zone is None:
            self.time_zone = 'UTC'

        local_time_zone = pytz.timezone(self.time_zone)
        offset = timedelta(seconds=self.threshold)

        last_pulse_local = pytz.utc.localize(self.last_pulse).astimezone(local_time_zone)
        next_date = croniter(self.cron, last_pulse_local).get_next(datetime)
        next_date = local_time_zone.localize(next_date)
        next_next_date = croniter(self.cron, next_date).get_next(datetime)
        next_date = next_date.astimezone(pytz.utc)
        next_next_date = local_time_zone.localize(next_next_date).astimezone(pytz.utc)
        now = pytz.utc.localize(datetime.now())

        # flatline if next_date and last_pulse are outside offset 
        if abs((next_date - last_pulse_local).total_seconds()) < self.threshold:
            return next_next_date + offset < now

        return  next_date + offset < now
Esempio n. 7
0
    def get_next_date(self, last_pulse):

      return self.tz().localize(croniter(self.cron, last_pulse.replace(tzinfo=None)).get_next(datetime))
Esempio n. 8
0
def handler(event, context):

    ec2_client = boto3.client('ec2')

    ec2_instances = ec2_client.describe_instances(
        Filters=[{
            'Name': 'tag-key',
            'Values': ['cronit:start']
        }, {
            'Name': 'tag-key',
            'Values': ['cronit:stop']
        }])

    items = []

    for reservation in ec2_instances['Reservations']:
        for ec2_instance in reservation['Instances']:
            item = (ec2_instance['InstanceId'],
                    map(
                        lambda tag: tag['Value'],
                        filter(lambda tag: tag['Key'] == 'cronit:start',
                               ec2_instance['Tags'])),
                    map(
                        lambda tag: tag['Value'],
                        filter(lambda tag: tag['Key'] == 'cronit:stop',
                               ec2_instance['Tags'])))
            items.append(item)

    instances_to_start = []
    instances_to_stop = []

    now = datetime.datetime.now().replace(second=0, microsecond=0)
    print 'Date now: %s' % now

    delayed = now - datetime.timedelta(minutes=1)
    print 'Date delayed: %s' % delayed

    for item in items:

        print 'Parsing item: ', item

        print 'Parsing start cron`s:'
        for i, cron_start in enumerate(item[1]):
            next_cron = croniter.croniter(cron_start.replace('?', '*'),
                                          delayed).get_next(datetime.datetime)
            print 'Instance %s next_cron: %s' % (item[0], next_cron)
            if next_cron <= now:
                instances_to_start.append(item[0])

        print 'Parsing stop cron`s:'
        for i, cron_stop in enumerate(item[2]):
            next_cron = croniter.croniter(cron_stop.replace('?', '*'),
                                          delayed).get_next(datetime.datetime)
            print 'Instance %s next_cron: %s' % (item[0], next_cron)
            if next_cron <= now:
                instances_to_stop.append(item[0])

    print 'Starting instances: %s' % instances_to_start
    instances_to_start and ec2_client.start_instances(
        InstanceIds=instances_to_start)

    print 'Stopping instances: %s' % instances_to_stop
    instances_to_stop and ec2_client.stop_instances(
        InstanceIds=instances_to_stop)

    print 'Done.'