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

Schedule job every x minutes #56

Open
khoogheem opened this issue Jan 5, 2020 · 6 comments
Open

Schedule job every x minutes #56

khoogheem opened this issue Jan 5, 2020 · 6 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@khoogheem
Copy link

Looks like there is no way to make ScheduledJobs run every X hours or X Mins.

The current helpers if I am not mistaken are every 1 min, 1 hour..

I don't see a way to create a ScheduledBuilder and use that to schedule a job.

looks like the jobs.schedule() calls the mutating function self.storage.configuration.schedule(job, builder: builder) which is internal.

@khoogheem
Copy link
Author

ok.. so I kinda found that this works. for doing something every 10 mins.. but surely we can come up with something easier:

    app.jobs.schedule(ApiPerformanceJob(app)).hourly().at(ScheduleBuilder.Minute(integerLiteral: 0))
    app.jobs.schedule(ApiPerformanceJob(app)).hourly().at(ScheduleBuilder.Minute(integerLiteral: 10))
    app.jobs.schedule(ApiPerformanceJob(app)).hourly().at(ScheduleBuilder.Minute(integerLiteral: 20))
    app.jobs.schedule(ApiPerformanceJob(app)).hourly().at(ScheduleBuilder.Minute(integerLiteral: 30))
    app.jobs.schedule(ApiPerformanceJob(app)).hourly().at(ScheduleBuilder.Minute(integerLiteral: 40))
    app.jobs.schedule(ApiPerformanceJob(app)).hourly().at(ScheduleBuilder.Minute(integerLiteral: 50))

@tanner0101 tanner0101 added this to To Do in Vapor 4 via automation Jan 20, 2020
@tanner0101 tanner0101 added the enhancement New feature or request label Jan 20, 2020
@tanner0101
Copy link
Member

I don't think this is supported yet. Maybe this would be a good API:

app.jobs.schedule(SomeJob()).minutely().every(10)

@khoogheem
Copy link
Author

khoogheem commented Jan 20, 2020

@tanner0101 That looks like it would make sense.. everything I was thinking about the other day.. just didn't convey what it should do.

@tanner0101 tanner0101 changed the title Any way to make a ScheduledJob every X mins/hours Schedule job every x minutes Jan 21, 2020
@stephanmantler
Copy link

My temporary approach for running a job every 5 minutes:

struct MyJob : ScheduledJob {
  // start with 4 to ensure first run happens immediately
  static var inter: Int = 4

  func run(c: QueueContext) -> EventLoopFuture<Void> {
    // internal counter increments by one, modulo 5 (so wraps to 0 every five minutes)
    MyJob = (MyJob + 1) % 5
    if MyJob > 0 {
      return context.eventLoop.future()
    }
    // 
    // --- do actual work --
    ...
  }
}

@jnordberg
Copy link

jnordberg commented Jul 15, 2020

👍

Would be cool if the schedule builder was public as well so you can implement your own, e.g.:

app.jobs.schedule(SomeJob()).custom(SolarNoonScheduler(lat: 78.651, long: 376.990))

@Sherlouk
Copy link

Issue seems to be quiet but I too would love this functionality, my workaround is very much similar to Kevin's. Though I've made it a little more flexible in an extension 😄

extension Application.Queues {
    func scheduleEvery(_ job: ScheduledJob, minutes: Int) {
        for minuteOffset in stride(from: 0, to: 60, by: minutes) {
            schedule(job).hourly().at(.init(integerLiteral: minuteOffset))
        }
    }
}

@0xTim 0xTim added the help wanted Extra attention is needed label Aug 2, 2022
@VaporBot VaporBot added this to To do in Help Wanted Issues Aug 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
Vapor 4
  
Backlog
Development

No branches or pull requests

6 participants