Giter Club home page Giter Club logo

Comments (11)

gee-forr avatar gee-forr commented on June 26, 2024 1

@Haegin we have similar use cases. Maybe our approach could be useful to you?

Our jobs are extremely thin wrappers around our LS code, so they contain no logic, except for calling the organizer, and then failing the job by throwing an exception if outcome.failure?

In fact - we don't actually have _job.rb files anymore... We build our organizers that need to be run via a job system like so:

  class MyReportOrganizer < ApplicationJob # NOTE: Inherits from ApplicationJob
    extend LightService::Organizer       # as well as extending ls-org

    queue_as :default

    def self.call(payload)
      with(date_range: payload[:date_range], email: payload[:email])
        .reduce(actions)
    end

    def self.actions
      [
        Reports::CollectRawData,
        Reports::WriteSpreadsheet,
        Reports::EmailReport,
      ]
    end

    def perform(start_date_string, end_date_string, email)
      date_range, email = hydrate(start_date_string, end_date_string, email)

      outcome = MyReportOrganizer.call(date_range: date_range, email: email)

      raise RuntimeError("Unable to process report.") if outcome.failure?

      true
    end

    def hydrate(start_date_string, end_date_string, email)
      start_date = Date.parse(start_date_string)
      end_date   = Date.parse(end_date_string)
      date_range = (start_date..end_date)

      [date_range, email]
    end
  end

So, what we've got here is a hybrid organizer/job that allows us to call it in the traditional manner from a controller, rake task, with rich objects etc and have it run synchronously, or call it like so:

MyReportOrganizer.perform_later(a_bunch, of_primitive, objects_here)

and it will take those primitives (which some job systems require), hydrate them into rich objects that are required for synchronous calling, and then run the organizer, failing it with an exception (to ensure a retry) if the job is a failure?.

Hope this helps.

from light-service.

gee-forr avatar gee-forr commented on June 26, 2024 1

@adomokos - sorry man, been really busy... this is still on my todo to add to the wiki

from light-service.

gee-forr avatar gee-forr commented on June 26, 2024 1

OK - wiki article is finally up: https://github.com/adomokos/light-service/wiki/Running-Light-Service-organizers-and-actions-in-jobs

from light-service.

adomokos avatar adomokos commented on June 26, 2024

What jobs are you referring to? Sidekiq? Resque? Maybe Rails?

from light-service.

Haegin avatar Haegin commented on June 26, 2024

We're using Delayed::Job via ActiveJob, but I imagine the approach would be the same for any of the above.

from light-service.

adomokos avatar adomokos commented on June 26, 2024

We've used LS with Resque to call APIs and persist data similar to how you described it. Resque workers picked up the payload, invoked a LS organizer with that data, and called a series of actions. Would that work for you?

from light-service.

gee-forr avatar gee-forr commented on June 26, 2024

@adomokos - lemme know if you want me to update the readme with this as an advanced usage example...

from light-service.

adomokos avatar adomokos commented on June 26, 2024

@gee-forr - that's exactly how I'd do it.
I wouldn't add it to the main README.md, but having an entry with the comment (and code example) you provided in the wiki would be helpful.

from light-service.

adomokos avatar adomokos commented on June 26, 2024

I'll close this issue once the entry is created in the wiki.

from light-service.

adomokos avatar adomokos commented on June 26, 2024

I'll close it once it's in the wiki.

from light-service.

adomokos avatar adomokos commented on June 26, 2024

Thank you so much for adding that page to the Wiki, @gee-forr, closing this issue. 👍

from light-service.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.