Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Execute every 1 minute with a start and stop time in the middle of an hour e.g. 6:30 - 7:30 #2

Open
rmarnold opened this issue Feb 16, 2021 · 2 comments

Comments

@rmarnold
Copy link

No description provided.

@bradshjg
Copy link
Owner

I really like this idea, thanks. There are definitely limits to cron expressions.

I think I have an API design for this, but wanted to run it by first to see if it sounds good. It does complicate things, but it opens up a couple interesting use cases. In addition to the one you've pointed out, it would also allowed for delayed starts or some external state-based control mechanisms.

We could add an active keyword argument to the cron function/decorator that expects a callable that takes no arguments. When the method is scheduled to fire, the run is skipped if the active callable evaluates falsy.

The way this could work in your case is something like

from datetime import datetime

from nameko_cron import cron


def should_run():
    now = datetime.now()
    if (6, 30) < (now.hour, now.minute) < (7, 30):
        return True
    return False

class Service:
    name ="service"

    @cron('* 6,7 * * *', active=should_run)
    def ping(self):
        # executes every 1 minute between 6:30-7:30
        print("pong")

Though the above probably has some time zone stuff we'd want to make sure we handle properly.

@bradshjg
Copy link
Owner

Another idea that I thought a bit about but am not too sure of the value is to create another layer of abstraction. This probably wouldn't belong in this library...well maybe the cron scheduler could be subsumed into a nameko-scheduler library as one implementation.

The API could be something like

class Service:
    name ="service"

    @schedule(scheduler=some_scheduler(*args, **kwargs))
    def ping(self):
        # executes every 1 minute between 6:30-7:30
        print("pong")

This won't be immediately useful, but it could potentially allow for custom scheduler implementation without requiring the implementer to have to handle the worker spawning and result handing and concurrency policy concerns. The idea would be that a scheduler would need to supply a generator that yields the sleep time a la https://github.com/bradshjg/nameko-cron/blob/master/nameko_cron/__init__.py#L66. I think I might still want the active kwarg because I do kinda like the idea of pulling some control logic outside of the service itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants