2

I'm having trouble getting celerybeat to schedule my function due to positional arguments.

Reading the documentation, I don't understand why some functions refer to a number and some don't. I've tried to add in the args as the same as my functions but I get the following error:

check_arguments(*(args or ()), **(kwargs or {}))
celery.beat.SchedulingError: Couldn't apply scheduled task 
list_market_catalogue: list_market_catalogue() takes 1 positional 
argument but 3 were given

Can anyone point me in the right direction?

tasks.py:

ET_TENNIS = 2
@shared_task(bind=True)
def list_market_catalogue(self):
    logger.warning('+' * 80)
    trading = get_betfair_client()
    time_ago = timezone.now() + datetime.timedelta(minutes=1000)
    time_fwd = timezone.now() + datetime.timedelta(minutes=6000)
    mfilter = market_filter(
        event_type_ids=[ET_TENNIS] ,
        market_start_time=time_range(
             from_=time_ago.strftime('%Y-%m-%dT%H:%I:%S.000Z') ,
             to=time_fwd.strftime('%Y-%m-%dT%H:%I:%S.000Z')
             )
       )
    res = trading.betting.list_market_catalogue(
         mfilter ,
         market_projection=[
            'EVENT' ,
            'MARKET_START_TIME' ,
          # 'MARKET_DESCRIPTION',
          # 'RUNNER_METADATA',
        ] ,
        sort='FIRST_TO_START' ,
        max_results=100 ,
        lightweight=True)
    if not len(res):
        logger.error('Market catalogue listing is empty')
        trading.session_token = None
        raise self.retry(countdown=5 , max_retries=12)

    for cat in res:
        if 'venue' not in cat['event']:
        logger.error(f'No event venue in {cat}')
        continue
    try:
        event = parse_event(cat['event'])
        market = parse_market(event , cat)
        runners = parse_runners(market , cat['runners'])
    except:
        logger.warning(cat)
        raise
        logger.warning(f'BETFAIR: Scraped {len(res)} from market 
        catalogue')

@shared_task
def parse_runners(market , items):
    """Parses runners from MarketCatalogue object"""
    runners = []
    for runner_item in items:
        num = runner_item['metadata'].get('CLOTH_NUMBER')
        if not num:
            matches = re.match(r'^(\d+)' , runner_item['runnerName'])
            if matches:
                num = matches.groups(0)[0]
            else:
                logger.error(f'Could not match number for 
                {runner_item}')
        runner , created = Runner.objects.update_or_create(
            selection_id=runner_item['selectionId'] ,
            defaults={
                'market': market ,
                # default
                'name': runner_item['runnerName'].upper() ,
                'sort_priority': runner_item['sortPriority'] ,
                'handicap': runner_item['handicap'] ,
                # metadata
                'cloth_number': num ,
                'stall_draw': 
                runner_item['metadata'].get('STALL_DRAW') ,
                'runner_id': runner_item['metadata']['runnerId'] ,
            }
        )
        if created:
            logger.info(f'Created {runner}')
        else:
            logger.debug(f'Updated {runner}')
        runners.append(runner)
    return runners

settings.py:

CELERY_BEAT_SCHEDULE = {
'list_market_catalogue': {
    'task': 'trader.tasks.list_market_catalogue',
    'schedule': timedelta(seconds=5),
    'args': (['self'],)
},
'parse_runners': {
    'task': 'trader.tasks.parse_runners' ,
    'schedule': timedelta(seconds=5) ,
    'args': (['market'],['items'])
},
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
tomoc4
  • 337
  • 2
  • 10
  • 29
  • 1
    try: `def list_market_catalogue(*args, **kwargs):` or remove `self` from function parameters. – Usman Maqbool May 04 '18 at 03:36
  • @UsmanMaqbool I've just updated the answer and changed the second function. I removed self in the original code but I still have an issue now with `parse_runners(market,items)` – tomoc4 May 04 '18 at 03:51

2 Answers2

1

Use *args as function parameters in all functions you use as task

use that args to fetch the parameters Say, in function parse_runners(market,items): you can use as parse_runners(*args) and inside function you can assign market=args[0] and items=args[1]

Aravindan
  • 9
  • 1
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 12 '22 at 03:05
0

I got the similar error below for celery beat:

celery.beat.SchedulingError: Couldn't apply scheduled task scheduled_task: Table type <class 'my_app.tasks.display'> for key b'task' not handled by amqp. [value: <@task: my_app.tasks.display of core at 0x1c4ba61c160>]

Because I have display task as shown below:

# "my_app/tasks.py"

from celery import shared_task

@shared_task
def display(arg):
    return arg

Then, I set display task to "task" by importing it from my_app.tasks as shown below:

# "core/settings.py"

from my_app.tasks import display

CELERY_BEAT_SCHEDULE = {
    "scheduled_task": {
        "task": display, # Here
        "schedule": 5.0,
        "args": ["Test"],
    }
}

So to solve the error, I set "my_app.tasks.display" to "task" without importing display task from my_app.tasks as shown below, then the error was solved:

# "core/settings.py"

# from my_app.tasks import display

CELERY_BEAT_SCHEDULE = {
    "scheduled_task": {
        "task": "my_app.tasks.display", # Here
        "schedule": 5.0,
        "args": ["Test"],
    }
}
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129