Example #1
0
def doAction(args):
    if args.add:
        url = args.url
        key = args.key
        if key is None:
            print("require key")
            return -1
        timeout = args.timeout
        repeat = args.repeat
        if url is None:
            print('url is required')
            return -1
        entry = Entry(f'urlCheck_{key}',
                      'tasks.urlSpeed',
                      repeat,
                      args=['GET', url, timeout, key],
                      app=tasks.app)
        entry.save()
        print(f"url added, key={entry.key}, store key for deletion")
    elif args.delete:
        key = args.key
        if key is None:
            print("require key")
            return -1
        entry = Entry.from_key(key, app=tasks.app)
        entry.delete()
        print("task deleted")
Example #2
0
def stop_timelapse_task(key: str) -> bool:
    try:
        entry = RedBeatSchedulerEntry.from_key(key, app=app)
        entry.delete()
        return True
    except Exception as e:
        logger.error(traceback.format_exc())
        raise TaskError(e)
Example #3
0
    def test_delete(self):
        initial = self.create_entry()
        initial.save()

        e = RedBeatSchedulerEntry.from_key(initial.key, app=self.app)
        e.delete()

        exists = self.app.redbeat_redis.exists(initial.key)
        self.assertFalse(exists)

        score = self.app.redbeat_redis.zrank(self.app.conf.REDBEAT_SCHEDULE_KEY, initial.key)
        self.assertIsNone(score)
Example #4
0
    def test_delete(self):
        initial = self.create_entry()
        initial.save()

        e = RedBeatSchedulerEntry.from_key(initial.key, app=self.app)
        e.delete()

        exists = self.app.redbeat_redis.exists(initial.key)
        self.assertFalse(exists)

        score = self.app.redbeat_redis.zrank(self.app.redbeat_conf.schedule_key, initial.key)
        self.assertIsNone(score)
Example #5
0
def _read_cached_scheduled_tasks():
    """Read in any cached task keys from a previous run if they were left because of a crash
    """
    results = []
    try:
        with open(".task_cache", "r") as f:
            for key in f.readlines():
                try:
                    e = Entry.from_key(key.strip(), app=app)
                    if e is not None:
                        results.append(e)
                except Exception as e:
                    print("Task not found: ", e)
    except FileNotFoundError as e:
        return None
    except Exception as e:
        print("Error: ", e)
    return results
Example #6
0
    def test_next(self):
        initial = self.create_entry().save()
        now = self.app.now()

        n = initial.next(last_run_at=now)

        # new entry has updated run info
        self.assertNotEqual(initial, n)
        self.assertEqual(n.last_run_at, now)
        self.assertEqual(initial.total_run_count + 1, n.total_run_count)

        # updated meta was stored into redis
        loaded = RedBeatSchedulerEntry.from_key(initial.key, app=self.app)
        self.assertEqual(loaded.last_run_at, now)
        self.assertEqual(loaded.total_run_count, initial.total_run_count + 1)

        # new entry updated the schedule
        redis = self.app.redbeat_redis
        self.assertEqual(redis.zscore(self.app.conf.REDBEAT_SCHEDULE_KEY, n.key), n.score)
Example #7
0
    def test_next(self):
        initial = self.create_entry().save()
        now = self.app.now()

        n = initial.next(last_run_at=now)

        # new entry has updated run info
        self.assertNotEqual(initial, n)
        self.assertEqual(n.last_run_at, now)
        self.assertEqual(initial.total_run_count + 1, n.total_run_count)

        # updated meta was stored into redis
        loaded = RedBeatSchedulerEntry.from_key(initial.key, app=self.app)
        self.assertEqual(loaded.last_run_at, now)
        self.assertEqual(loaded.total_run_count, initial.total_run_count + 1)

        # new entry updated the schedule
        redis = self.app.redbeat_redis
        self.assertEqual(redis.zscore(self.app.redbeat_conf.schedule_key, n.key), n.score)
Example #8
0
    def test_next(self):
        initial = self.create_entry().save()
        now = self.app.now()
        # 3.x is naive but 4.x is aware
        now = maybe_make_aware(now)

        n = initial.next(last_run_at=now)

        self.assertIsNotNone(now.tzinfo)
        self.assertEqual(n.last_run_at, now)
        self.assertEqual(initial.total_run_count + 1, n.total_run_count)

        # updated meta was stored into redis
        loaded = RedBeatSchedulerEntry.from_key(initial.key, app=self.app)
        self.assertEqual(loaded.last_run_at, now)
        self.assertEqual(loaded.total_run_count, initial.total_run_count + 1)

        # new entry updated the schedule
        redis = self.app.redbeat_redis
        self.assertEqual(
            redis.zscore(self.app.redbeat_conf.schedule_key, n.key), n.score)
Example #9
0
 def check_tasks(self, services: list, create: bool = True):
     for service_ in services:
         service = Service(service_)
         try:
             entry = Entry.from_key(redbeat_key_prefix +
                                    BASE_KEY.format(service.name),
                                    app=tasks.app)
         #     todo: check entry existence
         except:
             if create:
                 if service.type == 1:
                     entry = Entry(BASE_KEY.format(service.name),
                                   f'tasks.{service.type_name}',
                                   service.repeat_period,
                                   args=[
                                       service.metadata['url'],
                                       service.metadata['method'],
                                       service.metadata['timeout'],
                                       service.metadata.get('body', None)
                                   ],
                                   app=tasks.app)
                     entry.save()
                     print(entry.key)
     return 0
Example #10
0
    def test_from_key_missing_meta(self):
        initial = self.create_entry().save()

        loaded = RedBeatSchedulerEntry.from_key(initial.key, self.app)
        self.assertEqual(initial.task, loaded.task)
        self.assertIsNone(loaded.last_run_at)
Example #11
0
 def test_from_key_nonexistent_key(self):
     with self.assertRaises(KeyError):
         RedBeatSchedulerEntry.from_key('doesntexist', self.app)
Example #12
0
 def __init__(self, key='redbeat:watchdog-task'):
     self.key = key
     # Return KeyError or RedisConnectionError if encounter problem with Redis
     self.job = RedBeatSchedulerEntry.from_key(key=self.key, app=celery)
Example #13
0
def _get_entry_from_job(job):
    conf = RedBeatConfig(app)
    return RedBeatSchedulerEntry.from_key('{}{}'.format(
        conf.key_prefix, job.job_id),
                                          app=app)
Example #14
0
# Demonstrate how to update data in redis beat so worker can pickup updated data without restarting the beat
# Run those command first:
# celery beat -A update_task_redbeat -S redbeat.RedBeatScheduler -l INFO
# celery -A update_task_redbeat worker -l INFO --concurrency=2

# Then in python console, use these to update

from datetime import timedelta
import update_task_redbeat
from redbeat import RedBeatSchedulerEntry as Entry
from celery.schedules import schedule

app = update_task_redbeat.app
e = Entry.from_key('redbeat:task_259',  app=app)

# Update schedule to 5 seconds
e.schedule = schedule(5)
e.save()

# Update metadata to print Goodbye instead of Hello
# We simply change the args passed to the task
e.args = ["Goodbye"]
e.save()

# Delete task
e.delete()
Example #15
0
def delete_task(mapper, connection, target):
    celery_app = celery.get_celery_app()
    entry = RedBeatSchedulerEntry.from_key(target.get_redis_key(), celery_app)
    entry.delete()
Example #16
0
    def test_from_key_missing_meta(self):
        initial = self.create_entry().save()

        loaded = RedBeatSchedulerEntry.from_key(initial.key, self.app)
        self.assertEqual(initial.task, loaded.task)
        self.assertIsNone(loaded.last_run_at)
Example #17
0
 def test_from_key_nonexistent_key(self):
     with self.assertRaises(KeyError):
         RedBeatSchedulerEntry.from_key('doesntexist', self.app)