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

[New Feature] #343

Open
melquelima opened this issue Jul 31, 2024 · 4 comments
Open

[New Feature] #343

melquelima opened this issue Jul 31, 2024 · 4 comments

Comments

@melquelima
Copy link

would be great to have a column called next_runtime or a way to calculate next run time like

select cron.next_runtime('* * * * *')

@cwang9208
Copy link

+1

@melquelima
Copy link
Author

melquelima commented Nov 5, 2024

@cwang9208 ive created a code that does that for me check this out

next run time

import pycron
from datetime import datetime,timedelta
import pytz

def next_runtime(s:str)
  dt = datetime.now()
  
  while True:
      if pycron.is_now(s, dt):
          return dt.replace(second=0, microsecond=0)
      dt += timedelta(minutes=1)

last run time

import pycron
from datetime import datetime,timedelta
import pytz

def last_runtime(s:str)
  dt = datetime.now()
  
  while True:
      if pycron.is_now(s, dt):
          return dt.replace(second=0, microsecond=0)
      dt -= timedelta(minutes=1)
    

POSTGRES


// ================================ PYTHON + PYCRON
CREATE EXTENSION IF NOT EXISTS plpython3u

CREATE EXTENSION IF NOT EXISTS pg_cron;


CREATE or replace function  get_last_runtime (s varchar)
  RETURNS VARCHAR
AS $$
 
import pycron
from datetime import datetime,timedelta
import pytz

dt = datetime.now()

while True:
    if pycron.is_now(s, dt):
        return dt.replace(second=0, microsecond=0)
    dt -= timedelta(minutes=1)


$$ LANGUAGE plpython3u;


CREATE or replace function  get_next_runtime (s varchar)
  RETURNS VARCHAR
AS $$
 
import pycron
from datetime import datetime,timedelta
import pytz

dt = datetime.now()

while True:
    if pycron.is_now(s, dt):
        return dt.replace(second=0, microsecond=0)
    dt += timedelta(minutes=1)


$$ LANGUAGE plpython3u;

@cwang9208
Copy link

@cwang9208
Copy link

@marcoslot @marcocitus Is it possible to calculate the next run date using the entry data structure in pg_cron's source code? Could you please help.

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