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

Calling Syrup functions from another class #860

Closed
UncertainSpin opened this issue Apr 25, 2018 · 3 comments
Closed

Calling Syrup functions from another class #860

UncertainSpin opened this issue Apr 25, 2018 · 3 comments

Comments

@UncertainSpin
Copy link

UncertainSpin commented Apr 25, 2018

Hello,

I am attempting to make some modifications to OpenSTF and am encountering some issues when trying to interact between classes. This appears to be due to STF-Syrup's dependency injection blocking functions from an external scope.

In short, I would like to be able to reference functions in bar.js (Uses Syrup) FROM foo.js (Does not use Syrup). I can successfully require and assign bar.js, but instead of a typical class object, I receive a SerialSyrup object that util.inspect shows is constructed like this:

SerialSyrup {
  options: {},
  body: [Function],
  dependencies:
   [ SerialSyrup {
       options: {},
       body: [Function],
       dependencies: [],
       resolved: null } ],
  resolved: null }

Every attempt to reference functions that exist within the "module.exports" block of bar.js fails with "...is not a function" when called from foo.js . Is there a way I can reach inside this block, other than totally refactoring foo.js to be a Syrup module and importing bar.js as a dependency?

PS: Reposting from /openstf/stf-syrup, as that issue page appears dead.

@sorccu
Copy link
Member

sorccu commented Apr 25, 2018

Show your code.

@lenosze
Copy link

lenosze commented Jun 6, 2018

Same issue here. Attempting to modify the packages auto-cleanup to be scheduled every few days. For that I've written a simple cron that injects cleanup.js.

module.exports = syrup.serial()
  .dependency(require('./cleanup'))
  .define(function(options, cleanup) {
    var log = logger.createLogger('device:plugins:cleanupCron')
    return {
      uninstallPackagesCron: function() {
        nodeSchedule.scheduleJob(CLEANUP_EVERY_SUNDAY, function() {
          log.info('Cleanup job launched')
          // *logic here*
        })
      }
    }
  })

Cron is then required in app/index.js with the server startup

var cleanupCron = require('./../device/plugins/cleanupCron')

And what I get from logging the cleanupCron is the whole SerialSyrup object with no way to reach the uninstallPackagesCron function

 SerialSyrup {
  options: {},
  body: [Function],
  dependencies: 
   [ SerialSyrup {
       options: {},
       body: [Function],
       dependencies: [Array],
       resolved: null } ],
  resolved: null }

@sorccu
Copy link
Member

sorccu commented Jun 6, 2018

Look at https://github.com/openstf/stf/blob/master/lib/units/device/index.js. You’re not supposed to be able to require and use it directly. You have to call .consume() which then actually runs the function and returns a promise that resolves with the result.

@sorccu sorccu closed this as completed Jun 6, 2018
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

3 participants