Beispiel #1
0
async def main(kafka_topic=KAFKA_TOPIC, sites=SITES, run_total=None):

    ssl_context = None
    security_protocol = 'PLAINTEXT'
    if KAFKA_SSL_CA is not None:
        ssl_context = create_ssl_context(cafile=KAFKA_SSL_CA,
                                         certfile=KAFKA_SSL_CERT,
                                         keyfile=KAFKA_SSL_KEY)
        security_protocol = 'SSL'

    producer = AIOKafkaProducer(bootstrap_servers=KAFKA_HOST,
                                value_serializer=serializer,
                                security_protocol=security_protocol,
                                ssl_context=ssl_context)

    msh = Scheduler()
    try:
        for site in sites:
            url = site['url']
            log.info(f'Monitoring {url}')
            msh.add_job(
                CronJob(run_total=run_total,
                        name=f'check_{url}').every().second.go(
                            check_site, producer, site, kafka_topic))
        await producer.start()
        await msh.start()
    except Exception as e:
        log.error(f'Got error starting scheduler: {e}')
        raise e
    finally:
        await producer.stop()
Beispiel #2
0
    def run(self):
        self.backend = Backend().init()
        if not self.backend:
            return
        if not Auth().init():
            return

        Launchy.attach_loop(self.loop)

        worker = Worker()
        self.task_worker = asyncio.ensure_future(worker.run())

        backend_worker = BackendWorker()
        self.task_backend_worker = asyncio.ensure_future(backend_worker.run())

        aptly_worker = AptlyWorker()
        self.task_aptly_worker = asyncio.ensure_future(aptly_worker.run())

        notification_worker = NotificationWorker()
        self.task_notification_worker = asyncio.ensure_future(notification_worker.run())

        cfg = Configuration()
        daily_cleanup = cfg.aptly.get("daily_cleanup")
        if daily_cleanup is False or daily_cleanup == "off" or daily_cleanup == "disabled":
            return

        if not daily_cleanup:
            daily_cleanup = "04:00"
        cleanup_sched = Scheduler(locale="en_US")
        cleanup_job = CronJob(name='cleanup').every().day.at(daily_cleanup).go(self.cleanup_task)
        cleanup_sched.add_job(cleanup_job)
        self.task_cron = asyncio.ensure_future(cleanup_sched.start())

        app.set_context_functions(MoliorServer.create_cirrina_context, MoliorServer.destroy_cirrina_context)
        app.run(self.host, self.port, logger=self.logger, debug=self.debug)
 def __init__(self, allowed_usernames, callback):
     threading.Thread.__init__(self)
     self.crons = {}
     self.allowed_usernames = allowed_usernames
     self.callback = callback
     self.loop = asyncio.get_event_loop()
     self.scheduler = Scheduler(loop=self.loop)
     self.loop.create_task(self.scheduler.start())
     self.start()
Beispiel #4
0
 def __init__(self, *args, **kwargs):
     logger.warning("Lo Sgargabot is starting...")
     super().__init__(*args,
                      **kwargs,
                      command_prefix=config.PREFIX,
                      description=config.DESCRIPTION)
     self.scheduler = Scheduler(locale=config.SCHEDULER_LOCALE)
     self.loaded_cogs = LoadedCogs(self)
     self.db = mongoengine.connect(
         db=config.MONGODB_MAIN,
         host="mongodb://" + config.MONGODB_USER + ":" +
         config.MONGODB_PASSWORD + "@" + config.MONGODB_HOST + ":" +
         str(config.MONGODB_PORT) + "/?authSource=admin",
         tz_aware=True)
     self.loop.create_task(self.scheduler.start())
Beispiel #5
0
import asyncio

from async_cron.job import CronJob
from async_cron.schedule import Scheduler
from async_cron.job_loader import FileJobLoader


async def test(*args, **kwargs):
    print(args, kwargs)


def tt(*args, **kwargs):
    print(args, kwargs)


msh = Scheduler(locale="zh_CN")
myjob = CronJob(name='test', run_total=3).every(5).second.go(test, (1, 2, 3),
                                                             name=123)
job2 = CronJob(name='exact', tolerance=100).at("2019-01-15 16:12").go(tt, (5),
                                                                      age=99)
job3 = CronJob(name='very_hour').every().hour.at(":44").go(tt, (5), age=99)

# add timezone infomation using at func, such as : UTC/GMT+08:00, time_shift=8
job3 = CronJob(name='hour').every().hour.at(
    ":06", time_shift=8).from_hour(18).to_hour(20).go(tt, (5), age=99)
job4 = CronJob(name='minute').every(1).minute.go(tt, (5), age=99)
job5 = CronJob(name='weekday').weekday(2).at("11:18").go(tt, (5), age=99)
job6 = CronJob(name='monthday').monthday(16).at("11:22").go(tt, (5), age=99)
job7 = CronJob(name='monthday').every(5).monthday(16).at("11:22").go(tt, (5),
                                                                     age=99)
Beispiel #6
0
 def __init__(self, *args, **kwargs):
     super(SchedulerKernel, self).__init__(*args, **kwargs)
     self.sch = Scheduler()
Beispiel #7
0
import asyncio
import os
from async_cron.job import CronJob
from async_cron.schedule import Scheduler
from pypi_tools.tracker import tracker
import sentry_sdk

scheduler = Scheduler(locale="en_US")

#sentry_sdk.init(os.environ["SENTRY_PATH"])

tracker_job = CronJob(name='track_packages').every(3).minute.go(tracker)

#scheduler.add_job(tracker_job)

if __name__ == '__main__':
    # Execute broadcaster
    try:
        asyncio.get_event_loop().run_until_complete(scheduler.start())
    except KeyboardInterrupt:
        print('Scheduler exit')
    except Exception as e:
        sentry_sdk.capture_exception(e)
Beispiel #8
0
import asyncio
import logging
import shutil

from asgiref.sync import sync_to_async
from async_cron.job import CronJob
from async_cron.schedule import Scheduler
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from starlette.routing import Route

from ..mc import rcon

log = logging.getLogger(__name__)
rootlog = logging.getLogger(None)
scheduler = Scheduler()


DELAY = 5 * 60  # Seconds
FREQUENCY = 60 * 60


@sync_to_async
def copy_dirs(src, dst):
    shutil.copytree(src, dst, dirs_exist_ok=True)


async def capture_exceptions(func):
    try:
        await func()
    except Exception:
Beispiel #9
0
    await verify_error_proxy_task()
    await update_squid_task()


@cron_wait
async def fetch_new_proxy_task():
    logger.info("run fetch_new_proxy_task")
    await spider.run_spider()
    await verifier.verify_new_proxy()
    # await verify_error_proxy_task()
    await update_squid_task()


if __name__ == '__main__':
    logger.info("start")

    loop = asyncio.get_event_loop()
    loop.run_until_complete(update_squid_task())

    msh = Scheduler()
    msh.add_job(CronJob().every(10).minute.go(verify_ok_proxy_task))
    msh.add_job(CronJob().every(30).minute.go(fetch_new_proxy_task))
    try:
        loop.run_until_complete(asyncio.wait([
            msh.start(),
            run_api_server(),
        ]))
        loop.run_forever()
    except KeyboardInterrupt:
        print('exit')
class GtmHubScheduler(threading.Thread):

    days_list = {
        '0': _('Monday'),
        '1': _('Tuesday'),
        '2': _('Wednesday'),
        '3': _('Thursday'),
        '4': _('Friday'),
        '5': _('Saturday'),
        '6': _('Sunday')
    }

    def __init__(self, allowed_usernames, callback):
        threading.Thread.__init__(self)
        self.crons = {}
        self.allowed_usernames = allowed_usernames
        self.callback = callback
        self.loop = asyncio.get_event_loop()
        self.scheduler = Scheduler(loop=self.loop)
        self.loop.create_task(self.scheduler.start())
        self.start()

    def run(self):
        self.loop.run_forever()

    @staticmethod
    def is_valid_day(day):
        return day in GtmHubScheduler.days_list.keys()

    @staticmethod
    def is_valid_hour(hour):
        try:
            time.strptime(hour, '%H:%M')
            return True
        except ValueError:
            return False

    def is_valid_username(self, username):
        return username in self.allowed_usernames

    def has_cron(self, peer_id):
        return peer_id in self.crons.keys()

    def need_params(self, peer_id):
        return self.has_cron(
            peer_id) and not self.crons[peer_id].is_configured()

    def create_cron(self, peer_id):
        if self.has_cron(peer_id):
            raise InputException(_('You have a reminder curently'))

        self.crons[peer_id] = Cron(peer_id)

    def set_value(self, peer_id, value):
        if not self.has_cron(peer_id):
            raise InputException(_('You have not a reminder to configure'))
        if self.crons[peer_id].is_configured():
            raise InputException(_('Your reminder is already configured'))
        if self.crons[peer_id].day is None:
            if not GtmHubScheduler.is_valid_day(value):
                raise InputException(_('Invalid day'))
            self.crons[peer_id].day = value
        elif self.crons[peer_id].hour is None:
            if not GtmHubScheduler.is_valid_hour(value):
                raise InputException(_('Invalid hour'))
            self.crons[peer_id].hour = value
        else:
            if not self.is_valid_username(value):
                raise InputException(
                    _('Invalid username\nAvailable usernames : {}').format(
                        '\n' + '\n'.join(self.allowed_usernames)))
            self.crons[peer_id].username = value

    def enable_cron(self, peer_id):
        day = int(self.crons[peer_id].day)
        hour = self.crons[peer_id].hour
        username = self.crons[peer_id].username

        job = CronJob(name=peer_id, loop=self.loop).weekday(day).at(hour).go(
            self.callback, peer_id, username)

        self.scheduler.add_job(job)

    def cancel_cron(self, peer_id):
        try:
            self.scheduler.del_job(peer_id)
            del self.crons[peer_id]
        except KeyError:
            InputException(_("You don't have a reminder currently"))