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

Get the ID of the admin user that scheduled the currently running job? #168

Open
mobweb opened this issue Jul 21, 2021 · 6 comments
Open

Comments

@mobweb
Copy link

mobweb commented Jul 21, 2021

For M1 we've been using https://github.com/AOEpeople/Aoe_Scheduler. Your extension has proven to be a wonderful successor for us to use in M2.

However, there is one requirement we haven't been able to solve yet.

When a method is run, we'd like to be able to check if it has been executed by a cron job. If yes, we'd like to get the admin user ID of the admin user that initially scheduled the job.

A little background: We've been using this logic for exporting large datasets. An admin user could schedule a custom export of a large dataset (e.g. all orders). This would add an entry into the cron schedule. As soon as the scheduled job has been executed, we'd then send an email to the admin user that initially requested the export.

Here is some pseudo-code showing how we were able to do this in M1:

// In an adminhtml controller, we schedule the job
Mage::getModel('aoe_scheduler/api')->schedule($jobCode);
// This is inside the method that was run by the job
$currentJob = Mage::registry('currently_running_schedule');
if($currentJob && $currentJob->getScheduledBy()) {
    $scheduledBy = $currentJob->getScheduledBy(); // This is the ID of the admin user that initially scheduled the job (see above)
    // Do something with the Admin User ID
}

Is there a proposed solution for this use case using your extension?

Thanks!

@fredden
Copy link
Collaborator

fredden commented Jul 21, 2021

This sounds like it should be a feature of the module doing the export. (eg, set notification email address(es) when requesting the export. This way the module can detect the result (success/failure) and report a suitable message to the waiting human (eg, link to download; error message, duration, report type/title/summary of content).)

@mobweb
Copy link
Author

mobweb commented Jul 21, 2021

Thanks for your response @fredden, but how would you suggest we trigger the notification? Let's assume that our custom cron job executes a method called "exportOrders". Near the bottom of "exportOrders", we'd ideally have some code which determines:

  • if the current execution of "exportOrders" was triggered by a cron job
  • if yes, get a reference to that cron job
  • ideally, get the "scheduled_by" information from that cron job

Then we can use the admin user ID from "scheduled_by" to send an email to the email address registered with that admin user account.

@fredden
Copy link
Collaborator

fredden commented Jul 21, 2021

Where you record what the job should do (ie, what to export), also record who asked for it / notification address(es). Then whenever your scheduled task runs it will have access to this information (what to export, whom to notify).

@mobweb
Copy link
Author

mobweb commented Jul 21, 2021

Then whenever your scheduled task runs it will have access to this information

I don't think this is currently possible? Or is there a way for me to get information about the cron job that triggered the execution of the currently running method somehow?

EDIT: To clarify: Assuming that there will only ever be one scheduled export at the time, we could ofcourse just store the details (email address) of the admin person that scheduled the export (for example in a newly created DB table). I think this is what you are describing.

However, we're looking at a scenario where Admin A might have scheduled an export at 10:00, and Admin B has done the same at 10:30. Let's assume that the first cron job then runs at 11:00 and the second one at 11:30. Admin A should receive an email at 11:00 and Admin B at 11:30. We really need to get some kind of reference to the scheduled cron job that triggered the execution of a method.

@fredden
Copy link
Collaborator

fredden commented Jul 21, 2021

If I've understood correctly, this module has a feature to export orders. Which orders to export (eg, what (relative to run time) date range to export, statuses to include/exclude) is stored globally (probably in store configuration) not per-export/run. There's a cronjob that runs the export process. This cronjob isn't run on a time-based schedule, but is instead run whenever a human decides by adding a one-off entry to the Magento scheduler table.

To support notification of completion, the module author could add a configuration option next to the existing configuration options (include/exclude statuses, time range, etc). This could be an email address or other mechanism (like a webhook); it's the module author's decision.

Or, if it's actually important to notify different people about the same data, then the module could record which user requested the export process to run in its own log/queue/schedule table and then send a suitable notification based on this data.

@mobweb
Copy link
Author

mobweb commented Jul 22, 2021

Thanks for your feedback @fredden.

The order export was just one (simplified) example for many scenarios in which we are using the current implementation of the extension I mentioned in the OP to get a reference to the currently running cron job when a method is executed, and also get a reference to the admin user that scheduled the execution of the cron job.

The way we are handling it right now, we definitely need to do that. I am aware that we could re-organize our code and probably get the desired result another way. But as we are looking to port most of our code from M1 to M2 without doing too much changes to the logic, I am looking for a way get that same behaviour out of this extension.

Do you see a way for us to get a reference to the currently running cron job and perhaps even its "author", or is that beyond the scope of this extension? Seeing how it was provided by the widely used "predecessor" extension for M1 (https://github.com/AOEpeople/Aoe_Scheduler) I do think that it would be a useful feature.

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